Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    28

    Question 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

  2. #2
    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'
    More details are on the PHP site
    http://www.php.net/manual/en/languag...predefined.php

  3. #3
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    28

    Thumbs up 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

  4. #4
    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

  5. #5
    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).
    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.
    The New Zealand Site
    http://www.thenewzealandsite.com

  6. #6
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •