PHP – Get current URL with querystring values

function getCurrentURL() {
$protocol = “http”;
if($_SERVER[“SERVER_PORT”]==443 || (!empty($_SERVER[“HTTPS”]) && $_SERVER[“HTTPS”]==”on”)) {
$protocol .= “s”;
$protocol_port = $_SERVER[“SERVER_PORT”];
} else {
$protocol_port = 80;
}
$host = $_SERVER[“HTTP_HOST”];
$port = $_SERVER[“SERVER_PORT”];
$request_path = $_SERVER[“PHP_SELF”];
$querystr = $_SERVER[“QUERY_STRING”];
$url = $protocol.”://”.$host.(($port!=$protcol_port && strpos($host,”:”)==-1)?”:”.$port:””).$request_path.(empty($querystr)?””:”?”.$querystr);
return $url;
}

echo “Path: “.getCurrentURL();

About selvam4win

I am Selvam. I am very jolly person and frank one.

Posted on November 16, 2011, in PHP, Querystring and tagged , , , , . Bookmark the permalink. 2 Comments.

  1. Thanks Salvam, this is exactly what I was looking for. Well, almost🙂
    Can you tell me how to search for a specific string withing the html document? For example, I would like to use your code to search the html of the current page to find the string “

    <h2>Equivalent to only $2.92 monthly</h2>" so I can change it to <h2 class="featured">Equivalent to only $2.92 monthly</h2>

    I hope that makes sense. I’m a newbee🙂

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: