MisterWebNet

<<< Coding for the future >>>

Home Coding PHP Current page url
Current page url PDF Print E-mail
Tuesday, 24 November 2009 15:51

Getting the current page URL can be dependend on the server set up. The standard function (which may not always work! - read on if your stuck!) goes like this:

 

 

function curPageURL() {
print_r($_SERVER);
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

 

 

It looks pretty but does not always return the full URL especially if you have a lot of pass variables in the url.  The $_SERVER array contains a lot of useful information. You can see what it holds by printing it out:

 

print_r($_SERVER);

If you search for your string of variables in the printed out array, you'll probably find them under QUERY_STRING, i.e. $_SERVER['QUERY_STRING']

To get the full url then, you're pageurl in the function we begun with might look something like this:

$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];

 

Simply replace the $pageurl variables in the function above with this line! Note, I haven't allowed for https, or non-standard ports. You'll have to substitute in those into your pageurl variable from the function.

 

 


Last Updated on Tuesday, 24 November 2009 16:07
 

Add comment

We reserve the right to remove comments without notice. Users are responsible for their own comments and the contents contained within.


Security code
Refresh

ThugVillage.com
Mens Entertainment Magazine with girls, gadgets, indepth analysis, videos and much more
Best Flash Games
The best flash games on the net - 100% free!
HiredMedia
Off beat, deep-web, alternative videos