Skip to content

Get Full Current URL from Address Bar

Last updated on January 17, 2018

In this tutorial, you’ll learn how to get current web page URL from your web browser address bar using PHP script.

To get Current Page URL We can use $_SERVER Environment variables.

In PHP, We can not read URL path after hash(#) tag. Because browser does not send the hash data to the server. You can get #tag data only with JavaScript.

if the Website running on standard port (80,443) web site current URL will be,

if the website running under non-standard port, current URL will be,

$url="http://".$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
print_r($url);

i am gonna convert this script into standard function :

function current_url() {
    $current_url = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    $current_url .= $_SERVER["SERVER_NAME"];
    if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")  {
        $current_url .= ":".$_SERVER["SERVER_PORT"];
    }
    $current_url .= $_SERVER["REQUEST_URI"];
    return $current_url;
}

Using JavaScript : you can get current page URL from the address bar using below script


Useful Environment variables of URL:

$_SERVER[‘HTTP_HOST’] = Host name from the current request.
$_SERVER[‘HTTP’] = Set to a non-empty value if the protocol is HTTP
$_SERVER[‘HTTPS’] = Set to a non-empty value if the protocol is HTTPS
$_SERVER[‘SERVER_PORT’] = Server port. Default is: 80
$_SERVER[‘REQUEST_URI’] = The URI to access this page,For example, ‘/index.php’.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments