need help with array

Discussion in 'PHP' started by DjZoC, Aug 6, 2010.

  1. #1
    hello there, this is my command

    <?php
    
     $allowed_refferer = array(
      'http://www.google.com',
      'http://www.yahoo.com'
     );
    
     if (in_array($_SERVER['HTTP_REFERER'], $allowed_refferer)) {
      echo 'a';
     } else {
      echo 'b';
     }
    
    ?>
    PHP:
    i have one problem hope someone can help me with this...
    if the refferer is http://www.google.com is show me echo a

    but if the refferer is http://www.google.com/search... is not working anymore ... how i can make this work ?

    i want to use array becouse is more easy to add the links becouse before i use preg_match ...
     
    DjZoC, Aug 6, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Hmm I don't get it what's the problem. If the referrer is from http://www.google.com/search then why not just include it in the array? Otherwise, use preg_match
    
    <?php
    
     $allowed_refferer = array(
      'http://www.google.com',
      'http://www.yahoo.com',
      'http://www.google.com/search'
     );
    
     if (in_array($_SERVER['HTTP_REFERER'], $allowed_refferer)) {
      echo 'a';
     } else {
      echo 'b';
     }
    
    ?>
    
    PHP:
     
    Rainulf, Aug 6, 2010 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    Would be easier to do it by host rather than URL:

    if( isset( $_SERVER['HTTP_REFERER'] ) ) {
    	$Referer = parse_url( $_SERVER['HTTP_REFERER'] );
    
    	 $allowed_refferer = array(
    	  'google.com',
    	  'yahoo.com'
    	 );
    
    	 if (in_array( $Referer['host'] , $allowed_refferer)) {
    	  echo 'a';
    	 } else {
    	  echo 'b';
    	 }
    }
    PHP:
     
    Alex Roxon, Aug 6, 2010 IP
  4. GetGamesHere

    GetGamesHere Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    One other note, in_array() is case sensitive, so you might want to use strtolower().
     
    GetGamesHere, Aug 8, 2010 IP