What’s wrong with PHP?! part 3

The magic quotes, and the safe mode… ‘nough said.

Fortunately, both of these are deprecated in new versions of PHP.
Unfortunately, most hostings are not that keen on updating PHP to the newest version (mainly they’re still stuck at the likes of version 5.1.x).

What to do?

Get Rid of Magic Quotes

While testing locally, you often have pretty much the latest PHP version (be it from repositories, or manually installed). What a surprise it is to deploy your code to server with PHP version 5.1, and bang! – magic quotes strike!

Just put this as the very first thing in your PHP script (esp. if you can’t/don’t want to tweak your php.ini):

if (get_magic_quotes_gpc())
{
    function strip_array($var) { return is_array($var) ? array_map("strip_array", $var) : stripslashes($var); }
    $_POST = strip_array($_POST);
    $_SESSION = strip_array($_SESSION);
    $_GET = strip_array($_GET);
}

Safe Mode

The source of all evil, esp. as it strikes when you need to communicate with something outside PHP – e.g. sendmail – and everything dies with code 127 – “not found? what’s in the $PATH?…” No, it’s the safe mode!

Now you do have to change your php.ini, like it or not, and set

safe_mode = Off

and restart Apache.

Yes, that’s it, but I always forget this, and get frustrated going through permissions, paths, etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s