Mobile Number Tracker
Trace Mobile Number Earn Money Online
© TechWelkin.com

Get Full Current URL from Address Bar

JavaScript is beautiful!
Samyak Lalit | September 28, 2011 (Last update: November 7, 2019)

Samyak Lalit is an Indian author and disability rights activist. He is the principal author and founder of projects like TechWelkin, WeCapable, Viklangta, Kavita Kosh among many others.

Yesterday, I needed to retrieve the URL of a webpage as shown in the address bar of browser. I was coding in PHP –so my first choice was to use PHP to get the URL. But it is not as straightforward as it seems. I wonder why PHP has not so far included an easy way to accomplish this task which programmers have to perform quite often. Nevertheless, let’s see how you can get current URL from address bar using PHP and JavaScript.

Get Full Current URL from Address Bar using PHP

In order to use PHP to get the full current URL you can use the following code snippet:

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

Get Full Current URL from Address Bar using JavaScript

JavaScript is beautiful!

JavaScript is beautiful!

If you need to get the full URL of the current webpage on client-side in some JavaScript function, you can use the location.href to perform this:

var currentURL=location.href;
document.write(currentURL);

Get URL String Parameter with PHP

If you want value of a particular parameter passed along with the URL string –you can use the $_GET array of PHP for this purpose. For example, if the page URL is like:

day=wednesday&month=8

You can get the value of “day” and “month” parameters as below:

<!?php echo $_GET["day"]; ?>
<!?php echo $_GET["month"]; ?>

I hope this was useful for you. Please feel free to let me know if you have any questions on this topic. I will be happy to try and help you. Thank you for using TechWelkin.

© TechWelkin.com

2 responses to “Get Full Current URL from Address Bar”

  1. Steve says:

    I dont believe this gets the url from (or of) the address bar.
    I used an iframe to test this. It returns the address of the iframe and not the content of the address bar in the browser.
    As php is server side I’m not sure its that straightforward to do.
    Javascript may be the better way as irrespective of what’s gone on at the server – the address bar will always be client side.

  2. arash says:

    Thank you very much for helping :)

Leave a Reply

Your email address will not be published. Required fields are marked *