WP-Mix

A fresh mix of code snippets and tutorials

PHP: Get $_SERVER Variables

I use a variation of this function to get super global $_SERVER variables in my free shapeSpace WordPress theme. The function returns all relevant variables contained in the $_SERVER array. Note this is a general PHP technique, so WordPress is not required for this one.

Get $_SERVER Variables

To get most (not all) of the most useful $_SERVER variables, add the following two functions. The first function gets the variables, the second function sanitizes them. Also, make sure to read the Notes below for important information about this technique.

function shapeSpace_server_vars() {
	
	if (!isset($_SERVER)) return;
	
	$postvars = file_get_contents('php://input');
	
	$port      = isset($_SERVER['REMOTE_PORT'])       ? $_SERVER['REMOTE_PORT']       : 'undefined';
	$method    = isset($_SERVER['REQUEST_METHOD'])    ? $_SERVER['REQUEST_METHOD']    : 'undefined';
	$protocol  = isset($_SERVER['SERVER_PROTOCOL'])   ? $_SERVER['SERVER_PROTOCOL']   : 'undefined';
	$connect   = isset($_SERVER['HTTP_CONNECTION'])   ? $_SERVER['HTTP_CONNECTION']   : 'undefined';
	$gateway   = isset($_SERVER['GATEWAY_INTERFACE']) ? $_SERVER['GATEWAY_INTERFACE'] : 'undefined';
	$referer   = isset($_SERVER['HTTP_REFERER'])      ? $_SERVER['HTTP_REFERER']      : 'undefined';
	$server    = isset($_SERVER['SERVER_NAME'])       ? $_SERVER['SERVER_NAME']       : 'undefined';
	$http_host = isset($_SERVER['HTTP_HOST'])         ? $_SERVER['HTTP_HOST']         : 'undefined';
	$request   = isset($_SERVER['REQUEST_URI'])       ? $_SERVER['REQUEST_URI']       : 'undefined';
	$query     = isset($_SERVER['QUERY_STRING'])      ? $_SERVER['QUERY_STRING']      : 'undefined';
	$agent     = isset($_SERVER['HTTP_USER_AGENT'])   ? $_SERVER['HTTP_USER_AGENT']   : 'undefined';
	
	$ip_remote = isset($_SERVER['REMOTE_ADDR'])          ? $_SERVER['REMOTE_ADDR']          : 'undefined';
	$ip_client = isset($_SERVER['HTTP_CLIENT_IP'])       ? $_SERVER['HTTP_CLIENT_IP']       : 'undefined';
	$ip_forwrd = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : 'undefined';
	
	// $host_remote = gethostbyaddr($ip_remote);
	// $host_client = gethostbyaddr($ip_client);
	// $host_forwrd = gethostbyaddr($ip_forwrd);
	
	$vars = array(
		
		'PORT'         => $port, 
		'METHOD'       => $method, 
		'PROTOCOL'     => $protocol, 
		'CONNECT'      => $connect, 
		'GATEWAY'      => $gateway, 
		'REFERRER'     => $referer, 
		'SERVER'       => $server, 
		'HTTP HOST'    => $http_host, 
		'REQUEST'      => $request, 
		'QUERY'        => $query, 
		'USER AGENT'   => $agent, 
		'POST VARS'    => $postvars, 
		'IP REMOTE'    => $ip_remote, 
		'IP CLIENT'    => $ip_client, 
		'IP FORWARD'   => $ip_forwrd, 
		// 'HOST REMOTE'  => $host_remote, 
		// 'HOST CLIENT'  => $host_client, 
		// 'HOST FORWARD' => $host_forwrd,
		
	);
	
	foreach ($vars as $key => $val) {
		
		$vars[$key] = shapeSpace_clean($val);
		
	}
		
	return $vars;
	
}

This simple function returns $vars, which contains and array of sanitized values for each specified server variable. Here we are using the following function to handle the sanitization of each variable:

function shapeSpace_clean($string) {
	
	$string = trim($string); 
	$string = strip_tags($string);
	$string = stripslashes($string);
	$string = str_replace("\n", '', $string);
	$string = htmlentities($string, ENT_QUOTES);
	$string = trim($string); 
	
	return $string;
	
}

This function provides an effective way to sanitize everything, so each returned array value is safe. And of course you are welcome to modify or replace things as needed to suit your specific application.

Example usage

To get an idea of how the above functions may be used. In my free DIY Server Uptime Monitor, I use the technique to include server information in email notifications. This is done via the following function:

function shapeSpace_check_status() {
	
	$subject = 'DIY Server Uptime Monitor';
	
	$email = 'hello@example.com';
	
	$date = date('F jS Y, h:ia', time());
	
	$vars = shapeSpace_server_vars(); // <-- get the server variables
	
	$message = $date . "\n\n";
	
	foreach ($vars as $key => $val) {
		
		$message .= $key .': '. $val . "\n"; // <-- looping thru variables
		
	}
	
	mail($email, $subject, $message, 'From: '. $email);
	
}

Basically this function calls shapeSpace_server_vars(), loops through the array of server variables, and sends them via email. You can check out the uptime monitor tutorial to get more context and details.

Notes

Some important things to keep in mind when using the above functions.

1) The first function, shapeSpace_server_vars(), returns many but not all $_SERVER variables. For a complete list of all possible variables, check out the PHP documentation. And for a direct look at the contents of $_SERVER, do a quick variable dump like var_dump($_SERVER).

2) In the first function, shapeSpace_server_vars(), notice that the following lines are commented out, effectively disabled.

	// $host_remote = gethostbyaddr($ip_remote);
	// $host_client = gethostbyaddr($ip_client);
	// $host_forwrd = gethostbyaddr($ip_forwrd);
		.
		.
		.
		// 'HOST REMOTE'  => $host_remote, 
		// 'HOST CLIENT'  => $host_client, 
		// 'HOST FORWARD' => $host_forwrd,

If you decide to uncomment/enable these lines, the returned array of server variables will include additional information. Specifically, for each type of IP address, the script uses gethostbyaddr() to look up the associated host name. While this information may be useful, understand that calling gethostbyaddr() is rather expensive resource-wise. So probably don’t want to enable these extra variables unless absolutely necessary.

★ Pro Tip:

USP ProSAC Pro