Get Full Current URL from Address Bar

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.

1 thought on “Get Full Current URL from Address Bar”

Leave a Comment

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