Updated July 5, 2023
Introduction to PHP $_SERVER
PHP comes out as a function of $_Server, which is a super global variable in PHP having information about paths and headers. Basically, these super variables are always available in the scope of any PHP code.
$_Server consists of an array that has the information created by the web server, such as headers, path, scriptlocations. It is directly related to the runtime environment of the current PHP script. All the information regarding the server can be get using this function. This is available from PHP version 4 and higher.
Syntax:
The Syntax for getting details using $ server function is:-
$_SERVER['VARIABLE_NAME']
The variable name contains the name of the information we need to get from the $server method.
For Ex:- Suppose we want to get the Server Name.
echo $_SERVER['SERVER_NAME'];
So from this, we can get the details of whatever is required for that PHP environment.
Screenshot:
Working of $_ SERVER Function
- The $_Server Function is a super global variable in PHP.
- $_ Server gets the information of the headers, path, and location of the script from the web server in the form of an Array.
- $_Server is basically set by the web server where the PHP code is deployed. There is no guarantee that the thing that server will provide all the details required; sometimes, there may be a case where the information is not listed, and sometimes some excess information can also come in place. It is directly related to the scripts at the run time.
Example of PHP $_SERVER
Let us see some functioning of $ Server with some examples:
1. $_Server[‘PHP_SELF’]
This defines the file name of the script where the current PHP code is being run. This gives the path of the PHP.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['PHP_SELF'];
?>
</body>
</html>
Output:
2. $_SERVER[‘argv’]
This stores the argument passed in an array, and the return type for this is an array.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['argv'];
?>
</body>
</html>
Output:
3. $_SERVER[‘argc’]
This gives information about the number of command line parameters.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['argc'];
?>
</body>
</html>
Output:
4. $_Server[‘GATEWAY_INTERFACE’]
This gives information about the common gateway interface, if any. If there is no gateway used, the result obtained is null.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['GATEWAY_INTERFACE'];
?>
</body>
</html>
Output:
Blank as no Gateway interface is used.
5. $_SERVER[‘SERVER_ADDR’]
It returns the IP address of the host server where the PHP script is being run. If ran on local, this gives the local IP address of the machine.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['SERVER_ADDR'];
?>
</body>
</html>
Output:
The IP address of the host machine.
6. $_SERVER[‘SERVER_SOFTWARE’]
This gives the detail of the software used in the server. this may be Apache or any hosted web servers.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['SERVER_SOFTWARE'];
?>
</body>
</html>
Output:
Apache (cent Os)
7. $_SERVER[‘SERVER_PROTOCOL’]
This gives the detail of the protocol via the request made. HTTP , HTTPS are the most probable information for this function.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['SERVER_PROTOCOL'];
?>
</body>
</html>
Output:
HTTP/1.1
8. $_SERVER[‘REQUEST_METHOD’]
This gives the detail of the request method used to access a particular page.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['REQUEST_METHOD'];
?>
</body>
</html>
Output:
Get /post /put / head
9. $_SERVER[‘REQUEST_TIME’]
This records the timestamp for the start of any request.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['REQUEST_TIME'];
?>
</body>
</html>
Output:
10. $_SERVER[‘QUERY_STRING’]
If any query is used for accessing the page, this method gives the information about that.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['QUERY_STRING'];
?>
</body>
</html>
Output:
If query used the Query or else none.
11. $_SERVER[‘HTTP_ACCEPT’]
It gives the details of the acceptance of the HTTP request if it exists or else none.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['HTTP_ACCEPT'];
?>
</body>
</html>
Output:
The accept request if exists else none.
12. $_SERVER[‘HTTP_HOST’]
It gives the name of the host where the server is hosted.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['HTTP_HOST'];?>
</body>
</html>
Output:
The host Name.
13. $_SERVER[‘HTTP_REFERER’]
This returns the complete URL of the current page.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['HTTP_REFERER'];
?>
</body>
</html>
Output:
https://www.google.com
14. $_SERVER[‘REMOTE_HOST’]
It gives the host name from where the user is viewing the page.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo $_SERVER['REMOTE_HOST'];
?>
</body>
</html>
Output:
The host name from where the page is accessed.
$_SERVER['SERVER_PORT'],$_SERVER['SERVER_SIGNATURE'],
$_SERVER['PATH_TRANSLATED'],
$_SERVER['SCRIPT_NAME'],
$_SERVER['SCRIPT_URI']
Note: Some of the screenshots didn’t have the output, as the output depends on the configuration we have set for the particular PHP server.
Conclusion
From the above article, we saw the use of Function $Server in PHP. We also saw the internal working and the advantages of having the type of data which we are defining for various programming purposes. Also, the syntax and examples helped us to understand much precisely the function.
Recommended Articles
This is a guide to PHP $_SERVER. Here we also discuss the introduction and working of the $_ server function along with different examples and its code implementation. You may also have a look at the following articles to learn more –