Redirect user based upon HTTP_REFERER

限于喜欢 提交于 2019-12-23 02:18:40

问题


I am trying to redirect users on my site that come from a certian referal site to have a special message. I have this:

<?php $REFERER = $_SERVER['HTTP_REFERER'];

  if ($REFERER == "http://www.url.com/") { 
?>

Content Goes Here

?>    
    }
?>

And they do not get the message

I did a test and did it from one of my other sites and echoed what the $_SERVER['HTTP_REFERER']; put out and changed the above code to match it exactly and it worked but I think the issue I am having is the output of the $_SERVER['HTTP_REFERER']; is not EXACTLY the same. For instance if they were on the page www.domain.com/page2/index.php the referrer would be http://www.domain.com/page2/

is there a way to make it so people that come from www.domain.com no matter whats after the / ?!?

UPDATE:

I tried all the first 2 answers and could not get it to work (possibly my fault) so i did some research from what they sent and created this:

<?php 
    $mystring = $_SERVER['HTTP_REFERER']; 
    $findme   = 'domain'; 
    $pos = strpos($mystring, $findme);

if ($pos === false) {
    } else {
    echo "content";
 ?>

Is there anything wrong with this option as it seems to work?


回答1:


You can check if the string starts with http://www.url.com/

if (strpos($REFERER, "http://www.url.com/") === 0) { 



回答2:


A real simple one would be:

if (strpos($REFERER, "www.url.com") !== false) { 



回答3:


You can use php's parse_url function:

<?php $REFERER =  parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH);

    if ($REFERER['host'] == "www.url.com") { 
?>


来源:https://stackoverflow.com/questions/13654834/redirect-user-based-upon-http-referer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!