PHP

PHP + MySQL on Leopard

I’ve just tried to run PHP with MySQL on my Leopard and the first problem: “Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’”

I’ve just changed php.ini to:
mysql.default_socket = /tmp/mysql.sock
The same situation with mysqli settings - be default it is not set so you should update it to the real path.
And it works!

Convertation IP address to number

Here is the function, may somebody will find it usefull.

function
ipToNumber($sIp) {
$sPattern = ‘(25[0-5]|2[0-4]\d|[01]?\d\d|\d)’;
if(!preg_match(“/^{$sPattern}\\.{$sPattern}\\.
{$sPattern}
\\.{$sPattern}$/”
, $sIp)) {
// invalid IP
return -1;
} else {
$aNums = explode(‘.’, $sIp);
$i = count($aNums);
foreach($aNums as $nKey => $nValue) {
$i–;
$nResult += ($aNums[$nKey] * pow(256, $i));
}
return $nResult;
}
}

Singleton pattern with PHP

Sometimes you need only one instance of your class. For example it can be usefull to create only one instance of your database access class and use it everywhere in application. This will reduce memory usage and prevent you from having more than one database connection.

So, your class should look like bellow:

final class Database
{
    private static $instance;
   
    private function __construct()
    {
        // this code will never happen
    }
   
    public static function getInstance()
    {
       if (!isset(self::$instance)) {
           $class = __CLASS__;
           self::$instance = new $class;
       }   
       return self::$instance;
    }
   
    public function __clone()
    {
       trigger_error(‘It is impossible to clone singleton’, E_USER_ERROR);
    }
   
}

This class is defined like final to prevent extentding, usualy people forget about it. It is not a good idea to extend singleton classes because of static members. But if you’ll do this, you also should redefine getInstance() method and $instance member.

Strange things

while (list(,$vvv) = @each($aTmp)) {
  $kkkk = substr($vvv,0,strpos($vvv,‘=’));
  $vvvv = substr($vvv,strpos($vvv,‘=’)+1);
  $aValues[$kkkk] = $vvvv;
}

This is a part from the code I should work with. No comments.

MySQL new password problem

This morning is rich for troubles :)
Some PHP script was to act with MySQL database, but it failed with: “Warning: mysql_connect(): Client does not support authentication protocol requested by server; consider upgrading MySQL … Client does not support authentication protocol requested by server; consider upgrading MySQL client“.
I’ve digged a bit and solution was very simple -
SET PASSWORD FOR user@localhost = OLD_PASSWORD(‘newpassword’);
This updates password to use old authentication protocol.

Permissions problems with PHP on IIS

Today I had a problem - on Windows 2000/IIS server PHP didn’t work and scripts were failing with 401.3 error. The solution was very simple - Read/Execute permissions were to be set on PHP directory for IUSR_* and IWAM_* accounts. Since that has been done all works perfect. I
know this is a well known problem, but anyway a lot of people are fighting with it every day.

SQL-injections Vs Memrefresh

Yep, at first stage of development I was so busy with logic that absolutely forgot about security. And as result now you can login to alpha Memrefresh version with login “‘ or 1=1 or ‘1′=’1″ and any password. Hurry up I am planning to fix this at monday.

PS and this situation will never happend if I’ve used Java/C# prepared statements.