Search A2Z 24

How to get Current Page URL using PHP

PHP programmers/developers often need to call the URL for the current page (current webpage url). 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. PHP Server variable is quiet handy for such scenarios as it provide all the parameters passed through the server. We can get every piece of information about the current URL using the $_SERVER superglobal array.
Below we are implemented different type of functions & codes to retrive current page url.

1.Function to get current url:

function getURLThoughBasicFunc(){
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on"){
		$pageURL .= "s";
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80"){		
		$pageURL .= ":".$_SERVER["SERVER_PORT"];		
	return $pageURL;

You can now get the current page URL using echo/var_dump or can consume the same directly by assigning to a variable:

  echo getURLThoughBasicFunc();

2.Get short version of url:

Sometime in current page(webpage) we need to add output link as current url, that time we can use short version of url concept as mention below :

      $link =  "//$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
	  $escaped_link = htmlspecialchars($link, ENT_QUOTES, 'UTF-8');
	  echo '<a href="'.$escaped_link.'">'.$escaped_link.'</a>';

Here are some more details about the issues and edge cases of the Short version format //satz24.com/.

3.Get Full Version of url:

To get full version of url using PHP,In this function we are include many $_SERVER variables to get correct url value & we also using ternary statementfor keeping our function code minimal:

     function getCurrentWebpageFullURL($use_forwarded_host=false){
          $ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? true:false;
          $sp = strtolower($_SERVER['SERVER_PROTOCOL']);
          $protocol = substr($sp, 0, strpos($sp, '/')) ;
          $protocol .=($ssl) ? 's' : '';

          $port = $_SERVER['SERVER_PORT'];
          $port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
          $host =isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
          $host = ($use_forwarded_host && isset($_SERVER['HTTP_X_FORWARDED_HOST'])) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $host;
          $host = isset($host) ? $host : $_SERVER['SERVER_NAME'] . $port;

          $pageURL = $protocol . '://' . $host;
          return $pageURL . $_SERVER['REQUEST_URI'];
     echo getCurrentWebpageFullURL();

URL structure:

Result will come in format: scheme://domain:port/path?query_string

  • If URL is scheme://username:password@domain:port/path?query_string#fragment_id
    This function does not include username:password from a full URL or the fragment (hash).
  • It will not show the default port 80 for HTTP and port 443 for HTTPS, section 14.23 of the HTTP spec which specifies that the Host header must contain the port number unless it is the default port number.
  • The #fragment_id is not sent to the server by the client (browser) and will not be added to the full URL.
  • $_GET will only contain param=value1 for an URL like /post?param=value1¶m=value2.
  • Some CMS's and environments will rewrite $_SERVER['REQUEST_URI'] and return /post?param=value2 for an URL like /post?param=value1¶m=value2, use $_SERVER['QUERY_STRING'] in this case.
  • Keep in mind that an URI = URL + URN, but due to popular use, URL now means both URI and URL.
  • In this function we are use HTTP_X_FORWARDED_HOST, It will used when we plan to use proxies or balancers that time we need to pass a argument in function having value (true or 1). Otherwise you should remove HTTP_X_FORWARDED_HOST.

Client (Browser) controlled variables:

  • $_SERVER['REQUEST_URI'] Any unsupported characters are encoded by the browser before they are sent.
  • $_SERVER['HTTP_HOST'] and is not always available according to comments in the PHP manual: reserved variables
  • $_SERVER['HTTP_X_FORWARDED_HOST'] gets set by balancers and is not mentioned in the list of $_SERVER variables in the PHP manual.

Server controlled variables:

  • $_SERVER['HTTPS'] The client chooses to use this, but the server returns the actual value of either "off" or "on".
  • $_SERVER['SERVER_PORT'] The server only accepts allowed numbers as ports.
  • $_SERVER['SERVER_PROTOCOL'] The server only accepts certain protocols.
  • $_SERVER['SERVER_NAME'] It is set manually in the server configuration and is not available for IPv6

For understanding purpose the main differences between HTTP_HOST and SERVER_NAME

  • If the server runs on a port other than 80 (as might be common on a development/intranet machine) then HTTP_HOST contains the port, while SERVER_NAME does not.HTTP_HOST does not contain port 443 when running on HTTPS (unless you're running on a non-standard port)

    $_SERVER['HTTP_HOST'] == 'localhost:8080'
    $_SERVER['SERVER_NAME'] == 'localhost'
  • HTTP_HOST is the target host sent by the client. It can be manipulated freely by the user. It's no problem to send a request to your site asking for a HTTP_HOST value of www.satz24.com.
    SERVER_NAME comes from the server's VirtualHost definition and is therefore considered more reliable. It can, however, also be manipulated from outside under certain conditions related to how your web server is set up.
  • If you want to use IPv6, you probably want to use HTTP_HOST rather than SERVER_NAME.

More details about PHP server variables can be find at php reserved variables.
To check out the current values of Server variables, var_dump ($SERVER); can be a handy trick as well.

About Author

by Admin

Share your thoughts!

Login as a member to access comment posting block !! click-here

Thoughts From Other Users (0)

No Comments

Reveal Modal Goodness

This is a default modal in all its glory, but any of the styles here can easily be changed in the CSS.

This is just a simple modal with the default styles, but any type of content can live in here. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi quis sem vel enim eleifend tristique. Etiam tincidunt faucibus pharetra.