10 Essential $_SERVER variables PHP developer must know

$_SERVER variables
Spread the love

$_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.

1. HTTP_ACCEPT_LANGUAGE

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],0,2);

Similarly browser locale information can be retrieved using following code.

//Detect browser locale 
 $lang=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
 echo $lang[0];

2. SERVER_ADDR

This variable gives the IP address of the server under which the current script is executing.

echo "SERVER_ADDR : " . $_SERVER['SERVER_ADDR'];

3. SERVER_NAME

This server environment variable returns the name of the server host under which the current script is executing.

echo "SERVER_NAME : " . $_SERVER['SERVER_NAME'];

4. DOCUMENT_ROOT

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'];

5. HTTP_USER_AGENT

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'];

6. REMOTE_ADDR

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'];

7. REMOTE_PORT

Returns the port being used on the user’s machine to communicate with the web server.

echo "REMOTE_PORT : " . $_SERVER['REMOTE_PORT'];

8. PHP_SELF

This server environment variable gives the filename of the currently executing script, relative to the document root.

echo "PHP_SELF : " . $_SERVER['PHP_SELF'];

9. REQUEST_TIME

The timestamp of the start of the request. Available since PHP 5.1.0.

echo "REQUEST_TIME : " . $_SERVER['REQUEST_TIME'];

10. SERVER_SOFTWARE

Returns the main web server configured parameters.

echo "SERVER_SOFTWARE : " . $_SERVER['SERVER_SOFTWARE'];

Spread the love

Posted on July 18, 2019 in Website Design

Meet The Author

Jobin John
Jobin is a digital marketing professional with over 10 years of experience in the industry. He has a passion for driving business growth in the online realm. With an extensive background spanning SEO, web design, PPC campaigns, and social media marketing, Jobin masterfully crafts strategies that resonate with target audiences and achieve measurable outcomes.
Back to Top
Top