PHP environment variables. Everything you need to know about environment variables in PHP Translation

Immediately before starting the script, the server passes it some environment variables with information. Certain variables contain some headers, but not all (you can't get all the headers).

HTTP_ACCEPT- This variable lists all MIME data types that can be accepted by the browser. The line */* means that the browser understands any type.

HTTP_ACCEPT= */*

HTTP_REFERER- This variable represents information about the page from which the user came to this one. You can use this variable, for example, to track a user's movement around your site and then view the most popular routes.

HTTP_REFERER= http://www.spravkaweb.ru/

HTTP_COOKIE- This variable stores all URL-encoded Cookies.

HTTP_COOKIE=

HTTP_USER_AGENT- Identifies the user's browser. To establish the browser type, you need to check this line for the presence of words: if the browser is Internet Explorer, then the MSIE substring will be present, and if only the word Mozilla is present, then this is Netscape.

HTTP_USER_AGENT= Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0)

You have Internet Explorer

HTTP_HOST- Contains the domain name of the Web server on which the script ran. This variable is quite convenient to use, for example, to generate the full path, which is required in the Location header so as not to be tied to a specific server.

HTTP_HOST= www.spravkaweb.ru

HTTP_FROM- Email address of the user who sent the request.

HTTP_FROM=

SERVER_NAME- Domain name or server IP address.

SERVER_NAME= www.spravkaweb.ru

SERVER_SOFTWARE- The name and version of the server program responding to the client request.

SERVER_SOFTWARE= Apache/1.3.33 (Unix) mod_jk/1.2.8 mod_auth_passthrough/1.8 mod_log_bytes/1.2mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a PHP-CGI/0.1b

SERVER_PORT- This variable contains the server port that the user's browser accessed. Typically this is 80. The variable can also be used to form the Location header parameter.

SERVER_PORT= 80

SERVER_PROTOCOL- The variable contains the name and version of the information protocol that was used for the request.

SERVER_PROTOCOL= HTTP/1.0

REMOTE_ADDR- This variable contains the IP address (or domain name) of the user's host on which the browser was launched.

REMOTE_ADDR= 212.94.114.177

REMOTE_PORT- The port that is assigned to the user's browser to receive the server response.

REMOTE_PORT= 4277

REMOTE_USER- The identification name of the user sending the request.

REMOTE_USER=

SCRIPT_NAME- Contains the name of the file containing this script. This variable is convenient to use when forming the Location header when redirecting to yourself (self-redirect), as well as for substituting the value of the action attribute of the tag

on the page that the script calls when launched without parameters (in order not to be tied to a specific script name).

SCRIPT_NAME=/pril.php

DOCUMENT_ROOT- Root directory of the Web document tree.

DOCUMENT_ROOT=/home/spravka/public_html

REQUEST_METHOD- The method the user uses when transferring data. It should be noted that a well-written script should itself determine, based on this variable, which method the user is using, and receive data from the appropriate source, and not expect that the transfer will be carried out, for example, only by the POST method.

REQUEST_METHOD= GET

QUERY_STRING- Contains parameters that appear after the question mark in the URL. Let us remind you that they are available both with the GET method and with the POST method (if in the latter case they were defined in the action attribute of the tag ).

QUERY_STRING= ss=getenv

PATH_INFO- Contains additional information about the path.

PATH_INFO=

PATH_TRANSLATED- The same information as in the PATH_INFO variable with a prefix that specifies the path to the root directory of the Web document tree.

PATH_TRANSLATED=

CONTENT_TYPE- Media data type of the request.

CONTENT_TYPE=

CONTENT_LENGTH- Returns the number of bytes of data sent by the user. This variable needs to be analyzed if you are accepting and processing a POST form.

GATEWAY_INTERFACE- The version of CGI that the server uses.

GATEWAY_INTERFACE= CGI/1.1


Environment Variables

Environment Variables:




