Predefined Variables

PHP provides a large number of predefined variables to all scripts. The variables represent everything from external variables to built-in environment variables, last error messages to last retrieved headers.

See also the FAQ titled "How does register_globals affect me?"

Table of Contents

Коментарии

Be carful when using $_SERVER['DOCUMENT_ROOT']; in your applications where you want to distribute them to other people with different server types. It isnt always supported by the webserver (IIS).
2004-04-06 00:20:34
http://php5.kiev.ua/manual/ru/reserved.variables.html
Nothing about the message-body ...

You can get cookies, session variables, headers, the request-uri , the request method, etc but not the message body. You may want it sometimes when your page is to be requested with the POST method.

Maybe they should have mentioned $HTTP_RAW_POST_DATA or php://stdin
2004-10-19 11:13:37
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
The Environment variable $ENV is useful for coding portable platform specific application constants. 

// Define a Windows or else Linux root directory path
$_ENV['OS'] == 'Windows_NT' ? $path = 'L:\\www\\' : $path = ' /var/www/';

define('PATH', $path);

echo PATH;
2005-02-13 18:19:15
http://php5.kiev.ua/manual/ru/reserved.variables.html
If you use Apache's redirection features for custom error pages or whatever, the following Apache's REDIRECT variables are also available in $_SERVER:
$_SERVER['REDIRECT_UNIQUE_ID]' 
$_SERVER['REDIRECT_SCRIPT_URL]' 
$_SERVER['REDIRECT_SCRIPT_URI]' 
$_SERVER['REDIRECT_SITE_ROOT]' 
$_SERVER['REDIRECT_SITE_HTMLROOT]' 
$_SERVER['REDIRECT_SITE_CGIROOT]' 
$_SERVER['REDIRECT_STATUS]' 
$_SERVER['REDIRECT_QUERY_STRING]' 
$_SERVER['REDIRECT_URL]' 

I'm not sure if this is a complete list though
2005-06-07 09:33:22
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
Warning: $_SERVER['PHP_SELF'] can include arbitrary user input. The documentation should be updated to reflect this.

The request "http://example.com/info.php/attack%20here" will run /info.php, but in Apache $_SERVER['PHP_SELF'] will equal "/info.php/attack here". This is a feature, but it means that PHP_SELF must be treated as user input.

The attack string could contain urlencoded HTML and JavaScript (cross-site scripting) or it could contain urlencoded linebreaks (HTTP response-splitting).

The use of $_SERVER['SCRIPT_NAME'] is recommended instead.
2005-07-24 09:59:24
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
$_SERVER['QUERY_STRING'] 

Does not contain XHTML 1.1 compliant ampersands i.e. &

So you will need to do something like this if you are to use $_SERVER['QUERY_STRING'] in URL's.

//  XHTML 1.1 compliant ampersands 
$_SERVER['QUERY_STRING'] = 
str_replace(array('&', '&'), array('&', '&'), 
$_SERVER['QUERY_STRING']);
2005-07-31 05:41:06
http://php5.kiev.ua/manual/ru/reserved.variables.html
Re: You can take advantage of 404 error to an usable redirection using REQUEST_URI ...

Whilst this is effective, a line in the .htaccess such as:

RewriteEngine On
RewriteRule ^profiles/([A-Za-z0-9-]+) showprofile.php?profile=$1 [L,NC,QSA]

will throw the requested profile in a variable $profile to the showprofile.php page. 

You can further enhance the url (e.g http://servername/profiles/Jerry/homeaddress/index.htm) and the second variable value homeaddress becomes available in $url_array[3] when used below $url_array=explode("/",$_SERVER['REQUEST_URI']);   

Hope this helps - Works well for me

Drew
2005-09-30 11:51:36
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
Also on using IPs to look up country & city, note that what you get might not be entirely accurate.  If their ISP is based in a different city or province/state, the IPs may be owned by the head office, and used across several areas. 
You also have rarer situations where they might be SSHed into another server, on the road, at work, at a friend's...  It's a nice idea, but as the example code shows, it should only be used to set defaults.
2006-02-22 10:05:16
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
$_GET may not handle query string parameter values which include escaped Unicode values resulting from applying the JavaScript "escape" function to a Unicode string.
To handle this the query parameter value can be obtained  using a function such as:

function getQueryParameter ($strParam) {
  $aParamList = explode('&', $_SERVER['QUERY_STRING']);
  $i = 0;
  while ($i < count($aParamList)) {
    $aParam = split('=', $aParamList[$i]);
    if ($strParam == $aParam[0]) {
      return $aParam[1];
    } 
  }
  return "";
}

or by directly building an array or query string values and then processing the parameter string using a function such as the "unescape" function which can be found at http://www.kanolife.com/escape/2006/03/unicode-url-escapes-in-php.html (or http://www.kanolife.com/escape/ for related info).
2006-03-07 15:35:10
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
I was unable to convince my hosting company to change their installation of PHP and therefore had to find my own way to computer $_SERVER["DOCUMENT_ROOT"].  I eventually settled on the following, which is a combination of earlier notes (with some typos corrected):

<?php
if ( ! isset($_SERVER['DOCUMENT_ROOT'] ) )
 
$_SERVER['DOCUMENT_ROOT'] = str_replace'\\''/'substr(
   
$_SERVER['SCRIPT_FILENAME'], 00-strlen($_SERVER['PHP_SELF']) ) );
?>
2006-04-01 00:56:08
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
So you have an application in your web space, with a URL such as this:

http://<host>/<installation_path>/

and pages such as

http://<host>/<installation_path>/subfolder1/subfolder2/page.php

You have a file called config.php in <installation_path> which is include()d by all pages (in subfolders or not).

How to work out <installation_path> without hard-coding it into a config file? 

<?php

// this is config.php, and it is in <installation_path>
// it is included by <installation_path>/page.php
// it is included by <installation_path>/subfolder/page2.php
// etc

$_REAL_SCRIPT_DIR realpath(dirname($_SERVER['SCRIPT_FILENAME'])); // filesystem path of this page's directory (page.php)
$_REAL_BASE_DIR realpath(dirname(__FILE__)); // filesystem path of this file's directory (config.php)
$_MY_PATH_PART substr$_REAL_SCRIPT_DIRstrlen($_REAL_BASE_DIR)); // just the subfolder part between <installation_path> and the page

$INSTALLATION_PATH $_MY_PATH_PART
   
substrdirname($_SERVER['SCRIPT_NAME']), 0, -strlen($_MY_PATH_PART) )
    : 
dirname($_SERVER['SCRIPT_NAME'])
// we subtract the subfolder part from the end of <installation_path>, leaving us with just <installation_path> :)

?>
2006-04-14 09:18:23
http://php5.kiev.ua/manual/ru/reserved.variables.html
Refer to CanonicalName if you are not getting the ServerName in the $_SERVER[SERVER_NAME] variable....This was a pain to figure out for me...now it works as expected by turning canonical naming on.

http://www.apacheref.com/ref/http_core/UseCanonicalName.html
2006-05-05 15:19:41
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
I think it is very important to note that PHP will automatically replace dots ('.') AND spaces (' ') with underscores ('_') in any incoming POST or GET (or REQUEST) variables.

This page notes the dot replacement, but not the space replacement:
http://us2.php.net/manual/en/language.variables.external.php

The reason is that '.' and ' ' are not valid characters to use in a variable name.  This is confusing to many people, because most people use the format $_POST['name'] to access these values.  In this case, the name is not used as a variable name but as an array index, in which those characters are valid.

However, if the register_globals directive is set, these names must be used as variable names.  As of now, PHP converts the names for these variables before inserting them into the external variable arrays, unfortunately - rather than leaving them as they are for the arrays and changing the names only for the variables set by register_globals.

If you want to use:
<input name="title for page3.php" type="text">

The value you will get in your POST array, for isntance would be:
$_POST['title_for_page3_php']
2007-01-24 13:53:18
http://php5.kiev.ua/manual/ru/reserved.variables.html
If you have problems with $_SERVER['HTTPS'], especially if it returns no values at all you should check the results of phpinfo(). It might not be listed at all. 
Here is a solution to check and change, if necessary, to ssl/https that will work in all cases:

<?php
if ($_SERVER['SERVER_PORT']!=443) {
$sslport=443//whatever your ssl port is
$url "https://"$_SERVER['SERVER_NAME'] . ":" $sslport $_SERVER['REQUEST_URI'];
header("Location: $url");
}
?>

Of course, this should be done before any html tag or php echo/print.
2007-03-21 14:22:45
http://php5.kiev.ua/manual/ru/reserved.variables.html
Автор:
SECURITY RISK !

Never ever trust the values that comes from $_SERVER.

HTTP_X_FORWARDED, HTTP_X_FORWARDED_FOR, HTTP_FORWARDED_FOR, HTTP_FORWARDED, etc.. can be spoofed !

To get the ip of user, use only $_SERVER['REMOTE_ADDR'], otherwise the 'ip' of user can be easily changed by sending a HTTP_X_* header, so user can escape a ban or spoof a trusted ip.

Of course this is well know, but I don't see it mentioned in these notes..

If you use the ip only for tracking (not for any security features like banning or allow access to something by ip), you can also use HTTP_X_FORWARDED to get user's ip what are behind proxy.
2007-11-22 13:49:46
http://php5.kiev.ua/manual/ru/reserved.variables.html
I use HTTP_X_FORWARDED_FOR because my webserver is behind a reverse proxy.
This can be made secure:
Configure the reverse proxy to block this field, and override it correctly.
Configure the apache server to only accept incoming connections from the reverse proxy.
2011-03-15 04:32:48
http://php5.kiev.ua/manual/ru/reserved.variables.html

    Поддержать сайт на родительском проекте КГБ