Total results: 11

Find current URL using PHP

If you've ever wanted to find the complete current URL of the page you are on using PHP, it's easy using PHP's built in $_SERVER global variables.

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

To break it down, we start with a string writing the "http://".

We then use the period (.) character to concatenate on the server name. $_SERVER['HTTP_HOST'] returns the name of the server. e.g. 'localhost' or 'www.blakes-site.co.uk'.

Finally we concatenate on $_SERVER['REQUEST_URI'] using the period character again. Request URI returns the current page you are accessing on the server e.g. '/blog/view.php'

To show a URL without the 'http://', for simpler presentation simply don't add this string at the start.

$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

© Blake Simpson, 2012 – 2018