Im trying to access one of the standard CGI variables (such as $DOCUMENT_ROOT or $HTTP_REFERER) in a user-defined function, and it can seem to find it. Whats wrong?
It’s important to realize that the PHP directive register_globals also affects server and environment variables. When register_globals = off (the default is off since PHP 4.2.0), $DOCUMENT_ROOT will not exist. Instead, use $_SERVER[‘DOCUMENT_ROOT’] . If register_globals = on then the variables $DOCUMENT_ROOT and $GLOBALS[‘DOCUMENT_ROOT’] will also exist. If you’re sure register_globals = on and wonder why $DOCUMENT_ROOT isn’t available inside functions, it’s because these are like any other variables and would require global $DOCUMENT_ROOT inside the function. See also the manual page on variable scope. It’s preferred to code with register_globals = off. Note: Superglobals: availability note Superglobal arrays such as $_GET, $_POST, and $_SERVER, etc. are available as of PHP 4.1.0.
Related Questions
- Im trying to access one of the standard CGI variables (such as $DOCUMENT_ROOT or $HTTP_REFERER) in a user-defined function, and it can seem to find it. Whats wrong?
- Can the Server Side Include (SSI) echo directive display the values of standard CGI variables?
- The address for some local variables and function parameters seem to be wrong. Why?