Whether you are new to PHP or have been working with it for a while, you have probably heard of the $_SERVER variable. PHP automatically stores some important information in the $_SERVER super global array that can be used in your scripts or to help you troubleshoot problems with your requests. If you are used to previous versions of PHP, the $_SERVER has taken the place of the $HTTP_SERVER_VARS array. In this article I will show you some of the useful attributes of the $_SERVER array so you can begin using them in your scripts.
This useful tool allows you to get inside of your server environment and see what scripts are running, what kind of requests are being made and the details of each transaction.This information is stored in the $_SERVER array, which contains information about paths, headers and script locations. It also holds information about your user that can be helpful or useful information. The web server manages this array and creates the necessary entries.These can be used like a variable and printed in an echo statement for you to display on the page.
Here is a list of the attributes and what each one does:
- $_SERVER[‘PHP_SELF’] – This attribute returns the filename of the currently executing script, relative to the document root. It is supported on all platforms and can include path information if the PHP file is present in the path. The URL and any parameters are discarded, for example: if I am accessing http://www.mysite.com/index.php then $_SERVER[‘PHP_SELF’] would return /index.php
$_SERVER[‘SERVER_ADDR’] -Returns the IP address of the server that is currently executing the script. So if you wanted to print the IP address of the server you could use this snippet:
<?php echo $_SERVER['SERVER_ADDR']; ?>
$_SERVER[‘REMOTE_ADDR’] – Like the above that returns the server IP address, this returns the client IP address, or the address of the server viewing the script. Often this attribute is used to block certain IP addresses from your pages or if you want to serve content to different countries dynamically. Unfortunately this will not work if your client is behind a proxy. You would need to use $_SERVER[‘HTTP_X_FORWARDED_FOR’] instead.
<?phpecho $_SERVER['REMOTE_ADDR']; ?>
- $_SERVER[‘REQUEST_URI’] – If you need to view the URI of the HTTP request (such as the GET or POST status), you can use this attribute. This will also keep any URL parameter strings, unlike the PHP_SELF noted above. For example: if you are browsing to http://www.mysite.com/profile.php?id=45 then REQUEST_URI would return profile.php?id=45. Note that this does not work on IIS as it is not supported.
$_SERVER[‘HTTP_REFERER’] – This attribute will tell you where your site visitors are coming from and how they got there. This information is usually used for advertising or SEO purposes so you can see what sites are sending you traffic and if they are getting there by searching or by clicking a link.
<?phpecho $_SERVER['HTTP_REFERER']; ?>
$_SERVER[‘HTTP_USER_AGENT’] -The user agent is the browser or client the site visitor is using to get to your page. This can be helpful if you are collecting information about which browsers your users prefer or if your code is browser specific.
<?phpecho $_SERVER['HTTP_USER_AGENT']; ?>
$_SERVER[‘DOCUMENT_ROOT’] – This attribute will return the root directory file path of your server. This can be used a variable in relative paths when scripting so that if your file structure changes you will not have to go back and change your script.
<?php echo $_SERVER['DOCUMENT_ROOT']; ?>
$_SERVER[‘HTTP_HOST’] – This will return the host name that is found in the HTTP header. For example: if you request http://www.mysite.com/page.php then HTTP_HOST would return www.mysite.com.
<?php echo $_SERVER['HTTP_HOST']; ?>
- $_SERVER[‘QUERY_STRING’] – This attribute gives you all of the query string information of the URL, or all of the information after the “?”. This is especially useful when you need to parse out this incoming information or example the query string for troubleshooting purposes if you are having trouble with query string variables in your script. For example: if you request http://www.mysite.com/somepage.php?id=45&value=”yes” then QUERY_STRING will return id=45&value=”yes”.
$_SERVER[‘SCRIPT_FILENAME’] -This returns the absolute path of the script that is executing. Very helpful if you are troubleshooting scripting problems or aren’t sure which script is being called.
<?php echo $_SERVER['SCRIPT_FILENAME']; ?>
$_SERVER[‘REQUEST_TIME’] – This will return the server timestamp for the beginning of the request. Another helpful troubleshooting variable.
<?php echo $_SERVER['REQUEST_TIME']; ?>
There are many more attributes of $_SERVER but this is some of the handy ones to keep around. Some of these system variables can be used to help with authentication, administration, languages and connections. You can begin using the $_SERVER array to help you with your pages or as variables in your scripts to help you become a better PHP developer.
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|Apr 20-21||Web Development with PHP/MySQL|
|- Classroom - Online|