<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"> <title>PHP中获取当前URL</title> </head> <body> <script type="text/javascript"> var a = top.location.href; //顶级窗口的地址 var b = this.location.href; //当前窗口的地址 document.write('顶级窗口的地址:'+a+'<br>当前窗口的地址:'+b+'<br><br>'); </script> <?php //测试网站:http://localhost/webManage/demo.php?f=onestopweb.cn echo '获取域名或主机地址:<br>'; echo $_SERVER['HTTP_HOST']."<br>"; echo '获取网页地址:<br>'; echo $_SERVER['PHP_SELF']."<br>"; echo '获取网址参数:<br>'; echo $_SERVER["QUERY_STRING"]."<br>"; echo '获取完整的url:<br>'; echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."<br>"; echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']."<br>"; echo '包含端口号的完整url:<br>'; echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]."<br>"; $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]."<br>"; echo dirname($url); ?> </body> </html>
렌더링: