Different includes for certein referrers

Discussion in 'PHP' started by eho, Apr 4, 2013.

  1. #1
    Hello!

    I have a website about cosmetics where I want to offer specials for people who come to my website from specific sites.

    I have found something like that:
    <?php
    $referrer = $_SERVER['HTTP_REFERER'];
    if (preg_match("/specificwebsite.com/",$referrer)) {
          header('Location: http://www.mywebsite.com/1.html');
    } else {
          header('Location: http://www.mywebsite.com/2.html');
    };
    ?>
    Code (markup):
    But this is REDIRECTING and its only for one referrer (I have them over 20), I need something that INCLUDE files, is it possible with $_SERVER['HTTP_REFERER']?

    Also it is possible to have these specific domains in external file like domains.txt so I do not have to edit every page on my website when I update that list?

    Anyone know script that can do that?

    Thank you for help,
    Evelin
     
    eho, Apr 4, 2013 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    You could make use of http://php.net/manual/en/language.types.array.php and check if the domainname is in the $_SERVER['HTTP_REFERER'];

    example:
    
    <?php
    $sites = array(
    "www.yourwebsite.com" => "1.html",
    "www.google.com" => "2.html",
    "www.bing.com" => "3.html",
    "www.mysite.org" => "4.html",
    "tweakers.net" => "5.html",
    "www.test.com" => "6.html"
    );
     
    foreach ($sites AS $key => $location)
    {
        if (stripos($_SERVER['HTTP_REFERER'], $key) > 0)
        {
            header("Location: " . $location);
            exit;
        }
    }
    ?>
     
    
    PHP:
     
    EricBruggema, Apr 4, 2013 IP
    YoGem likes this.
  3. eho

    eho Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    I have a script that should do what I want, it looks like this:

    <?php
        $x=$_SERVER['HTTP_REFERER'];
        $x1=str_ireplace("http://","",$x);
        $t=file_get_contents("sites.txt");
        $arr=explode("\n",$t);
        if(!empty($x) && in_array($x1,$arr))
        {
            include("file1.php");
        }
    ?>
    PHP:
    Unfortunately when I added this to my website I got "Internal Server Error", anyone can tell me what is wrong with that code?

    Thank you,
    Evelin
     
    eho, Apr 10, 2013 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    
        $referer = str_ireplace('http://', '', $_SERVER['HTTP_REFERER']);
       
        $refListing = explode("\n",
                              file_get_contents('sites.txt'));
        if(in_array($x1, $arr))
        {
            include("file1.php");
        }
    
    Code (markup):
    better way is

    to create a seperate PHP file with your list you want to use as referer check

    
    <?php
    /* array_referers.php */
     
    $refererCheckArray = array( /* your list to all the 'files' */
    'yourwebsite.com' => 'file1.php',
    'otherwebsite.com' => 'file2.php',
    'google.com' => 'google.php',
    'gfxpoll.com' => 'polls.php',
    'etc' => 'etc.php'
    );
     
    foreach ($refererCheckArray AS $url => $file) /* loop rech row in refererCheckArray key = value
    {
        if (stristr($_SERVER['HTTP_RERFERER'], $url)) // is the referer containing the url case insensetive
        {
            include($file);
            break; /* break foreach, no need to contine..*/
        }
    }
    ?>
    
    Code (markup):
    and you can use this code as an include!

    *code not tested!*
     
    EricBruggema, Apr 10, 2013 IP