Thursday, December 31, 2009

fontreplace - PHP/jQuery Font Replacement (FLIR)

Lately I was quite into web design and interface so much that I started exploring sIFR and FLIR. Yesterday I decided to develop a FLIR for PHP and jQuery.

You can download the fontreplace package at: http://www.mediafire.com/?0o511knwywm (122 kB, zip format).

Features:
  • PHP 4.4.2 and above compatible
  • Uses jQuery Javascript Framework
  • Transparent background for CSS background support
  • Lightweight
  • Reads font color, font size, and font dynamically.
  • Detects whether user has font before loading from server
  • 100% SEO Friendly and minimal code changes
  • Supports font color RGBA with alpha.
  • Tested working on IE7, IE8, FF 2.0, 3.0, 3.5, Safari (Win) 4.0 and Chrome 3.0, 4.0

The demo and example is inside the zip file. Place the content of the zip file on a PHP-enabled server and run it on the browser.

Sunday, December 27, 2009

The Art of Password Protection in PHP

While I was working on some algorithm on PHP earlier on, I began to thought about the methods of password protection when I was still a novice in PHP, till now where I stand. Here's a little bit on how I went through the stages of password protection.

The Art of Password Protection, MD5, SHA1, Salting, php code

Of course my website wasn't hack and I wasn't that bad NOT to hash the password at all. When I started off building my first website, betaPod, I actually used MD5 for password to be hashed. Afterwhich, I learnt about how MD5 passwords can be rainbow-tabled and cracked, and came to know about what is called salting.

Then when I thought that if salting is not enough, I came up with positions of the salt based on the password entered, which makes the hash very unique and secured.

You can definitely use the code in the image.

Wednesday, November 25, 2009

Issues with multiple requests on PHP

Again today I was answering questions on StackOverflow when I came across the question about not being able to load pages when downloading/loading large files.

If you are using session, I suggest you use session_write_close() before you output the file to the browser.

This is because when the session is opened on one page, you cannot load another page until the session has been written and released. session_write_close() is called automatically when your script ends, but because your outputting process takes time before your script end, your session file is locked and thus other pages cannot be viewed.

However, if you are using different browser and/or system, it will be ok because the session file locked is unique to each SESSION ID.

Look at: http://php.net/manual/en/function.session-write-close.php

However do take note that after session_write_close(), you cannot call session_start() or there will be a warning. Also if you make changes to $_SESSION, it will not take effect.

Monday, November 2, 2009

Getting value from an object (or instance of a class) in PHP 5

Just earlier on I was working on the String class in the Samstyle PHP Framework. I was glad that I read the PHP manual and found this magical method in PHP5 OOP called __toString().

Using __toString(), you can actually return a value for the object/instance of a class.

See the example below:

class MyClass{

  function __construct(){
    // constructor
  }

  function __toString(){
    // to String
    return 5;
  }

}

$inst = new MyClass();

echo $inst; // echos 5

How powerful!

Friday, October 30, 2009

jQuery-like chaining in PHP's Object Oriented Programming

It's interesting to see how object methods in PHP can be chained just like jQuery's chaining.

jQuery's chaining:

$("div").children(".firstbtn").hide().fadeIn();

In PHP, it is also possible to chain similarly.

Using the string class from Samstyle PHP Framework, we can do chaining in PHP like this:

echo $str->trim()->replace('a', 'b')->value();

To do chaining in PHP, simply add a return statement at the end your methods where appropriate.

return $this;

Advisable to only do this kind of chaining for Action methods, meaning to say that these methods are only doing actions which are not supposed to return value.

Hope this helps!

Monday, October 12, 2009

file_get_contents() vs fread() - Speed Test and Benchmarking

File access is commonly found in any PHP application. Be it caching, or reading data from a file or what. Lately I've been wondering, whether the use of file_get_contents() over fread() is a good practice (i always prefer file_get_contents()).

So this morning I ran a test between this 2 methods of reading a file. The script I used to run is at http://thephpcode.pastebin.com/f2e84fab1

So here's the result from the tests of 5 runs:
 fread() (seconds)file_get_contents() (seconds)
Mean0.036740.01322
Min0.0307109355926510.0098111629486084
Max0.045550823211670.016166925430298


So here it's clear that file_get_contents() is much faster than fread(), mainly because fread() consists of an overhead additional loop and several functions.

Saturday, October 10, 2009

Using Bit Shifting to Divide / Multiply

To think of it, I rarely use bitwise operators in my coding - especially with all the convenience of other operators (can you imagine calculating bits whole day?).

But seriously when I was reading on Bitwise operators, I realised that ($n / 2) == ($n >> 1). So i tested out and...

By shifting bits, we can actually do multiplication and division. But however, this is only true when ($n / $x) is still a integer.


$n = 1600;

var_dump(($n / 2) == ($n >> 1));
var_dump(($n / 4) == ($n >> 2));
var_dump(($n / 8) == ($n >> 3));
var_dump(($n / 16) == ($n >> 4));

var_dump(($n * 2) == ($n << 1));
var_dump(($n * 4) == ($n << 2));
var_dump(($n * 8) == ($n << 3));
var_dump(($n * 16) == ($n << 4));


I ran some tests on which one is faster, and found that bitshifting is actually much faster.

Here are the results:
 Normal (seconds)Bitshift (seconds)
Mean0.124070.10461
Min0.1229791641240.104335069656
Max0.1260280609130.105180025101
Post to the testing script: http://thephpcode.pastebin.com/f1f6ec979

So when you do some simple math in programming, you know ways to speed things up.

Pre vs Post Increment in PHP

Earlier on I was browsing Google Code, and I chanced upon JSpeed - a javascript optimizer. I saw that they actually change post increments to pre-increments. I wondered why and thought I could ask this on Stack Overflow.

One theory came up to be "preincrement (++i) adds one to the value of i, then returns i; in contrast, i++ returns i then adds one to it, which in theory results in the creation of a temporary variable storing the value of i before the increment operation was applied". -- http://physical-thought.blogspot.com/2008/11/pre-vs-post-increment-speed-test.html

So I thought, what about PHP?

I went on to test it on PHP with a FOR loop and I have the following code:
<?php

$time_start = microtime_float();

for($i = 0; $i<10000000;$i++){

}

$time_end = microtime_float();
$loadedin = (float)($time_end - $time_start);
echo $loadedin.' s
';

$time_start = microtime_float();

for($i = 0; $i<10000000;++$i){

}

$time_end = microtime_float();
$loadedin = (float)($time_end - $time_start);
echo $loadedin.' s
';

function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}


?>


Amazingly, pre-increment is much faster than post-increment in PHP. Look at the results:
 Pre-inc (seconds)Post-inc (seconds)
Mean0.686950.74147
Min0.6859259605410.740597963333
Max0.688167810440.743010044098


Thus I conclude that pre-increment is useful in for loops than post-increment. It is much faster through the iterations.