PHP: Get Current Page URL


In this tutorial, we’ll learn how to get current web page url from your web browser address bar using PHP script
Syntax

<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?>

We use variable $_SERVER in php to get full url from address bar:
$_SERVER[‘HTTP_HOST’] – This variable will show only server name.
$_SERVER[‘REQUEST_URI’] – This variable will show you the path to file of your url.

DEMO: http://demo.tutorialspots.com/getCurrent.php

function getUrl() {
  $url  = @( $_SERVER["HTTPS"] != 'ON' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
  $url .= ( $_SERVER["SERVER_PORT"] != 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
  $url .= $_SERVER["REQUEST_URI"];
  return $url;
}

Leave a Reply