getting current url
Posted: Fri Sep 09, 2005 4:53 pm
how you get current URL?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
print_r($_SERVER);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();
?>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);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).Jenk wrote:May be worth changing the PHP_SELF for SCRIPT_NAME