Get full URL from address bar in PHP



InĀ  this tutorial I will show you how to get full URL from address bar in PHP.

To get URL in PHP, we need to useĀ predefined variable $_SERVER. $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.

$fullurl = 'http'.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

for example browser address bar showing following URL : www.domain.com/product.php?var=1&var=2

$_SERVER['HTTP_HOST']

will return www.domain.com

and

$_SERVER['REQUEST_URI']

will return /product.php?var=1&var=2

Now $fullurl will return full URL from address bar.

If your website is running with https, you can use $_SERVER[‘HTTPS’] or $_SERVER[‘SERVER_PORT’] to check https in URL exists or not.

Hope this tutorial will help you to get full URL from address bar in PHP.