简体   繁体   English

根据引荐来源网址进行重定向

[英]Redirect based on referer's url

I am trying to redirect visitors to a site based on their referring url. 我正在尝试根据访问者的引用URL将访问者重定向到网站。

Here is the script: 这是脚本:

php
$domain='blankds.com';
$referrer=$_SERVER['HTTP_REFERER'];
echo $referrer;
if (preg_match("/$domain/",$referrer)) {
 header('Location: http://www.blackisgreen.org/page_1.php');
 } else {
 header('Location: http://www.blackisgreen.org/page_2.php');
};

Errors: I get a "Warning: cannot modify header" error because I am echoing $referrer before sending headers. 错误:我收到“警告:无法修改标题”错误,因为我在发送标题之前回显了$ referrer。

If I remove the echo the script does not work. 如果删除回显,脚本将无法运行。

Any suggestions? 有什么建议么?

PHP is sending headers to the user requesting the page when you echo $referrer . 当您echo $referrer时,PHP会将标头发送给请求页面的用户。 The header function you are then calling attempts to modify these headers and affix a location redirect but cannot as the headers have already been sent along with the start of your page content. 然后,您要调用的标头函数尝试修改这些标头并附加位置重定向,但由于标头已经随页面内容的开头一起发送,因此无法进行。

To get around this problem, take a look at PHP's Output Control functions, especially ob_start(); 为了解决这个问题,请看一下PHP的Output Control函数,尤其是ob_start(); which inserted at the top of your script should allow you to continue echoing the redirect location and allowing you to redirect at the same time. 在脚本顶部插入的代码应允许您继续回显重定向位置并允许您同时进行重定向。

Just as a note: Any output will auto-generate headers. 请注意:任何输出都会自动生成标头。 If you want to redirect with headers you just need to comment out echo $referrer; 如果要使用标题重定向,则只需注释掉echo $referrer; If you need to see what referrer is going to which site for debugging purposes, just put it in the URL, the receiving page should ignore it. 如果您需要查看哪个引荐来源网址将用于调试目的,只需将其放置在URL中,接收页面应将其忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM