Home > Backend Development > PHP Tutorial > PHP获取当前页面完整url地址的方法

PHP获取当前页面完整url地址的方法

WBOY
Release: 2016-06-20 13:03:46
Original
992 people have browsed it

php获取当前页面完整真实的url地址的方法,包括带参数的,总结了三种方法,大家可以根据需要选择,具体函数代码如下:

第一种方法:

<p>function GetCurUrl(){</p>	if(isset($_SERVER['REQUEST_URI'])){<br />		$url=$_SERVER['REQUEST_URI'];<br />	}else{<br />		if(isset($_SERVER['argv'])){<br />			$url=$_SERVER['PHP_SELF'].'?'.$_SERVER['argv'][0];<br />		}else{<br />			$url=$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];<br />		}<br />	}<br />	return urlencode($url);//注意这里给编码了<br /><p>}</p>
Copy after login

第二种方法(据说织梦用的就是这种方法):

<p>//获得当前的脚本网址</p>function GetCurUrl(){<br />	if(!empty($_SERVER['REQUEST_URI'])){<br />		$scriptName=$_SERVER['REQUEST_URI'];<br />		$nowurl=$scriptName;<br />	}else{<br />		$scriptName=$_SERVER['PHP_SELF'];<br />		if(empty($_SERVER['QUERY_STRING'])){<br />			$nowurl=$scriptName;<br />		}else{<br />			$nowurl=$scriptName.'?'.$_SERVER['QUERY_STRING'];<br />		}<br />	}<br />	return $nowurl;<br /><p>}</p>
Copy after login

第三种方法(推荐):

<p>//php获取当前访问的完整url地址</p>function GetCurUrl(){<br />	$url='http://';<br />    if(isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on'){<br />        $url='https://';<br />    }<br />    if($_SERVER['SERVER_PORT']!='80'){<br />        $url.=$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];<br />    }else{<br />        $url.=$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];<br />    }<br />    return $url;<br />}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template