getting current url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

getting current url

Post by psychotomus »

how you get current URL?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

print_r($_SERVER);
will give you some useful tidbits :wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

May be worth changing the PHP_SELF for SCRIPT_NAME :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Agreed - PHP_SELF is insecure unless filtered and validated like any other user input.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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).
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
Post Reply