Redirect user based upon HTTP_REFERER

2019-06-03 05:31发布

问题:

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") { 
?>