get url in php from address bar – How to get the URL shown at address bar?

get url in php from address bar using a combination of $_SERVER[“HTTP_HOST”] and $_SERVER[“REQUEST_URI”] Example.

get url in php from address bar

i need to use predefined variable $_SERVER. It is an array containing details such as headers, paths, as well as script locations.

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’.

Get Full Current URL from Address Bar

Get Full Current URL from Address Bar using PHP


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

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

Get Full Current URL from Address Bar using JavaScript

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

Get URL String Parameter with PHP

rank=125&type=new. You can get the value of “rank” and “type” parameters as below:



Don’t Miss : Get The Full URL In PHP

standard function

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

In PHP, you can retrieve the complete URL from the address bar using the $_SERVER[‘REQUEST_URI’] superglobal variable. Here is an example of how to use it:

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

In this example, we first concatenate the “http://” protocol with the $_SERVER[‘HTTP_HOST’] server name, and then append the $_SERVER[‘REQUEST_URI’] path and query string to get the complete URL. The $_SERVER[‘HTTP_HOST’] variable contains the domain name or IP address of the server, and the $_SERVER[‘REQUEST_URI’] variable contains the path and query string of the current request.

Note that this method only works if the request was made using the HTTP or HTTPS protocol. If the request was made using a different protocol, such as FTP or SSH, the $_SERVER[‘HTTP_HOST’] variable may not be set, and you may need to use a different method to retrieve the complete URL.

I hope you get an idea about get url in php from address bar.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment