help creating a domain ban list in PHP

Discussion in 'PHP' started by phantom, Nov 3, 2007.

  1. #1
    I have a list of domains I want to use as a ban list.


    The file for the ban list contains domains like:

    yahoo.com
    google.com


    I want to compare those names in the banlist with $_SERVER['HTTP_REFERER']

    and if they exist in both the server variable and the banlist file...set a new variable called $dontadd to = 1.

    The problem I am having is that the domain I want to check against in $_SERVER['HTTP_REFERER'] may have extra data in it like www.domain.com or worse www.foo.domains.com or even trailing info after the domain.

    Can you guys help me out to only match a domain that was contained in my ban list?

    Here is my clumsy code so far.

    $banlist = "http://$siteurl/banlist";
    $domain  = $_SERVER['HTTP_REFERER'];
    $domain = str_replace('http://', '', $domain );
    $domain = str_replace('/', '', $domain );$domain = "/^$domain/";
    $fcg = file_get_contents($banlist);
    if (preg_match($domain,$fcg)){
    $dontadd ='1';
    Code (markup):

    ...which works but it's not fool proof.

    any ideas how to make it fool proof or close to it?

    Thanks in advance!
     
    phantom, Nov 3, 2007 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $parse = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
    
    $banned = file('banlist.txt');
    
    foreach ($banned as $line) {
    	if (stripos($parse, $line)) 
    		$dontadd = 1;
    }
    PHP:
    Try that (untested). It should work well enough unless they come from a site like www.google.com.notbannedsite.com, which is unlikely.
     
    decepti0n, Nov 3, 2007 IP
  3. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    why dont you just make an array out of the domains? and then check if is in_array();
     
    bobb1589, Nov 3, 2007 IP
  4. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #4
    deception:

    No matter what I try with your code $dontadd is always = to 1

    It seems as if it counts a line even when there is none.

    Any other ideas?
     
    phantom, Nov 4, 2007 IP
  5. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #5
    phantom: It should work. I think the file() is not reading in the array properly. As it sometimes does to me. Try to just do an array instead.

    
    $parse = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
    $banned = array('site.com','site2.com','site3.com');
    $skip = false;
    
    /* The way to do it with the in_array thingy
    if (in_array($parse, $banned)) 
    {
      $skip = true;
    }
    */
    
    foreach ($banned as $line) 
    {
        if (stripos($parse, $line) !== false)
        {
            $skip = true; 
            break;
        }
    }
    
    
    PHP:
     
    exodus, Nov 4, 2007 IP