PHP && JS获取当前网址

一:JS实现 

top.location.href   顶级窗口的地址
this.location.href  当前窗口的地址

二:PHP实现

#测试网址:     http://localhost/blog/testurl.php?id=5

//获取域名或主机地址
echo $_SERVER[‘HTTP_HOST‘]."<br>"; #localhost

//获取网页地址
echo $_SERVER[‘PHP_SELF‘]."<br>"; #/blog/testurl.php

//获取网址参数
echo $_SERVER["QUERY_STRING"]."<br>"; #id=5

//获取用户代理
echo $_SERVER[‘HTTP_REFERER‘]."<br>"; 

//获取完整的url
echo ‘http://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER[‘REQUEST_URI‘];
echo ‘http://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER[‘PHP_SELF‘].‘?‘.$_SERVER[‘QUERY_STRING‘];
#http://localhost/blog/testurl.php?id=5

//包含端口号的完整url
echo ‘http://‘.$_SERVER[‘SERVER_NAME‘].‘:‘.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://localhost:80/blog/testurl.php?id=5

//只取路径
$url=‘http://‘.$_SERVER[‘SERVER_NAME‘].$_SERVER["REQUEST_URI"];
echo dirname($url);
#http://localhost/blog
时间: 2024-08-28 20:48:41

PHP && JS获取当前网址的相关文章

如何用js得到当前页面的url信息方法(JS获取当前网址信息)

设置或获取对象指定的文件名或路径. alert(window.location.pathname) 设置或获取整个 URL 为字符串. alert(window.location.href); 设置或获取与 URL 关联的端口号码. alert(window.location.port) 设置或获取 URL 的协议部分. alert(window.location.protocol) 设置或获取 href 属性中在井号“#”后面的分段. alert(window.location.hash) 设

js得到当前页面的url信息方法(JS获取当前网址信息)

1.设置或获取整个 URL 为字符串: window.location.href 2.设置或获取与 URL 关联的端口号码: window.location.port 3.设置或获取 URL 的协议部分 window.location.protocol 4.设置或获取 href 属性中跟在问号后面的部分 window.location.search 5.获取变量的值(截取等号后面的部分) var url = window.location.search;//    alert(url.lengt

js获取来源网址

举例: 1. a.html文件内容如下: <a href="b.html">浏览b.html </a> 2. b.html文件中的内容如下: <body> <script type="text/javascript"> document.write(document.referrer); </script> </body> 3. 则在通过a.html中的超链接访问b.html的时候,显示的结果是

js获取当前页面url网址等信息

使用js获取当前页面的url网址信息. 1.设置或获取整个 URL 为字符串: window.location.href 2.设置或获取与 URL 关联的端口号码: window.location.port 3.设置或获取 URL 的协议部分 window.location.protocol 4.(www.jbxue.com)设置或获取 href 属性中跟在问号后面的部分 window.location.search 5.获取变量的值(截取等号后面的部分) var url = window.lo

js 获取当前页url网址信息

转载地址:js如何准确获取当前页面url网址信息 摘录: 举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.window.location.href(设置或获取整个 URL 为字符串) var test = window.location.href; alert(test);返回:http://i.cnblogs.com/EditPosts.aspx?opt=1 2.window.location.protocol(

js获取当前页面的url网址信息小汇总

在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,在这里是我的一些获取url信息的小总结. 下面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.window.location.href(设置或获取整个 URL 为字符串) var test = window.location.href; alert(test); 返回:http://i.cnblogs.com/EditPosts.as

js 获取当前的网址

js 获取当前的网址http://www.xcx.cc/index.php/home/index/ind?idf=12321var $cur_url=window.location.href; //获取全部的网址var $psurl = window.location.pathname; // 获取 index.php/home/index/ind var $par = window.location.search; //获得 ?idf=12333var test = window.locati

使用JS准确获取URL网址中参数的几种方法

记录下使用JS准确获取URL网址中参数的方法: 参考链接1. https://blog.csdn.net/Zhihua_W/article/details/54845945?utm_source=blogxgwz9 2.https://blog.csdn.net/william_jzy/article/details/84942781 原文地址:https://www.cnblogs.com/lwming/p/10954725.html

js获取网站项目根路径

//js获取项目根路径,如: http://localhost:8083/uimcardprj function getRootPath(){ //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp var curWwwPath=window.document.location.href; //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp var pathName=window.document.