php 获取PHP调用网页的URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3461338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get URL of calling webpage in PHP
提问by Nate
I am working on a website statistics engine for a class. The idea being that you can simply embed a little code in your web page which will call the stats website on every page load and the stats website will then track your hits and such… nothing ground breaking.
我正在为一个班级开发网站统计引擎。这个想法是你可以简单地在你的网页中嵌入一些代码,它会在每个页面加载时调用统计网站,然后统计网站会跟踪你的点击量等等......没有什么突破性的。
What I would like to do is be able to break down website hits by webpage. For instance, a person can include the same code on each page, and the stats website will know which page got hit how many times. Is there a way in PHP to obtain the URL of the calling page (the page on which the embedded code exists)? I know how to get the URL of the page in which the PHP code is running, but not the calling page.
我想做的是能够按网页分解网站点击量。例如,一个人可以在每个页面上包含相同的代码,统计网站就会知道哪个页面被点击了多少次。PHP中有没有办法获取调用页面(嵌入代码所在的页面)的URL?我知道如何获取运行 PHP 代码的页面的 URL,但不知道调用页面的 URL。
Alternatively, I could probably use some JavaScript to pass the page URL to the stats website, but the less code that needs to be embedded the better so I'm hoping for a PHP solution.
或者,我可能会使用一些 JavaScript 将页面 URL 传递到统计网站,但需要嵌入的代码越少越好,所以我希望有一个 PHP 解决方案。
Thanks in advance for any and all help!
在此先感谢您的任何帮助!
回答by Ian Wetherbee
$_SERVER['HTTP_REFERER']
will get you the referring page.
$_SERVER['HTTP_REFERER']
将为您提供参考页面。
回答by Zak
In addition to $_SERVER['HTTP_REFERER']
since there may be large chunks of the URI you wish to ignore, you can just pass the page name as a request param to the called script.
此外,$_SERVER['HTTP_REFERER']
由于您可能希望忽略大量 URI,您可以将页面名称作为请求参数传递给被调用的脚本。
I.E. on the page with stats tracking you request
带有跟踪您请求的统计信息的页面上的 IE
URL/stats_tracker.php?page=checkout
on the stats tracker page you just look for
在统计跟踪页面上,您只需查找
$_REQUEST['page'];
This can be cleaner than looking at the whole URL.
这比查看整个 URL 更清晰。