Predefined Variables – PHP

PHP gives any script it runs a large number of variables that are already set up. PHP has an extra set of predefined arrays that hold variables from the web server, the environment, and what the user types in. Superglobals are the name for these new arrays.

Every scope has access to all of the following variables by default.

PHP Superglobals

1 $GLOBALS

Includes a reference to each variable that is currently available in the script’s global scope. The names of the global variables are the keys to this array.

2 $ SERVER

This is a list of things, like headers, paths, and script locations. This array is made up of entries made by the web server. There is no guarantee that every web server will offer any of these. For a full list of all the SERVER variables, see the next section.

3 $ GET

A group of variables that the HTTP GET method sends to the current script.

4 $ POST

A group of variables that were sent to the current script using the HTTP POST method.

5 $ FILES

The items that were sent to the current script using the HTTP POST method.

6 $ REQUEST

The contents of $_GET, $_POST, and $_COOKIE are stored in this array.

7 $ COOKIE

A group of variables that were sent to the current script through HTTP cookies.

8 $ SESSION

A set of session variables that can be used by the current script.

9 $ PHP SELF

A string with the name of the PHP script file that is being called.

10 $php errormsg

$php errormsg is a variable that holds the last error message that PHP made.

Server variables: $_SERVER

$_SERVER is an array that holds information like headers, paths, and script locations. This array is made up of entries made by the web server. There is no guarantee that every web server will offer any of these.

1 $_SERVER[‘PHP SELF’]

The name of the script file that is currently running, in relation to the document root.

2 $ SERVER[‘argv’]

A list of the arguments that were given to the script. This gives C-style access to the command line parameters when the script is run from the command line. If the GET method is used, this will have the query string.

3 $ SERVER[‘argc’]

If the script is run from the command line, this variable holds the number of command line parameters.

4 $ SERVER[‘GATEWAY INTERFACE’]

What version of the CGI standard the server is using, such as “CGI/1.1.”

5  $ SERVER[‘SERVER ADDR’]

The IP address of the server that is running the current script.

6 $ SERVER[‘SERVER NAME’]

The name of the server host where the script is currently running. If the script is running on a virtual host, this will be the value set for that virtual host.

7 $ SERVER[‘SERVER SOFTWARE’]

The server ID string is given in the headers when a request is answered.

8 $ SERVER[‘SERVER PROTOCOL’]

Name and version number of the information protocol that was used to request the page, such as “HTTP/1.0”;

9 $ SERVER[‘REQUEST METHOD’]

Which method of accessing the page was used, such as “GET,” “HEAD,” “POST,” or “PUT.”

10 $ SERVER[‘REQUEST TIME’]

The time stamp of when the request began. Available since PHP 5.1.0.

11 $ SERVER[‘QUERY STRING’]

If there was one, the query string that was used to get to the page.

12 $ SERVER[‘DOCUMENT ROOT’]

The document root directory under which the current script is running, as set in the server’s configuration file.

13  $ SERVER[‘HTTP ACCEPT’]

If the current request has an Accept: header, this is what it says.

14 $ SERVER[‘HTTP ACCEPT CHARSET’]

If the current request has an Accept-Charset: header, this is what it says. “iso-8859-1,*,utf-8” is an example.

15 $ SERVER[‘HTTP ACCEPT ENCODING’]

If the current request has an Accept-Encoding: header, this is what it says. Example: ‘gzip’.

16 $ SERVER[‘HTTP ACCEPT LANGUAGE’]

Contents of the current request’s Accept-Language: header, if there is one. Example: ‘en’.

17 $ SERVER[‘HTTP CONNECTION’]

If the current request has a Connection: header, this is what it says. “Keep-Alive” is an example.

18 $ SERVER[‘HTTP HOST’]

If the current request has a Host: header, this is what it says.

19 $ SERVER[‘HTTP REFERER’]

The address of the page that sent the user agent to this page, if any.

20 $ SERVER[‘HTTP USER AGENT’]

This is a string that shows what kind of user agent is accessing the page. Mozilla/4.5 [en] is a good example (X11; U; Linux 2.2.9 i586).

21 $ SERVER[‘HTTPS’]

Set to a value that is not empty if the script was asked for through HTTPS.

22 $ SERVER[‘REMOTE ADDR’]

The IP address of the computer from which the page is being viewed.

23 $ SERVER[‘REMOTE HOST’]

The name of the host where the current page is being viewed. The user’s REMOTE ADDR is used to do a reverse DNS lookup.

24 $ SERVER[‘REMOTE PORT’]

The port that the user’s computer is using to talk to the web server.

25 $ SERVER[‘SCRIPT FILENAME’]

The full name of the script that is currently running.

26 $ SERVER[‘SERVER ADMIN’]

The value given to the SERVER ADMIN directive in Apache’s configuration file for a web server.

27 $ SERVER[‘SERVER PORT’]

The port on the server machine that the web server uses to talk to other computers. By default, this will be set to ’80’.

28 $ SERVER[‘SERVER SIGNATURE’]

If server-generated pages are turned on, this string has the server version and the name of the virtual host.

29 $ SERVER[‘PATH TRANSLATED’]

The path to the current script based on the file system.

30 $ SERVER[‘SCRIPT NAME’]

It holds the path to the current script. This helps pages that need to link to themselves.

31 $ SERVER[‘REQUEST URI’]

The address that was given to get to this page, such as “/index.html.”

32 $ SERVER[‘PHP AUTH DIGEST’]

This variable is set to the “Authorization” header sent by the client when running as a module under Apache and using Digest HTTP authentication.

33  $ SERVER[‘PHP AUTH USER’]

This variable is set to the user’s username when running under Apache or IIS (ISAPI on PHP 5) as a module that does HTTP authentication.

34 $ SERVER[‘PHP AUTH PW’]

This variable is set to the user’s password when running under Apache or IIS (ISAPI on PHP 5) as a module that does HTTP authentication.

35 \s$ SERVER[‘AUTH TYPE’]

This variable is set to the authentication type when running under Apache as a module that does HTTP authentication.

People also search

Leave a Comment

Scroll to Top