Migrating away from register globals

If you are luckily enough to work in a Web 2.0 start up, you probably won’t have to deal with too much legacy code. But for the rest of us, we can often find ourselves working with code which can be even decades out of date.

One of the big issues in PHP has been the deprecation of register globals. Of course, this happened quite a long time ago, because the idea of register globals was just plain stupid, but recent versions of PHP (5.3 onwards), will now throw a deprecation error.

So, we need to find a way to turn register globals off.

The end solution is of course to refactor the code so it doesn’t use register globals at all. Anything short of this is going to be a security nightmare, it’s like a ticking time bomb sitting on your server. But until then, there is a way you can emulate it in your PHP code while you work to get rid of it, allowing you to turn the register global settings off.

All you need is something like this in your code.

foreach ($_REQUEST as $key => $val) {
	$$key = $val;
}
Timeline

Newsletter

Don't have time to check my blog? Get a weekly email with all the new posts. This is my personal blog, so obviously it is 100% spam free.

Metadata

Tags: , , ,

This entry was posted on Friday, April 27th, 2012 at 11:51 am and is filed under Programming, Tech. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.