Page 1 of 1

getting current url

Posted: Fri Sep 09, 2005 4:53 pm
by psychotomus
how you get current URL?

Posted: Fri Sep 09, 2005 4:56 pm
by John Cartwright

Code: Select all

print_r($_SERVER);
will give you some useful tidbits :wink:

Posted: Fri Sep 09, 2005 4:56 pm
by feyd

Posted: Fri Sep 09, 2005 5:25 pm
by Burrito
that function wont' work correctly...it puts the port in the wrong location, assumes that $_SERVER['HTTPS'] can be empty, and assumes that $_SERVER['REQUEST_URI'] is valid.

try this:

*briefly tested*

Code: Select all

<?
function geturl() 
{ 
  $ports = array('https' => 443, 'http' => 80); 
  $prefix = (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off" ? 'http' : 'https'); 
  $url = $prefix; 
  $url .= '://'; 
  $url .= $_SERVER['HTTP_HOST']; 
  $url .= $_SERVER['SERVER_PORT'] != $ports[$prefix] ? ':' . $_SERVER['SERVER_PORT'] : '';
  $url .= $_SERVER['PHP_SELF']; 
  $url .= (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != "" ? "?".$_SERVER['QUERY_STRING'] : "");
  return $url; 
} 

echo geturl();
?>
edit: fixed the query string after testing on a https server.

Posted: Fri Jan 27, 2006 4:02 am
by Jenk
May be worth changing the PHP_SELF for SCRIPT_NAME :)

Posted: Fri Jan 27, 2006 5:41 am
by Maugrim_The_Reaper
Agreed - PHP_SELF is insecure unless filtered and validated like any other user input.

Posted: Fri Jan 27, 2006 6:15 am
by jayshields
Is there an easy way to get the current URL (or just script name or anything) but include all the current GET data in the URL aswell?

At the moment I use this:

Code: Select all

$tmp = FALSE;
$first = FALSE;
$url = basename(__FILE__);
foreach($_GET as $key => $value) {
  if($first == FALSE) $url .= '?';
  $url .= $key . '=' . $value . '&';
  $tmp = TRUE;
  $first = TRUE;
}
if($tmp == TRUE) $url = substr($url, 0, strlen($url) - 1);
But I'm sure it's far too long winded to be commonly used.

Posted: Fri Jan 27, 2006 7:30 am
by Roja
Jenk wrote:May be worth changing the PHP_SELF for SCRIPT_NAME :)
Keep in mind that SCRIPT_NAME isn't populated on IIS, and I seem to recall that it doesn't on OpenBSD (although I dont have an install handy to confirm again).

Posted: Fri Jan 27, 2006 7:43 am
by redmonkey
And also that when running as a CGI, SCRIPT_NAME will most likely reflect the location of the PHP executable and not the actual script.