06/21/2011

PHP’s Global Server Variables

by Cyle
Categories: php
Tags: , , , ,
Comments: 1 Comment

Most all web servers pass an array containing a lot of helpful information such as headers, host information, and more. There are many elements in the array, but I am only going to discuss the ones I use most. The $_SERVER array must be called inside the script and not on a command line. Let’s start with an example:

1
2
3
<?php
echo $_SERVER['SERVER_NAME'];
?>

If the above example was run on this blog, it would output:

njarb.com

Below are the elements I use most in $_SERVER array:

‘SERVER_ADDR’: The IP address of the server that is currently running the script (usually the web host server).

‘SERVER_NAME’: The name of the server that is currently running the script.

‘REQUEST_METHOD’: The method used to access this page. (‘GET’, ‘POST’, ‘PUT’, ‘HEAD’)

‘REQUEST_TIME’: The time of the start of the request. (Available in PHP 5+)

‘DOCUMENT_ROOT’: The document root directory set by the server’s configuration file which the current script is running.

‘HTTP_REFERER’: The address of the page that referred him/her to your webpage. This can be modified by most browsers, so it should not be trusted, but is good information.

‘HTTP_USER_AGENT’: The browser information of the user (Example: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586))

‘HTTPS’: This element is set to a non-empty value if the script was queried through the HTTPS protocol.

‘REMOTE_ADDR’: The IP address of the user viewing the page.

‘REMOTE_HOST’: The host name of the user viewing this page.

‘REQUEST_URI’: The original URL that was requested before any redirects sent the user to the final landing page.

‘REDIRECT_STATUS’: The redirect status (200 = OK, 404 = not found, 403 = forbidden, etc.)

Example:

Below is an example using all the mentioned elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
echo '<b>SERVER_ADDR</b>: '.$_SERVER['SERVER_ADDR'];
echo '<br /><br /><b>SERVER_NAME</b>: '.$_SERVER['SERVER_NAME'];
echo '<br /><b>REQUEST_METHOD</b>: '.$_SERVER['REQUEST_METHOD'];
echo '<br /><b>REQUEST_TIME</b>: '.$_SERVER['REQUEST_TIME'];
echo '<br /><b>DOCUMENT_ROOT</b>: '.$_SERVER['DOCUMENT_ROOT'];
echo '<br /><b>HTTP_REFERER</b>: '.$_SERVER['HTTP_REFERER'];
echo '<br /><b>HTTP_USER_AGENT</b>: '.$_SERVER['HTTP_USER_AGENT'];
echo '<br /><b>HTTPS</b>: '.$_SERVER['HTTPS'];
echo '<br /><b>REMOTE_ADDR</b>: '.$_SERVER['REMOTE_ADDR'];
echo '<br /><b>REMOTE_HOST</b>: '.$_SERVER['REMOTE_HOST'];
echo '<br /><b>REQUEST_URI</b>: '.$_SERVER['REQUEST_URI'];
echo '<br /><b>REDIRECT STATUS</b>: '.$_SERVER['REDIRECT_STATUS'];
?>

The output of the above code for this current session is:

SERVER_ADDR: 192.185.244.78
SERVER_NAME: njarb.com
REQUEST_METHOD: GET
REQUEST_TIME: 1492977511
DOCUMENT_ROOT: /home4/cconoly/public_html/njarb.com
HTTP_REFERER:
HTTP_USER_AGENT: CCBot/2.0 (http://commoncrawl.org/faq/)
HTTPS:
REMOTE_ADDR: 54.198.188.116
REMOTE_HOST:
REQUEST_URI: /2011/06/phps-global-server-variables/
REDIRECT STATUS: 200

All of the $_SERVER elements and more about each property can be found at php.net’s PHP $_SERVER – Manual


1 Comment »

  1. […] for Helping « PHP’s Global Server Variables Measuring Execution Time With PHP […]

Leave a Reply

Your email address will not be published. Required fields are marked *



user-avatar
Today is Sunday
04/23/2017