How to display web servers IP address (and other info) in a web page
This simple article will explain how you can display some commonly used information directly into a web page using the very popular PHP programming language.In order to run PHP scripts on your web site you must have the appropriate software installed and configured. Most hosting accounts will already have this available.
If the below examples do not work then you will need to install PHP support on your server or ask your hosting provider to do so.
When a web server serves data to a client there is a multitude of information available both about the server itself as well as the current client accessing the server.
This information is stored in PHP’s predefined variables.
In order for your server to recognize the PHP code in your web page, it must be enclosed within the proper opening and closing tags.
<?php …your code … ?>
For example to display the IP address of the web server directly onto your web page you can use the following code.
<?php echo $_SERVER[’SERVER_ADDR’]; ?>
‘$_SERVER’ is the name of PHP’s array that contains information about the web server.
‘SERVER_ADDR’ is the element in that array that holds the actual data.
Following are examples of how to display some of the most common variables.
- Server information -
The IP address of the web server
- $_SERVER[‘SERVER_ADDR’]
The timestamp of the start of the request. Available since PHP 5.1.0.
- $_SERVER[‘REQUEST_TIME’]
The query string, if any, via which the page was accessed.
- $_SERVER[‘QUERY_STRING’]
The document root directory under which the current script is executing, as defined in the server’s configuration file.
- $_SERVER[‘DOCUMENT_ROOT’]
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
- $_SERVER[‘HTTP_REFERER’]
Contents of the User-Agent (browser): header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). Among other things, you can use this value with get_browser() to tailor your page’s output to the capabilities of the user agent.
- $_SERVER[‘HTTP_USER_AGENT’]
The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host.
- $_SERVER[‘SERVER_ADMIN’]
The port on the server machine being used by the web server for communication. For default setups, this will be ‘80′; using SSL, for instance, will change this to whatever your defined secure HTTP port is.
- $_SERVER[‘SERVER_PORT’]
String containing the server version and virtual host name which are added to server-generated pages, if enabled.
- $_SERVER[‘SERVER_SIGNATURE’]
The absolute pathname of the currently executing script.
- $_SERVER[‘SCRIPT_FILENAME’]
- Client information-
The IP address from which the user is viewing the current page.
- $_SERVER[‘REMOTE_ADDR’]
The port being used on the user’s machine to communicate with the web server.
- $_SERVER[‘REMOTE_PORT’]
The URI which was given in order to access this page; for instance, ‘/index.html’.
- $_SERVER[‘REQUEST_URI’]
For more information and a more in depth explanation about all available variables and options see http://www.php.net
-------------------------
Digg it
del.icio.us
Technorati?
March 1st, 2009 at 2:53 pm
I wanted to comment and thank the author, good stuff
October 1st, 2009 at 9:24 pm
Its a good explanation of $_SERVER. Thanks for explaining it.