If you’ve been directed to this page, that means that you’re complaining about how your variables in a POST or GET aren’t being carried over to the next page. You’ve been sent here because its an *extremely* common pitfall, and yet one that is exceptionally easy to work around if you know what to do – and also aggravating to explain over and over, hence this page.

What it probably is
99.9% of the time, the webserver you’re running your script on has a configuration option called register_globals turned off.

How to find out

Create a blank document and type the following:

<?php phpinfo(); ?>

Save it as something you’ll remember, and upload it to your webserver. That will print out all your configuration settings for your PHP install. On that page somewhere, find the setting register_globals. I bet you ten bucks it says “Off”.

“Aww crap – it says they are off – what do I do?”
Easy… you have one of a few choices… We’ll start with the most recommended way first. Pay attention. This isn’t difficult, but you need to see what’s going on.

We’ll call the variable in question (you know, the one that isn’t showing up, which is why you’re here) $snipevar just for demonstration purposes. You were likely trying to print out or use your variable by just using $snipevar, which is understandable.

However when register_globals are turned off, you’ll have to call your variable as such:

$_POST[‘snipevar’], $_GET[‘snipevar’], or $_REQUEST[‘snipevar’]

You can read more about how each of the predefined variables work by going to the php manual page, located here: http://www.php.net/manual/en/language.variables.predefined.php

The super groovy thing about these reserved variables is that they are superglobal – meaning if you use them in functions, you don’t need to specify global $snipevar; in order for the function to be able to see it anymore. Just use the handy dandy superglobal variables, and it will know their value from anywhere in your scripts.

“Okay – I’ll do that, but just so I know, what are my other options?”
You only have a few options… one is to edit your php.ini file and turn register_globals back on. However if you didn’t know what they did in the first place (if you did, why would you be *here* after all) – I *strongly* suggest you not do that.

Your other possible choice – and this one depends on your server setup – is to stick an .htaccess file in the directory you need to turn globals back on in. Your .htaccess file would look like this:

<IfModule mod_php4.c>
php_flag register_globals on
</IfModule>

IF your server is set up to allow htaccess files to override your main settings, this may work for you. If not, youre shit outa luck, so get used to the superglobals!

ssd-virtual-servers-banner-468x60

Advertisement

468x60_makemoney
Previous post

Cropped Thumbnails using PHP and the GD Library

Next post

Burning in effigy... or somethin'

snipe

snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist. I'm the CTO at Mass Mosaic and the CEO of Grokability, Inc. in San Diego, CA. Tweet at me @snipeyhead or read more...