Category Archives: PHP Interview Question

how to get the server details in php

$_SERVER Data

$_SERVER is a super global array that containing information such as headers, paths, and script locations etc.
php server details
By using the $_SERVER  array you can get many server information like Server IP, Document root, Server name, server software and more.  The sever super global array are created by the server varies from server to server.

 

Here is all server element


 HTTP_HOST
 HTTP_CONNECTION
 HTTP_CACHE_CONTROL
 HTTP_ACCEPT
 HTTP_UPGRADE_INSECURE_REQUESTS
 HTTP_USER_AGENT
 HTTP_DNT
 HTTP_REFERER
 HTTP_ACCEPT_ENCODING
 HTTP_ACCEPT_LANGUAGE
 HTTP_COOKIE
 PATH
 SystemRoot
 COMSPEC
 PATHEXT
 WINDIR
 SERVER_SIGNATURE
 SERVER_SOFTWARE
 SERVER_NAME
 SERVER_ADDR
 SERVER_PORT
 REMOTE_ADDR
 DOCUMENT_ROOT
 REQUEST_SCHEME
 CONTEXT_PREFIX
 CONTEXT_DOCUMENT_ROOT
 SERVER_ADMIN
 SCRIPT_FILENAME
 REMOTE_PORT
 GATEWAY_INTERFACE
 SERVER_PROTOCOL
 REQUEST_METHOD
 QUERY_STRING
 REQUEST_URI
 SCRIPT_NAME
 PHP_SELF
 REQUEST_TIME_FLOAT
 REQUEST_TIME

 

Some important server elements


<?php
echo "Host: " . $_SERVER["HTTP_HOST"] . "<BR>";
echo "Browser: " . $_SERVER["HTTP_USER_AGENT"] . "<BR>";
echo "User's IP address: " . $_SERVER["REMOTE_ADDR"]."
";
echo "User's Software: " . $_SERVER["SERVER_SOFTWARE"]."
";
echo "User's name: " . $_SERVER["SERVER_NAME"]."
";
?>

 

Output


Host: localhost
Browser: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
User's IP address: ::1
User's Software: Apache/2.4.4 (Win64) PHP/5.4.12
User's name: localhost

 

Print complete server elements


<?php
echo "Complete Server Details";
foreach($_SERVER as $key => $value){
echo $key."=====".$value."</br>";
}
?>