-1

Possible Duplicate:
PHP Get the Full URL
How do I capture the whole URL using PHP?

I would like to the whole of the URL including the _GET variable names and values, for example www.mywebsite.com/store.php?department=MENS

The code I have used below only gives me the URL without the _GET variable part.

$url = $_SERVER['SERVER_NAME']; 
$page = $_SERVER['PHP_SELF'];
$page = $_POST['url'];
echo "http://".$url.$page; 

All I would like is to be able to copy that URL exactly how it is.

Community
  • 1
  • 1
A Star
  • 605
  • 9
  • 18
  • Repost of [How do I capture the whole URL using PHP?](http://stackoverflow.com/questions/9355305/how-do-i-capture-the-whole-url-using-php) – deceze Feb 20 '12 at 02:14

2 Answers2

2
function currenturl() {
$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;
}

simply do $url=currenturl(); that's it

  • Upvote. Good call on the port, as well. Inline if statements could help a lot in making this cleaner, by the way - for example `$pageurl = ($_SERVER["HTTPS"] == "on") ? 'https' : 'http';` – Kavi Siegel Feb 20 '12 at 02:16
  • Yup, it looks nicer that way. I actually use inline ifs wherever it's human as I have thing against loads of code. But I edited a bit just to make it easier to read –  Feb 20 '12 at 03:40
0
$url = (!empty($_SERVER['HTTPS']))
    ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] 
    : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
Teej
  • 12,764
  • 9
  • 72
  • 93