Script Wrappers in PHP

I’m guessing that most web developers with a few years of experience have at least one nightmare client. I’m currently struggling with a total arse of a project. This one in particular was originally built some years ago by a predecessor of mine who, to be fair, was fairly lazy in a lot of respects.

Part of the work I’m currently doing on the site involves moving the whole she-bang over to one of our newer, faster, better severs. The initial move went surprisingly well, (I LOVE WHM’s account transfer features), but I soon hit upon a large stumbling block.

The previous developer was green enough to still make use of PHP’s long-deprecated register globals feature; the new server, being up-to-date and secure, obviously has the register_globals directive switched off.

The project is fairly huge, with lots of horrible spaghetti code. Sifting through every page, (of which there are thousands), substituting $myGrateVaryabul for $_GET[“myGrateVaryabul”] would have taken hours and I couldn’t see any easy way of search/replacing the code.

OK, I admit, I gave up and decided to allow register_globals to be switched on, but I just wanted it to be available for this one, single site on the server. After a quick Google search, I found that you can change the PHP configuration on a per-site basis with WHM/cPanel by simply uploading a custom php.ini to the website’s public_html folder. This seemed like an ideal solution but… the configuration is only applied to scripts within the same directory as the php.ini file; it’s not recursive! As I say, the site is huge and I wasn’t willing to upload 500 or so php.ini files… back to the drawing board.

My next idea was to change PHP’s configuration from a site-wide .htaccess file, using the php_value directive, but we have PHP installed as a CGI binary rather than an Apache module so that idea went instantly out of the window.

Sticking with the idea of using .htacess, I realised that I could use mod_rewrite to send all requests through a “wrapper” script. This solution worked like a charm, and I was soon basking in problem solving glory. (Once the initial ‘oh my god, what a bodge’ feeling wore off).

The .htaccess file:

RewriteEngine on
RewriteRule ^wrapper_script.php$ - [L]
RewriteRule \.php$ /wrapper_script.php [E=FILE:%{SCRIPT_FILENAME},L]

wrapper_script.php:


Share Button