echo "You came from: ".getenv("HTTP_REFERER")."
";
$br=getenv("HTTP_USER_AGENT");
if(strpos($br,"MSIE")!==false) echo "You have Internet Explorer
";
else echo "You have Netscape or others.
";
echo "Your IP: ".getenv("REMOTE_ADDR")."
";
echo "Here are the parameters in the browser line: ".getenv("QUERY_STRING");
?>

Variables

In PHP, variables begin with a dollar sign ( $ ). This character can be followed by any number of alphanumeric and underscore characters, but the first character cannot be a digit or an underscore. You should also remember that variable names in PHP are case sensitive, unlike keywords.

When declaring variables in PHP, it is not necessary to explicitly indicate the type of the variable; however, the same variable can have different types throughout the program.

A variable is initialized when a value is assigned to it and exists as long as the program is executed. That is, in the case of a web page, this means that until the request is completed.

External Variables

After a client request is parsed by the web server and passed to the PHP machine, the latter sets a number of variables that contain data related to the request and are available throughout its execution. First PHP takes environment variables your system and creates variables with the same names and values ​​in the PHP script environment so that scripts located on the server can access features of the client's system. These variables are placed in an associative array $HTTP_ENV_VARS(You can learn more about arrays in Chapter 4).

Naturally, array variables $HTTP_ENV_VARS are system dependent (since it is actually environment variables). You can view the values ​​of environment variables for your machine using the env (Unix) or set (Windows) command.

PHP then creates a group of GET variables that are created when the query string is parsed. The query string is stored in a variable $QUERY_STRING and represents the information following the symbol " ? " in the requested URL. PHP breaks the query string character-by-character & into individual elements, and then looks for the "=" sign in each of those elements. If the "=" sign is found, a variable is created with a name from the characters to the left of the equal sign. Consider the following form:

action = "http://localhost/PHP/test.php" method=" get">HDD: type=" text"name=" HDD"/>
CDROM: type=" text"name=" CDROM"/>
type=" submit"/>

If in this form you type, for example, “Maxtor” in the HDD line, and “Nec” in the CDROM line, it will generate the following request form:

http://localhost/PHP/test.php?HDD=Maxtor&CDROM=Nec

In our case, PHP will create the following variables: $HDD= "Maxtor" and $CDROM= "Nec".

You can work with these variables from your script (we use test.php) as with ordinary variables. In our case, they are simply displayed on the screen:

