Results 1 to 6 of 6
-
08-30-2002, 11:17 PM #1Newbie
- Join Date
- Jul 2001
- Location
- New Zealand
- Posts
- 28
Newbie question: php environment variables undefined
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?The New Zealand Site
http://www.thenewzealandsite.com
-
08-31-2002, 05:50 AM #2Web Hosting Master
- Join Date
- May 2002
- Location
- UK
- Posts
- 2,997
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
PHP Code:$_SERVER['HTTP_USER_AGENT']
http://www.php.net/manual/en/languag...predefined.php
-
09-01-2002, 02:48 AM #3Newbie
- Join Date
- Jul 2001
- Location
- New Zealand
- Posts
- 28
beautiful! that's exactly what it was!
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.The New Zealand Site
http://www.thenewzealandsite.com
-
09-01-2002, 11:44 PM #4Web Hosting Master
- Join Date
- Nov 2000
- Posts
- 3,046
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).
A well-reasoned assumption is very close to fact.
- Adorno
-
09-02-2002, 04:44 AM #5Newbie
- Join Date
- Jul 2001
- Location
- New Zealand
- Posts
- 28
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).The New Zealand Site
http://www.thenewzealandsite.com
-
09-09-2002, 09:03 AM #6Web Hosting Master
- Join Date
- May 2002
- Location
- UK
- Posts
- 2,997
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.