Pages

Feb 3, 2011

Mac OS and XAMPP: conflicts with native PHP

My developments on my macbook have been on hold for quite some time now... Last time I used PHP on it, it was running Leopard and everything was running smooth. Today (with Snow Leopard), while performing a simple PHP task (using php-cli), it shouted the following error:

PDO Connection Error: SQLSTATE[HY000] [2002] No such file or directory

After more investigation then I'd like.. I found the problem.

The php-cli I was using was pointing to the OS's native php (which I've never used).

All I needed to do was:
$ mv /usr/bin/php /usr/bin/php-native
$ cd /usr/bin
$ ln -s /Applications/XAMPP/xamppfiles/php php

This way I kept a copy of OS's native PHP, and created a symlink to XAMPP's PHP.

Feb 1, 2011

php url-safe base64 encoding

Basically:
  • '+' is replaced by '-'
  • '/' is replaced by '_'(underscore)
  • '=' is replaced by '!'

function urlsafe_b64encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;
}

function urlsafe_b64decode($string) {
    $data = str_replace(array('-','_'),array('+','/'),$string);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    return base64_decode($data);
}