php获取当前页面完整真实的url地址的方法,包括带参数的,总结了三种方法,大家可以根据需要选择,具体函数代码如下:
第一种方法:
function GetCurUrl(){ if(isset($_SERVER['REQUEST_URI'])){ $url=$_SERVER['REQUEST_URI']; }else{ if(isset($_SERVER['argv'])){ $url=$_SERVER['PHP_SELF'].'?'.$_SERVER['argv'][0]; }else{ $url=$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; } } return urlencode($url);//注意这里给编码了 }
第二种方法(据说织梦用的就是这种方法):
//获得当前的脚本网址 function GetCurUrl(){ if(!empty($_SERVER['REQUEST_URI'])){ $scriptName=$_SERVER['REQUEST_URI']; $nowurl=$scriptName; }else{ $scriptName=$_SERVER['PHP_SELF']; if(empty($_SERVER['QUERY_STRING'])){ $nowurl=$scriptName; }else{ $nowurl=$scriptName.'?'.$_SERVER['QUERY_STRING']; } } return $nowurl; }
第三种方法(推荐):
//php获取当前访问的完整url地址 function GetCurUrl(){ $url='http://'; if(isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on'){ $url='https://'; } if($_SERVER['SERVER_PORT']!='80'){ $url.=$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI']; }else{ $url.=$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; } return $url; }