1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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:
    Code (Text):
    1. <?php
    2. $referrer = $_SERVER['HTTP_REFERER'];
    3. if (preg_match("/specificwebsite.com/",$referrer)) {
    4.       header('Location: http://www.mywebsite.com/1.html');
    5. } else {
    6.       header('Location: http://www.mywebsite.com/2.html');
    7. };
    8. ?>
    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,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #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:
    1.  
    2. <?php
    3. $sites = array(
    4. "www.yourwebsite.com" => "1.html",
    5. "www.google.com" => "2.html",
    6. "www.bing.com" => "3.html",
    7. "www.mysite.org" => "4.html",
    8. "tweakers.net" => "5.html",
    9. "www.test.com" => "6.html"
    10. );
    11.  
    12. foreach ($sites AS $key => $location)
    13. {
    14.     if (stripos($_SERVER['HTTP_REFERER'], $key) > 0)
    15.     {
    16.         header("Location: " . $location);
    17.         exit;
    18.     }
    19. }
    20. ?>
    21.  
    22.  
    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:
    1. <?php
    2.     $x=$_SERVER['HTTP_REFERER'];
    3.     $x1=str_ireplace("http://","",$x);
    4.     $t=file_get_contents("sites.txt");
    5.     $arr=explode("\n",$t);
    6.     if(!empty($x) && in_array($x1,$arr))
    7.     {
    8.         include("file1.php");
    9.     }
    10. ?>
    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,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #4
    Code (Text):
    1.  
    2.     $referer = str_ireplace('http://', '', $_SERVER['HTTP_REFERER']);
    3.    
    4.     $refListing = explode("\n",
    5.                           file_get_contents('sites.txt'));
    6.     if(in_array($x1, $arr))
    7.     {
    8.         include("file1.php");
    9.     }
    10.  
    better way is

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

    Code (Text):
    1.  
    2. <?php
    3. /* array_referers.php */
    4.  
    5. $refererCheckArray = array( /* your list to all the 'files' */
    6. 'yourwebsite.com' => 'file1.php',
    7. 'otherwebsite.com' => 'file2.php',
    8. 'google.com' => 'google.php',
    9. 'gfxpoll.com' => 'polls.php',
    10. 'etc' => 'etc.php'
    11. );
    12.  
    13. foreach ($refererCheckArray AS $url => $file) /* loop rech row in refererCheckArray key = value
    14. {
    15.     if (stristr($_SERVER['HTTP_RERFERER'], $url)) // is the referer containing the url case insensetive
    16.     {
    17.         include($file);
    18.         break; /* break foreach, no need to contine..*/
    19.     }
    20. }
    21. ?>
    22.  
    and you can use this code as an include!

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