如何将 HTTP 引用地址传递给 PHP 脚本
这看起来应该很简单。任何指示将不胜感激。
考虑三个站点:
- widgetserver.com,它向客户端站点提供小部件
- clientsite.com,显示小部件
- previoussite.com,这是一个链接到 clientsite.com 的网站
在clientsite.com 的页面中,有一段代码调用widgetserver.com 上的PHP 函数。它看起来像这样:
当 clientsite.com 加载时,它运行“show_widget.php”,这又将小部件放在页面上,
这就是我陷入困境的地方:我想从“show_widget.php”内部访问将用户引向clientsite.com的网站,但在这个函数我们在 widgetserver.com 上,所以引用者是 clientsite.com,而不是 previoussite.com。
有什么方法可以将 clientsite.com 的引用者传递给“show_widget.php”吗?
This seems like it should be simple. Any pointers would be much appreciated.
Consider three sites:
- widgetserver.com, which serves widgets to client sites
- clientsite.com, which displays the widget
- previoussite.com, which is a site that links to clientsite.com
In clientsite.com's page there is a piece of code that calls a PHP function on widgetserver.com. It looks like this:
<script type="text/javascript" src="http://widgetserver.com/show_widget.php></script>
When clientsite.com is loaded, it runs "show_widget.php", which in turn places the widget on the page.
This is where I am stuck: I would like to access the site that referred the user to clientsite.com from inside "show_widget.php", but while in this function we are on widgetserver.com, so the referrer is clientsite.com, not previoussite.com.
Is there any way I can pass clientsite.com's referrer to "show_widget.php"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
document.referrer 包含您想要的引用者信息,因此如果它们正在运行 javascript,您可以执行以下操作:
这将需要位于 head 标记之外(除非它位于绑定到文档就绪事件或其他事件的函数中) ),否则会失败。
其背后的想法是在页面上生成具有所需属性的动态标记。第一行创建它,第二行设置脚本 src,第三页将其加载到 document.head DOM 元素中,这会导致对其进行评估,并根据需要加载文档。
如果您的客户使用 jQuery,则使用 $.getScript 更容易,与 document.referrer 的用法类似
document.referrer contains the referrer information you want, so if they're running javascript, you could do something like this:
This will need to go OUTSIDE of the head tag (unless it's in a function being bound to the document ready event or something), or it will fail.
The idea behind this is to generate a dynamic tag on the page with the attributes you want. The first line creates it, second line sets the script src, and the third page loads it into the document.head DOM element, which causes it to be evaluated, and loads the document as required.
If your clients are using jQuery, it is easier to use $.getScript, with a similar use of document.referrer
您无法从
show_widget.php
脚本中“猜测”此信息:客户端必须将该信息传输到您的脚本。这意味着您的小部件应该包含一些像这样的代码(假设客户端上的页面是用 PHP 生成的):
当然:
为了避免在用户浏览器没有发送引用者时生成/显示“注意:未定义索引:HTTP_REFERER”,您可能还需要添加一个检查,以在使用它之前查明它是否已定义 - 使用
isset
为此;例如,类似这样的事情可能会这样做:You cannot "guess" this information from your
show_widget.php
script : clientsite will have to transmit that information to your script.Which means your widget should be included with some code like this (provided the page on clientsite is generated in PHP) :
Of course :
To avoid a "Notice: Undefined index: HTTP_REFERER" from being generated/displayed when there is no referer sent by the user's browser, you might also want to add a check, to find out if it's defined before using it -- use
isset
for that ; for instance, something like this might do :您可以检测 clientside.com 主脚本上引用的内容并将其作为变量传递给您的脚本:
You could detect the referred on the main script of clientside.com and pass it as a variable to your script: