Knowpapa.com - a developer's blog

Get The Current Page URL with PHP $_SERVER suberglobal

PHP programmers often need to call the URL for the current page. This is most often used in processes like “share this” or “email this” or “link to this” page.

PHP comes with an in-built super global variable called $_SERVER which makes it very easy to retrieve the current page URL. You can get every piece of information about the current URL using the $_SERVER suberglobal array.

Before we try to fetch the URL lets see how a normal URL structure looks like:

http://www.abc.com/dir1/script1.php?art=kungfoo&name=jackiechan

Any typical URL like this can be broken into several common parts like:

  • http: The url protocol
  • www.abc.com – The domain name or the hostname.
  • dir1: The directory within the root
  • script1.php – The actual php script
  • art=kungfoo – the first URL parameter (art) and it’s value (kungfoo)
  • name=jackiechan – the second URL parameter (name) and it’s value (jackiechan)

In PHP, the $_SERVER suberglobal variable allows easy methods to retrieve all these information about the current URI. The code is as follows:

PHP code Output Remark
echo $_SERVER[‘SERVER_PROTOCOL’]; HTTP/1.1 the url protocol
echo $_SERVER[‘HTTP_HOST’]; www.demo.com the domain name
echo $_SERVER[‘SCRIPT_NAME’]; script1.php the actual php script
echo $_SERVER[‘QUERY_STRING’]; art=kungfoo&name=jackiechan all the URL parameters

Now its time to trim and consolidate all these parts to get the final URL. The code part:

    $protocol = $_SERVER['SERVER_PROTOCOL']
    $domain     = $_SERVER['HTTP_HOST'];
    $script   = $_SERVER['SCRIPT_NAME'];
    $parameters   = $_SERVER['QUERY_STRING'];
//next we do a bit of string manipulation to convert output like HTTP/1.1 to http
    $protocol=strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
                === FALSE ? 'http' : 'https';

//now the final part to concatenate all this together to form the URL
    $FinalUrl = $protocol . '://' . $domain. $script . '?' . $parameters;

     echo $FinalUrl;

This example is is just an indication of the things that can be achieved with $_SERVER suberglobal array.
Some other $_SERVER elements that are very commonly used include:

  • $_SERVER[‘SERVER_ADDR’]: IP address of the server
  • $_SERVER[‘REQUEST_METHOD’]: Returns the page access method used i.e. ‘GET’, ‘HEAD’, ‘POST’, ‘PUT’.
  • $_SERVER[‘REQUEST_TIME’]: timestamp of the start of the request.
  • $_SERVER[‘HTTP_REFERER’]: returns the referrer page uri – used in redirecting to last page after login
  • $_SERVER[‘SCRIPT_FILENAME’]: returns the path including the filename, like __DIR__
  • $_SERVER[‘HTTP_COOKIE’]. returns the raw value of the ‘Cookie’ header sent by the user agent.
  • $_SERVER[‘HTTP_ACCEPT_LANGUAGE’]): returns the default set language – useful for websites with multilingual content & readers
  • $_SERVER[‘HTTP_USER_AGENT’]: returns the kind of device being used to access (desktop/mobile device etc) – suitable for switching interface for different devices.