How to Get the Current Page URL

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:

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:
print $currenturl = selfURL();


17 thoughts on “How to Get the Current Page URL

  1. 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!


  2. 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.


  3. Pingback: 2010 in review « The Dreamer!

  4. hi!,I like your writing very much! share we communicate extra approximately your article on AOL?
    I need a specialist in this house to resolve my problem.
    May be that’s you! Taking a look ahead to peer you.

  5. Thanks , I have just been searching for information approximately this topic for a while and yours is the best I’ve came upon till now. However, what about the conclusion? Are you sure about the supply?

  6. Sneakers are good as long as you are running along the ground, but are really not meant to be used when you do this kind of work.
    Note down your requirements and these notes should include details of what tasks
    you are planning to do with a chainsaw. The best advice with regard
    to replacing your chainsaw parts would be to find a dealer who gives you
    good quality service backup and can get you the necessary spare parts in
    good time.

  7. Magnificent goods from you, man. I have understand your stuff previous to and you are just extremely wonderful.
    I actually like what you have acquired here, really
    like what you’re stating and the way in which you say it.

    You make it enjoyable and you still take care of to keep
    it wise. I cant wait to read far more from you. This is really a wonderful website.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s