Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Redirecting based on visitor referral

Options
  • 18-06-2007 12:30am
    #1
    Registered Users Posts: 12,811 ✭✭✭✭


    Is there anyone out there that could tell me how to do this?

    I am looking to send someone who follows a link from another site to a certain page, or alternative website to mine.


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    You can use the $_SERVER superglobal in PHP, not 100% bulletproof, but I don't think it's ever going to be when you're dealing with referers.

    Anyway, I was bored, so I put something together that might work...
    [php]
    <?php
    $referers_i_hate = array("http://localhost/test1.html", "http://www.boards.ie", "http://localhost/test2.html");
    $go_away_to_here = "http://localhost/test2.html";

    if (isset($_SERVER) && strlen($_SERVER) > 0)
    {
    for ($i=0;$i<count($referers_i_hate);$i++)
    {
    if ($_SERVER == $referers_i_hate[$i])
    {
    header( "Location: $go_away_to_here" );
    }
    }
    }

    ?>
    <!--Start HTML here -->
    <html>

    [/php]
    Since it's sending a header, make sure it's the first thing on the page.
    Fill $referers_i_hate with whatever urls you don't want as a referer... $go_away_to_here allows one url to fock them all off to.

    You could go more fancy and block whole domains or all domains but your own (including/excluding blank if the referrer var is empty).

    There might be better ways of doing it, and my code may be horrible and wrong, but sure it gets the ball rolling.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    thanks donkeystyle, I will give that a go and get back to you in a moment with the results.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    <?php
    $referers_i_hate = array("http://www.siteihate1.com", "http://www.siteihate2.com", "http://www.siteihate3.com");
    $go_away_to_here = "http://www.gotothissite.com";
    
    if (isset($_SERVER['HTTP_REFERER']) && strlen($_SERVER['HTTP_REFERER']) > 0)
    {
      for ($i=0;$i<count($referers_i_hate);$i++)
      {
        if ($_SERVER['HTTP_REFERER'] == $referers_i_hate[$i])
        {
        header( "Location: $go_away_to_here" );  
        }
      }
    } 
    
    

    Do I need to replace HTTP_SERVER with anything?

    Edit: Actually I am looking for whole domains, as a site I wish to block has links to mine from different pages.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    No, just leave $_SERVER as-is.
    Edit: Actually I am looking for whole domains, as a site I wish to block has links to mine from different pages.
    Well, you could stick all of them into $referers_i_hate... as long as they're comma-seperated and in double quotes you can add as many as you want.
    Unless it's more dynamic like a forum or they keep changing it or whatever... that'd be a fulltime job. :eek:

    It's possible to add a few lines to that code that'll redirect based on domain, but whole URLs were just more convenient for me last night :)
    I'll either 'explode' the referer string and glue it back together as the domain name, or do some preg_match on it or whatever.

    I should get some time for this later tonight... unless any other PHP'ers want to jump in in the mean time.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    
    Parse error: syntax error, unexpected '<' in /home/limerick/domains/limerickblogger.org/public_html/blog/wp-content/themes/default/header.php on line 16
    
    

    This is the error message created by above code.


  • Advertisement
  • Registered Users Posts: 11,987 ✭✭✭✭zAbbo


    Might be an idea to test it on a plain php file before putting it into wordpress, just to get it working.

    I'd also be interested in the solution, whats on line 16 ?

    Google shows up this - http://www.phpjabbers.com/redirect-based-on-referrer-or-ip-address-php2.html


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    The code you pasted back is sans the closing PHP tag "?>"
    <?php
    $referers_i_hate = array("http://www.siteihate1.com", "http://www.siteihate2.com", "http://www.siteihate3.com");
    $go_away_to_here = "http://www.gotothissite.com";
    
    if (isset($_SERVER['HTTP_REFERER']) && strlen($_SERVER['HTTP_REFERER']) > 0)
    {
      for ($i=0;$i<count($referers_i_hate);$i++)
      {
        if ($_SERVER['HTTP_REFERER'] == $referers_i_hate[$i])
        {
        header( "Location: $go_away_to_here" );  
        }
      }
    } 
    
    [COLOR="Red"][B]?>[/B][/COLOR]
    
    Stick that in, then start the rest of the page... should be fine.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    I see, will try again.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    While it is no longer causing the error message, it doesn't seem to be doing what I want it to do. It doesn't divert the person away from my site. I am guessing that they would have to be coming from the exact URL for the link to work.

    would it be possible to divert them no matter what part of a website they are comming from?

    something like this

    Click on the link to indymedia to see what happens when someone comes from the site I have linked to above.

    something on indymedia sees them coming from somewhere undesirebale and fobbes them off to a different site.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Yeah I know exactly what you mean alright... the one I posted does that, but you'd have to use the exact URL they're coming from.

    There's one there on the link zabbo posted that uses the preg_match I was talking about.... (ie. if the domain you want to shítlist appears anywhere in the referer, then do X)
    zabbo wrote:
    It's the first one at the top of the page there.

    I can alter my one if you want, but it'll be re-inventing the wheel a bit. ;)


  • Advertisement
  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    Ok will have a look and let you know how I get on. thanks


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    now it works.

    If you follow this link to my site, it should work normally,

    however, if you take this route, it will send you somewhere else.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Yeah works for me :)


  • Registered Users Posts: 11,987 ✭✭✭✭zAbbo


    nicely done, those stormfront boys are something else, eh.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    Actually, on reflection, the second one might prove more useful, given that I have the indivual's IP address.

    Might send him and his friends to Section 19 of the litter pollution act every time they try to access my site.

    better still, is it possible to incorporate both pieces of code?


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Yeah you could combine them alright... I'd say take one or the others last 'else' block, snip out what's there (between the { }) and paste the other code inside (without the <?php ?> tags obv)... which one you put inside what depends on your priority.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    sweet, will give that a go,

    do I have to include the $visitor = $_SERVER; line as well though, from the second one?


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    
    <?
    $referrer = $_SERVER['HTTP_REFERER'];
    $visitor = $_SERVER['REMOTE_ADDR'];
    if (preg_match("/cadbury/",$referrer)) {
          header('Location: http://www.rowntree.com');
    } elseif (preg_match("/lyons/",$referrer)) {
          header('Location: http://www.barrystea.ie');
    } elseif (preg_match("/1.2.3.4/",$visitor)) {
          header('Location: http://www.startrek.coml');
    };
    }
    ?>
    
    

    How does that look?


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    do I have to include the $visitor = $_SERVER; line as well though, from the second one?
    Yeah I'd leave that in.
    How does that look?
    You've got a few extra bits left over "};"

    Cleaned it up a bit...
    <?
    $referrer = $_SERVER['HTTP_REFERER'];
    $visitor = $_SERVER['REMOTE_ADDR'];
    
    if (preg_match("/cadbury/",$referrer)) 
    {
          header('Location: http://www.rowntree.com');
    } 
    elseif (preg_match("/lyons/",$referrer)) 
    {
          header('Location: http://www.barrystea.ie');
    } 
    elseif (preg_match("/1.2.3.4/",$visitor)) 
    {
          header('Location: http://www.startrek.coml');
    }
    ?>
    


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    oki doki, will bang that in and see what happens.

    should know if it works soon enough :D


  • Advertisement
  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Yes... soon everyone will be eating fruit pastels, drinking barrys tea and watching star trek... phase one is complete, mwooahahahha! >:D


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    I can pm you the code with the correct URLs if you would like

    the first two redirects work, however, I won't know if the IP redirect works unless the person at that IP says something.


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    I can pm you the code with the correct URLs if you would like
    Nah, I've seen tubgirl and goatse already, thanks anyway ;):p
    the first two redirects work, however, I won't know if the IP redirect works unless the person at that IP says something.
    You could always set it to your own IP and see what happens.


  • Registered Users Posts: 12,811 ✭✭✭✭billy the squid


    Nah, I've seen tubgirl and goatse already, thanks anyway ;):p

    Nah no tubgirl im afraid, just sends unwanted visitors to sites they might find informative, and members of a racist group to the litter act (statute book website0
    You could always set it to your own IP and see what happens.

    oh i see, good thinking.

    edit: the IP thing works perfectly

    thanks for the help zabbo and donkeystyle.


Advertisement