note 75709 deleted from reserved.variables by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 18:27:14 +0000 |
| Subject: | note 75709 deleted from reserved.variables by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134363@lists.php.net to get a copy of this message | ||
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;
?>