Note Submitter: mike
----
<?php
/*
Simple function to get current page URL using comman PHP variables
Function inputs:
$base if set to true will add the basename to the URL
$www if set to true will add www. to host if not found
$query if set to true will add the query string to the URL
$echo if set to true will echo the URL instead of just returning it
*/
function get_url($base = true, $www = true, $query = true, $echo = false){
$URL = ''; //open return variable
$URL .= (($_SERVER['HTTPS'] != '') ? "https://" : "http://"); //get protocol
$URL .= (($www == true && !preg_match("/^www\./", $_SERVER['HTTP_HOST'])) ? 'www.'.$_SERVER['HTTP_HOST'] : $_SERVER['HTTP_HOST']); //get host
$path = (($_SERVER['REQUEST_URI'] != '') ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']); //tell the function what path variable to use
$URL .= ((pathinfo($path, PATHINFO_DIRNAME) != '/') ? pathinfo($path, PATHINFO_DIRNAME).'/' : pathinfo($path, PATHINFO_DIRNAME)); //set up directory
$URL .= (($base == true) ? pathinfo($path, PATHINFO_BASENAME) : ""); //add basename
$URL = preg_replace("/\?".preg_quote($_SERVER['QUERY_STRING'])."/", "", $URL); //remove query string if found in url
$URL .= (($query == true && $_SERVER['QUERY_STRING'] != '') ? "?".$_SERVER['QUERY_STRING'] : ""); //add query string
if($echo == true){
echo $URL;
}else{
return $URL;
}
}
/* Example */
$page_url = get_url();
echo $page_url;
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.