Practical Web Programming

Thursday, November 01, 2007

How To Get the Address Bar URL in PHP

If you want to get the full URL in your browser's address bar, this PHP function can help you. One practical use is when you want to add a social bookmark section in you site so that you can automatically submit the current webpage.

In Blogger who don't have the same problem because you have the
<$BlogItemPermalinkURL$> tag that returns the post's URL.

Here's the PHP sourcecodes:

function getOwnURL() 
{
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}

function strleft($s1, $s2)
{
return substr($s1, 0, strpos($s1, $s2));
}

To use, just invoke the function within the PHP print method like this:

print(getOwnURL()); 

0 comments:

Recent Post