欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 获取当前 URL

程序员文章站 2022-05-19 11:49:24
...
<!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>

效果图:

PHP 获取当前 URL