$_SERVER variables provide important information about the Apache webserver environment. There are numerous $_SERVER variables providing a different kind of information like the IP address, port number of the web server, the client browser information, document root folder of the web server, etc.. In this tutorial, I have mentioned 10 most important and frequently used variables which I think every PHP developer should know.
This variable can be used to detect the user’s browser language. The retrieved information can be useful in automatically configuring the website text in localized language of the user’s language.
//Detect browser language $lang=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']); echo substr($lang,0,2);
Similarly browser locale information can be retrieved using following code.
//Detect browser locale $lang=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']); echo $lang;
This variable gives the IP address of the server under which the current script is executing.
echo "SERVER_ADDR : " . $_SERVER['SERVER_ADDR'];
This server environment variable returns the name of the server host under which the current script is executing.
echo "SERVER_NAME : " . $_SERVER['SERVER_NAME'];
Returns the document root directory under which the current script is executing, as defined in the server’s configuration file.
echo "DOCUMENT_ROOT : " . $_SERVER['DOCUMENT_ROOT'];
This is the most important variable. HTTP_USER_AGENT is essential in knowing the user agent (browser).
echo "HTTP_USER_AGENT : " . $_SERVER['HTTP_USER_AGENT'];
The IP address from which the user is viewing the current page can be retrieved from this server variable.
echo "REMOTE_ADDR : " . $_SERVER['REMOTE_ADDR'];
Returns the port being used on the user’s machine to communicate with the web server.
echo "REMOTE_PORT : " . $_SERVER['REMOTE_PORT'];
This server environment variable gives the filename of the currently executing script, relative to the document root.
echo "PHP_SELF : " . $_SERVER['PHP_SELF'];
The timestamp of the start of the request. Available since PHP 5.1.0.
echo "REQUEST_TIME : " . $_SERVER['REQUEST_TIME'];
Returns the main web server configured parameters.
echo "SERVER_SOFTWARE : " . $_SERVER['SERVER_SOFTWARE'];
Comments are closed.