faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

53 of 88 people (60%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

environmental variables such as query string are not passed to php code i.e. INCLUDE file="showacct.php"

Mar 15th, 2008 21:21
dman, ha mo, Philip Olson, Pavel Prishivalko, Techek, Matt Gregory, Johnny Akers, http://www.ttnr.org


A few reasons why $QUERY_STRING (or any predefined variable for that
matter) may not exist:
 a) The register_globals directive is off, which means all predefined 
    variables do not exist by simply appending a $ to them.  Instead, 
    (and always preferred), you'll go through a PHP predefined 
    variable, such as:
    print $HTTP_SERVER_VARS['QUERY_STRING'];
    print $_SERVER['QUERY_STRING']; // exists 4.1.0+
    As stated, this is the preferred method and can be read about 
    here:
      http://www.php.net/manual/en/language.variables.predefined.php
      http://www.php.net/manual/en/reserved.variables.php
 b) There is no query string, as no GET information (stuff after the 
    ? in the url) exists.  In this case, there is no query string.
 c) You're accessing this in a function.  In this case, it's important 
    to understand variable scope, see:
      http://www.php.net/manual/en/language.variables.scope.php
    In short, if you want to access variables** inside a function, 
    you need to define it as global first.  See the above manual 
    page for more information on variable scope.
If a variable exists before a file is included, it will exist inside the
include just the same.  This is true 100% of the time.
  $var = 'foo';
  include 'bar.php'; // $var is available in bar.php
** Superglobals are a little different, like $_SERVER does not need to
be defined global to be accessable in a function.  They are described in
the above man page (predefined variables).