PHP获取当前页面的URL

时间:2023-03-09 15:40:10
PHP获取当前页面的URL
/**
* 获取当前页面完整URL地址
*
* @author 52php.cnblogs.com
*/
function http_get_page_url()
{
global $_G; if (empty($_G['pageUrl']))
{
$protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
$phpSelf = $_SERVER['PHP_SELF'] ? string_safe_replace($_SERVER['PHP_SELF']) : string_safe_replace($_SERVER['SCRIPT_NAME']);
$pathInfo = isset($_SERVER['PATH_INFO']) ? string_safe_replace($_SERVER['PATH_INFO']) : '';
$relateUrl = isset($_SERVER['REQUEST_URI']) ? string_safe_replace($_SERVER['REQUEST_URI']) : $phpSelf . (isset($_SERVER['QUERY_STRING']) ? '?' . string_safe_replace($_SERVER['QUERY_STRING']) : $pathInfo);
$_G['pageUrl'] = trim($protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relateUrl, '/');
} // 删除 backUrl 参数
$_G['pageUrl'] = preg_replace('/&backUrl=[^&]+/', '', $_G['pageUrl']); return $_G['pageUrl'];
}
/**
* 安全过滤函数
*
* @param string $string
* @return string
*/
function string_safe_replace($string)
{
$string = str_replace('%20', '', $string);
$string = str_replace('%27', '', $string);
$string = str_replace('%2527', '', $string);
$string = str_replace('*', '', $string);
$string = str_replace('"', '"', $string);
$string = str_replace("'", '', $string);
$string = str_replace('"', '', $string);
$string = str_replace(';', '', $string);
$string = str_replace('<', '&lt;', $string);
$string = str_replace('>', '&gt;', $string);
$string = str_replace("{", '', $string);
$string = str_replace('}', '', $string);
$string = str_replace('\\', '', $string); return $string;
}

延伸阅读:

$_SERVER["SCRIPT_NAME"]、$_SERVER["PHP_SELF"]、$_SERVER["QUERY_STRING"]、$_SERVER["REQUEST_URI"]