get url of a site

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ranjitbd
Forum Newbie
Posts: 24
Joined: Sun May 03, 2009 1:59 pm

get url of a site

Post by ranjitbd »

Code: Select all

 
 
how to get the full url string from a address bar of relative site ? not only the domain name.
 
 
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: get url of a site

Post by markusn00b »

How about this:

Code: Select all

echo "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: get url of a site

Post by requinix »

It can get more complicated than that.

Code: Select all

function getURL() {
    $protocol = (empty($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] == "off" ? "http" : "https");
    $server = (empty($_SERVER["HTTP_HOST"]) ? $_SERVER["SERVER_NAME"] : $_SERVER["HTTP_HOST"]);
    $port = ($_SERVER["SERVER_PORT"] == 80 && $protocol == "http" || $_SERVER["SERVER_PORT"] == 443 && $protocol == "https" ? "" : ":" . $_SERVER["SERVER_PORT"]);
    return $protocol . "://" . $server . $port . $_SERVER["REQUEST_URI"];
}
Post Reply