Web Hosting Talk







View Full Version : Newbie question: php environment variables undefined


xor
08-30-2002, 11:17 PM
I hope this is just a dumb newbie question with an easy solution/explanation...

I'm a ColdFusion developer who is now learning PHP and I'm having a bit of a problem. Any advice is appreciated.

I've got PHP installed and running (W2K, IIS5), I can call phpinfo() and all the variables display just fine. But when I write code to try to display some of the same variables I get the following error message:

Notice: Undefined variable: HTTP_USER_AGENT in D:\wwwroot\FB3Dev\test.php on line 14

Well, that is my script alright. The code I am using is:

<?php
echo $HTTP_USER_AGENT;
?>

I'm getting this out of tutorials I've printed from the net, and quadruple-checking my syntax. I can't see what I'm doing wrong. I've also tried with other variables such as $SCRIPT_NAME and $PHP_SELF, with similar results. I'm able to successfully set and display the value of other variables, it just seems to have a problem with the built-in variables, but the odd thing is that phpinfo() gives me the right result!

Any ideas/suggestions?

Rich2k
08-31-2002, 05:50 AM
I don't know what version of PHP you are using but in the most recent (with Register_globals disabled) those methods of calling the environment variables have been changed.

Now they are in arrays.

E.g. to get HTTP_USER_AGENT use

$_SERVER['HTTP_USER_AGENT']

More details are on the PHP site
http://www.php.net/manual/en/language.variables.predefined.php

xor
09-01-2002, 02:48 AM
Thanks Rich2k!
That is exactly what the problem was.

This must pose quite an upgrade problem for existing php sites migrating to the latest version if they have, for instance, used forms with the action page pointing to the form page using $PHP_SELF.

But this is so good to know because it could be a real "gotcha" if I were to use a host running a slightly older version of php.

JustinH
09-01-2002, 11:44 PM
this is why I never rarely use predefined variables in my script. Or set all of them I need at the top of the page :). Eventually things like this change, which is why it is SO important to write good code (and use comments).

xor
09-02-2002, 04:44 AM
Originally posted by comphosting
this is why I never rarely use predefined variables in my script. Or set all of them I need at the top of the page :). Eventually things like this change, which is why it is SO important to write good code (and use comments).

I couldn't agree more! I've been using Fusebox2 methodology with my ColdFusion coding and now I am learning PHP and Fusebox3. That's how I ran into the problem with the predefined variables. As you say they are definitely something to put into the app_globals (or whatever it's equivalent in FuseBox3) file in fusebox-speak, ie a top-level file that gets included into all the others.

Rich2k
09-09-2002, 09:03 AM
I tend to use an include function in which I set the variables I am likely to use into my own array. That way you only ever need to change one file if the language changes.