简体   繁体   English

根据引荐网址重定向访问者

[英]Redirect visitor based on referral URL

I want to redirect visitors to one page on my website if they have come from a certain URL and another if they don't. 我想将访问者重定向到我网站上的某个页面(如果它们来自某个URL),如果不是来自某个URL,则将其重定向。

<?
$referer = $_SERVER['HTTP_REFERER'];
if ( $referer != "http://URL1" ) {
  header('Location: page1');
  exit;
 } else {
  header('Location: page2');
 }
?>

Whatever referrer I come to the page on it goes to page 1 and never to page 2. I have used this code in a index.php file so its the first page the visitor is directed too. 无论访问哪个引荐来源,我都转到第1页,而从不访问第2页。我在index.php文件中使用了此代码,因此它也是访客指向的第一页。

UPDATE: alright, so from the discussions, it seems that the reason why your code won't work is that you are checking the referer string using the "now-www" url, while the actual referer string has "www" in the url. 更新:好的,因此从讨论中看来,您的代码无法正常工作的原因是您正在使用“ now-www” URL检查引荐字符串,而实际的引荐字符串在URL中具有“ www” 。 Please, make sure to use the exact referer string. 请确保使用确切的引用字符串。 Otherwise, if you are redirecting based on the hostname of the referer you can use the updated answer below. 否则,如果要基于引用者的hostname进行重定向,则可以使用下面的更新后的答案。

<?php
$referer = str_replace("www.", "", parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
switch($referer) {
  case "johnchow.com":
    header("Location: page1");
    break;
  case "domain2.com":
    header("Location: page2");
    break;
  default:
    header("Location: page3");
}
exit;

For starters change this to 首先,将其更改为

if ( $referer != "http://URL1" || $referer != "http://URL2" )

Secondly, page1 and page2 are likely giving the error because they are invalid. 其次,page1和page2可能会给出错误,因为它们无效。 Include the path and extension. 包括路径和扩展名。 For example: 例如:

header('Location: http://www.yourlocation/page1.php')

Looks like the error has been clarified... 看来错误已得到澄清...

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

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