Thursday, December 11, 2008

Php - Get current page URL

Several times it becomes necessary to find the url of the current page in php. It is not a very big deal to find it, but for a newbie it may take quite some time. So here is a function to do that. The url is stored in a static variable so that it doesn't have to be generated everytime the function is called.


function getCurrentPageUrl(){
static
$pageURL = '';
if(empty(
$pageURL)){
$pageURL = 'http';
if(isset(
$_SERVER['HTTPS']) && $_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;
}
?>

No comments: