note 75709 added to reserved.variables
| From: | mike at osu1 dot php dot net | Date: | Tue, 12 Jun 2007 20:19:33 +0000 |
| Subject: | note 75709 added to reserved.variables | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127428@lists.php.net to get a copy of this message | ||
<?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;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 68.230.193.105
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.php
Edit -- https://master.php.net/note/edit/75709
Del: integrated -- https://master.php.net/note/delete/75709/integrated
Del: useless -- https://master.php.net/note/delete/75709/useless
Del: bad code -- https://master.php.net/note/delete/75709/bad+code
Del: spam -- https://master.php.net/note/delete/75709/spam
Del: non-english -- https://master.php.net/note/delete/75709/non-english
Del: in docs -- https://master.php.net/note/delete/75709/in+docs
Del: other reasons-- https://master.php.net/note/delete/75709
Reject -- https://master.php.net/note/reject/75709
Search -- https://master.php.net/manage/user-notes.php