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’.
Read Also:  Bootstrap Toggle Inside JQuery DataTable

Get Full Current URL from Address Bar

Get Full Current URL from Address Bar using PHP

<?php
$get_live_active_path="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
print_r($get_live_active_path);
?>

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:

<!?php echo $_GET["rank"]; ?>
<!?php echo $_GET["type"]; ?>

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;
}

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.