PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2013-09-11 17:31:42  修改时间:2024-12-09 12:59:03  分类:11.PHP基础  编辑
/**
 * 获取当前页面完整URL地址
 *
 * @author www.php-note.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"]