If referrer statement - +rep for help!

Discussion in 'PHP' started by Random Guy, Apr 3, 2009.

  1. #1
    I'm trying to figure out how to do this in PHP:

    if (visitorreferrerurl != from currentdomain) {
     some echoed html code here
    }
    Code (markup):
    Don't know what the php command to check that stuff is. Anyone know offhand?

    Note - this will be going into a wordpress theme on all pages, so I'm not sure if all PHP commands can be run through <?php ?> in there or not.
     
    Random Guy, Apr 3, 2009 IP
  2. kusal

    kusal Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this code,

    <?php
    
    $visitorreferrerurl = $_SERVER['HTTP_HOST']; 
    $currentdomain = 'www.yourdomain.com';
    
    if($visitorreferrerurl != $currentdomain)
    {
    	echo 'error';
    }
    
    ?>
    Code (markup):
     
    kusal, Apr 3, 2009 IP
    Random Guy likes this.
  3. ultrasonic

    ultrasonic Peon

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Actually it would be...

    if(stristr($_SERVER['HTTP_REFERER'], "yoursite.com")){
    // from your site
    }else{
    // not from here
    }
     
    ultrasonic, Apr 4, 2009 IP
    Random Guy likes this.
  4. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    DO NOT USE THIS. It is not proper coding logic. Stristr returns a string, should not be used this way (checking for existance). Here:

    
    if(strpos($_SERVER['HTTP_REFERER'], "yoursite.com")===false){
    // Not from your site
    }else{
    // from here
    }
    
    PHP:
     
    NatalicWolf, Apr 4, 2009 IP
    Random Guy likes this.
  5. fourfingers

    fourfingers Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    if(preg_match("/domain.com/i",$_SERVER['HTTP_REFERER'])) {
      // my site in the referrer
    } else {
      // other site or blank referrer
    }
    PHP:
     
    fourfingers, Apr 5, 2009 IP
    Random Guy likes this.
  6. Random Guy

    Random Guy Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the help everyone :)
     
    Random Guy, Apr 7, 2009 IP
  7. joep1978

    joep1978 Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    stristr returns a string OR false if needle is not found. There is nothing illogical about checking if stristr has returned false.
     
    joep1978, Apr 7, 2009 IP
  8. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I don't mean to resurrect a dead topic, but yes there is something illogical about USING stristr for check a string's presence in another string. It returns a string, which means PHP's interpreter must search the string, then splice the string...Instead, using strpos will result in you being able to find the string AND just return a number...which is all that is needed. Using extra code, is pointless and on large sites can make the difference between downtime and uptime...

    strpos can be used as follows:

    
    // Checking for string existance
    if(strpos('abc','f')===false) //Since 0 in this case is not a false result we check for an absolute false.
    // DO STUFF
    else
    // WAS FOUND, DO STUFF
    
    
    PHP:
     
    NatalicWolf, May 20, 2009 IP
  9. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #9
    yep, its $_SERVER['HTTP_REFERER']

    an easy way to do it .. turn the prior website they came from (HTTP_REFERER) into a variable.

    $website_camefrom = $_SERVER['HTTP_REFERER'];
    // then do the logic for it.
    if($website_camefrom == $website_youwant){
    //example.
    echo "You came from the website i wanted you to come from!";
    } else {
    echo "you came from a website i dont give a crap about dork!";
    }

    in the code to un in the logic ..you can do whatever you want instead of echoing stuff.
    Turn your Referr and your websites you want to check it by ..into PHP simple variables.
    thats a good way to keep it from becoming confusing if you look at it 5 years later and wonder what the heck it is.
     
    ezprint2008, May 21, 2009 IP