Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. Another example maybe a page URL has Querystring info appended to it and you need to send an email off to someone with that same exact URL and Querystring information. There are plenty of other reasons as well. Here is how you can do that.
Add the following code to a page:
<?php
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) {
$values = substr($s1, 0, strpos($s1, $s2));
return $values;
}
?>
You can now get the current page URL using the line:
<?php
print $currenturl = selfURL();
ooopinionsss
How you think when the economic crisis will end? I wish to make statistics of independent opinions!
Neeti
Thanks, it really helped, works perfect.
GS
Good job, thanks for sharing this function. Much appreciated.
Free Antispyware Downloads
Great! I can use the script to get my existing URL.
Abid
Hi Rana
I really enjoyed your post and i will be sure to read through the rest of your blog… I really appreciate the way in which you look at this subject, looking at this in a new light as they say.. bookmarked!
Regards
-Abid
sammaye
Hi,
$_SERVER[‘SERVER_NAME’] will not work on multi server environments. Should avoid server_name header variable since most multi server environments use it to detect their individual servers for load balancing etc.
Thanks,
David
What? Why not: $_SERVER[‘REQUEST_URI’] ?
ankursharma7
I found the proper solution at http://www.7tech.co.in/php/get-url-of-current-page-php-wordpress/ 🙂