Very often we need to have the ful URL of the page and use it somehow in our code, for example a dynamic script with social bookmarks or anything else we can think of. This can be done using Javascript but I prefer to use PHP since it's not based on the client side and it works with or without Javascript turned on in our browsers.
We will define a function and call it anytime and anywhere we like. It will act as supposed and return a value that's based on the actual browsed page. You can define this function in any page. It will act, based on the page from where it's called. I like to use a functions.php file where I store my functions. It's just an example anyways.
function selfURL() {
$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)); }
Using the following line you can print your full URL as I said, anytime and anywhere you like:
print(selfURL());
Thank you for reading this,
Mihalcea Romeo
2007-05-26 | 02:04 pm
2007-05-17 | 07:40 pm