getting current url
Moderator: General Moderators
-
psychotomus
- Forum Contributor
- Posts: 487
- Joined: Fri Jul 11, 2003 1:59 am
getting current url
how you get current URL?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
print_r($_SERVER);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*
edit: fixed the query string after testing on a https server.
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();
?>- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
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:
But I'm sure it's far too long winded to be commonly used.
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);