echo("

HDD is $HDD

"); echo("

CDROM is $CDROM

"); ?>

If a page request is made using the POST method, then a group of POST variables appears, which are also interpreted and placed in an array $HTTP_POST_VARS.

Environment Variables

Environment Variables in PHP

Immediately before starting the script, the server passes it some environment variables with information. Certain variables contain some headers, but not all (you can't get all the headers). Below I will provide a list of the most important environment variables.

HTTP_ACCEPT

This variable lists all MIME data types, which can be interpreted by the browser. The line */* means that the browser understands any type.

HTTP_ACCEPT= image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*

HTTP_REFERER

This variable represents information about the page from which the user arrived at this one. You can use this variable, for example, to track a user's movement around your site and then view the most popular routes.

HTTP_REFERER= http://www.spravkaweb.ru/php/pril/

HTTP_COOKIE

This variable stores all URL-encoded Cookies.

HTTP_COOKIE= hotlog=1; ZDEDebuggerPresent=php,phtml,php3; b=b; PHPSESSID=16805922a9258cda274316e60f649cf8

HTTP_USER_AGENT

Identifies the user's browser. To establish the browser type, you need to check this line for the presence of words: if the browser is Internet Explorer, then the MSIE substring will be present, and if only the word Mozilla is present, then this is Netscape.

For example:

HTTP_USER_AGENT= Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MyIE2; Maxthon)

HTTP_HOST

Contains the domain name of the Web server on which the script ran. This variable is quite convenient to use, for example, to generate the full path, which is required in the Location header so as not to be tied to a specific server.

HTTP_HOST= www.spravkaweb.ru

HTTP_FROM

Email address of the user who sent the request.

SERVER_NAME

Domain name or server IP address.

SERVER_NAME= www.spravkaweb.ru

SERVER_SOFTWARE

The name and version of the server program responding to the client request.

SERVER_PORT

This variable contains the server port that the user's browser accessed. Typically this is 80. The variable can also be used to form the Location header parameter.

SERVER_PROTOCOL

The variable contains the name and version of the information protocol that was used for the request.

SERVER_PROTOCOL= HTTP/1.1

REMOTE_ADDR

This variable contains the IP address (or domain name) of the user's host on which the browser was launched.

REMOTE_PORT

The port that is assigned to the user's browser to receive the server response.

REMOTE_USER

The identification name of the user making the request.

SCRIPT_NAME

Contains the name of the file containing this script. This variable is convenient to use when forming the Location header when redirecting to yourself (self-redirect), as well as for substituting the value of the action attribute of the tag on the page that the script calls when launched without parameters (in order not to be tied to a specific script name).

DOCUMENT_ROOT

The root directory of the Web document tree.

REQUEST_METHOD

The method the user uses when transferring data. It should be noted that a well-written script should itself determine, based on this variable, which method the user is using, and receive data from the appropriate source, and not expect that the transfer will be carried out, for example, only by the POST method.

QUERY_STRING

Contains the parameters that appear after the question mark in the URL. Let us remind you that they are available both with the GET and POST methods (if in the latter case they were defined in the action attribute of the tag ).

PATH_INFO

Contains additional information about the path.

PATH_TRANSLATED

Same information as in the variable PATH_INFO with a prefix specifying the path to the root directory of the Web document tree.

CONTENT_TYPE

Media data type of the request.

CONTENT_LENGTH

Returns the number of bytes of data sent by the user. This variable needs to be analyzed if you are accepting and processing a POST form.

GATEWAY_INTERFACE

The CGI version the server is using.

Example of using environment variables

Environment Variables

Environment Variables:

echo "You came from: ".getenv("HTTP_REFERER")."
";

$br=getenv("HTTP_USER_AGENT");

if(strpos($br,"MSIE")!==false) echo "You have Internet Explorer
";

else echo "You have Netscape or others.
";

echo "Your IP: ".getenv("REMOTE_ADDR")."
";

echo "Here are the parameters in the browser line: ".getenv("QUERY_STRING");

From the book The Art of Programming for Unix author Raymond Eric Stephen

From the book The Art of Programming for Unix author Raymond Eric Stephen

From the book Programming in Ruby [Language ideology, theory and practice of application] by Fulton Hal

From the book HTML 5, CSS 3 and Web 2.0. Development of modern Web sites. author Dronov Vladimir

From the book Application Development in the Linux Environment. Second edition author Johnson Michael K.

10.4. Environment Variables When a Unix program runs, the environment available to it includes a set of name-value relationships (both names and values ​​are strings). Some of them are installed manually by the user, others are installed by the system during

From the book Firebird DATABASE DEVELOPER'S GUIDE by Borri Helen

10.4.1. System Environment Variables There are many well-known environment variables that a program can obtain when run from a Unix shell. These variables (especially HOME) often need to be evaluated before reading the local file

From the book Linux Programming with Examples author Robbins Arnold

10.4.2. Custom Environment Variables Although applications are free to interpret environment variables outside of the system-defined set, actual use of such a feature is currently quite unusual.

From the author's book

10.4.3. When to Use Environment Variables What both user and system environment variables have in common is that they contain data that would be tedious to store in a large number of configuration files. And extremely tiring

From the author's book

14.4. Environment Variables Sometimes it is necessary to access environment variables, which are the link between the program and the outside world. Environment variables are simply labels associated with some text (usually small); they store, for example, paths to

From the author's book

Functions and variables. Local Variables Previously declared functions create their own variables within their body. These are so-called local variables. Such variables are only accessible within the function body in which they are declared. When execution completes

From the author's book

22.3.3. Environment Variables In programs that use setuid or setgid capabilities, special care must be taken with environment settings. These variables are determined by the user who activated the program, thereby opening the door for attacks. The most obvious attack

From the author's book

Environment variables Environment variables are global system settings that are used when the operating system initially boots. On Windows, Linux, and most UNIX systems, the Firebird server recognizes and uses certain environment variables if they

From the author's book

Where Windows Environment Variables Are SetThe type of environment variables and how they are set varies from one version of Windows to another. In table Table 3.1 shows the types (if applicable) and methods of setting the values ​​of environment variables. Table 3.1. Environment variable settings for

From the author's book

Chapter 2 Arguments, Options, and Environment Variables The first task of any program is usually to interpret command-line options and arguments. This chapter examines how C (and C++) programs receive their command line arguments, describing standard

Immediately before starting the script, the server passes it some environment variables with information. Certain variables contain some headers, but not all (you can't get all the headers). Below I will provide a list of the most important environment variables.

HTTP_ACCEPT

This variable lists all MIME data types, which can be interpreted by the browser. The line */* means that the browser understands any type.

HTTP_ACCEPT= image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*

HTTP_REFERER

This variable represents information about the page from which the user arrived at this one. You can use this variable, for example, to track a user's movement around your site and then view the most popular routes.

HTTP_REFERER= http://www.site/php/pril/

HTTP_COOKIE

This variable stores all URL-encoded Cookies.

HTTP_COOKIE= hotlog=1; ZDEDebuggerPresent=php,phtml,php3; b=b; PHPSESSID=

HTTP_USER_AGENT

Identifies the user's browser. To establish the browser type, you need to check this line for the presence of words: if the browser is Internet Explorer, then the MSIE substring will be present, and if only the word Mozilla is present, then this is Netscape.

For example:

HTTP_USER_AGENT= Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MyIE2; Maxthon)

HTTP_HOST

Contains the domain name of the Web server on which the script ran. This variable is quite convenient to use, for example, to generate the full path, which is required in the Location header so as not to be tied to a specific server.

HTTP_HOST= www.site

HTTP_FROM

Email address of the user who sent the request.

SERVER_NAME

Domain name or server IP address.

SERVER_NAME= www.site

SERVER_SOFTWARE

The name and version of the server program responding to the client request.

SERVER_PORT

This variable contains the server port that the user's browser accessed. Typically this is 80. The variable can also be used to form the Location header parameter.

SERVER_PORT= 80

SERVER_PROTOCOL

The variable contains the name and version of the information protocol that was used for the request.

SERVER_PROTOCOL= HTTP/1.1

REMOTE_ADDR

This variable contains the IP address (or domain name) of the user's host on which the browser was launched.

REMOTE_PORT

The port that is assigned to the user's browser to receive the server response.

REMOTE_USER

The identification name of the user making the request.

SCRIPT_NAME

Contains the name of the file containing this script. This variable is convenient to use when forming the Location header when redirecting to yourself (self-redirect), as well as for substituting the value of the action attribute of the tag on the page that the script calls when launched without parameters (in order not to be tied to a specific script name).

DOCUMENT_ROOT

The root directory of the Web document tree.

REQUEST_METHOD

The method the user uses when transferring data. It should be noted that a well-written script should itself determine, based on this variable, which method the user is using, and receive data from the appropriate source, and not expect that the transfer will be carried out, for example, only by the POST method.

QUERY_STRING

Contains the parameters that appear after the question mark in the URL. Let us remind you that they are available both with the GET and POST methods (if in the latter case they were defined in the action attribute of the tag ).

PATH_INFO

Contains additional information about the path.

PATH_TRANSLATED

Same information as in the variable PATH_INFO with a prefix specifying the path to the root directory of the Web document tree.

CONTENT_TYPE

Media data type of the request.

CONTENT_LENGTH

Returns the number of bytes of data sent by the user. This variable needs to be analyzed if you are accepting and processing a POST form.

GATEWAY_INTERFACE

The CGI version the server is using.

Example of using environment variables

Environment Variables

Environment Variables:


"; $br=getenv("HTTP_USER_AGENT"); if(strpos($br,"MSIE")!==false) echo "You have Internet Explorer
"; else echo "You have Netscape or others.
"; echo "Your IP: ".getenv("REMOTE_ADDR")."
"; echo "Here are the parameters in the browser line: ".getenv("QUERY_STRING"); ?>

Environment Variables

Environment Variables in PHP

Immediately before starting the script, the server passes it some environment variables with information. Certain variables contain some headers, but not all (you can't get all the headers). Below I will provide a list of the most important environment variables.

HTTP_ACCEPT

This variable lists all MIME data types, which can be interpreted by the browser. The line */* means that the browser understands any type.

HTTP_ACCEPT= image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*

HTTP_REFERER

This variable represents information about the page from which the user arrived at this one. You can use this variable, for example, to track a user's movement around your site and then view the most popular routes.

HTTP_REFERER= http://www.spravkaweb.ru/php/pril/

HTTP_COOKIE

This variable stores all URL-encoded Cookies.

HTTP_COOKIE= hotlog=1; ZDEDebuggerPresent=php,phtml,php3; b=b; PHPSESSID=16805922a9258cda274316e60f649cf8

HTTP_USER_AGENT

Identifies the user's browser. To establish the browser type, you need to check this line for the presence of words: if the browser is Internet Explorer, then the MSIE substring will be present, and if only the word Mozilla is present, then this is Netscape.

For example:

HTTP_USER_AGENT= Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MyIE2; Maxthon)

HTTP_HOST

Contains the domain name of the Web server on which the script ran. This variable is quite convenient to use, for example, to generate the full path, which is required in the Location header so as not to be tied to a specific server.

HTTP_HOST= www.spravkaweb.ru

HTTP_FROM

Email address of the user who sent the request.

SERVER_NAME

Domain name or server IP address.

SERVER_NAME= www.spravkaweb.ru

SERVER_SOFTWARE

The name and version of the server program responding to the client request.

SERVER_PORT

This variable contains the server port that the user's browser accessed. Typically this is 80. The variable can also be used to form the Location header parameter.

SERVER_PROTOCOL

The variable contains the name and version of the information protocol that was used for the request.

SERVER_PROTOCOL= HTTP/1.1

REMOTE_ADDR

This variable contains the IP address (or domain name) of the user's host on which the browser was launched.

REMOTE_PORT

The port that is assigned to the user's browser to receive the server response.

REMOTE_USER

The identification name of the user making the request.

SCRIPT_NAME

Contains the name of the file containing this script. This variable is convenient to use when forming the Location header when redirecting to yourself (self-redirect), as well as for substituting the value of the action attribute of the tag on the page that the script calls when launched without parameters (in order not to be tied to a specific script name).

DOCUMENT_ROOT

The root directory of the Web document tree.

REQUEST_METHOD

The method the user uses when transferring data. It should be noted that a well-written script should itself determine, based on this variable, which method the user is using, and receive data from the appropriate source, and not expect that the transfer will be carried out, for example, only by the POST method.

QUERY_STRING

Contains the parameters that appear after the question mark in the URL. Let us remind you that they are available both with the GET and POST methods (if in the latter case they were defined in the action attribute of the tag ).

PATH_INFO

Contains additional information about the path.

PATH_TRANSLATED

Same information as in the variable PATH_INFO with a prefix specifying the path to the root directory of the Web document tree.

CONTENT_TYPE

Media data type of the request.

CONTENT_LENGTH

Returns the number of bytes of data sent by the user. This variable needs to be analyzed if you are accepting and processing a POST form.

GATEWAY_INTERFACE

The CGI version the server is using.

Example of using environment variables

Share