Hey all, thanks for reading my little problem, I believe I'm just doing something stupid and not seeing it... 🙂

I have a function using preg match and eregi to ban a user by IP or partial IP

Let's say current user / view: $REMOTE_ADDR = 24.4.20.120

 
      $bannedIPArr[]='81.177.';   // ban all start from 123.  24.4.20
      $bannedIPArr[]='24.4.20.';   // ban all start from 123.  24.4.20
      print "Banned IParr = ";
      print_r($bannedIPArr)."<br>";


   $banfun = BannedIP1( $bannedIPArr, $REMOTE_ADDR  );
    if ( $banfun )
    echo "banned function = True";
    else echo "banned function = false";

FUNCTION BannedIP1( $bannedIPArr, $REMOTE_ADDR )
{
      $banned = FALSE;


    FOREACH ( $bannedIPArr AS $value ) {

         echo "<br>"."foreach val ".$value."<br>";

         IF ( eregi( $REMOTE_ADDR, $value ) )
             $banned = TRUE;

         IF ( eregi( $REMOTE_ADDR, $value ) )
              echo "eregi = true"."<br>";
         ELSE echo "eregi = false"."<br>";

         IF ( preg_match( "/^$REMOTE_ADDR/", $value ) ) {
              $banned = TRUE;
         }

         IF ( preg_match( "/^$REMOTE_ADDR/", $value ) )
              echo "preg = true"."<br>";
         ELSE echo "preg = false"."<br>";

    } # [ END ] FOREACH ( $bannedIPArr AS $REMOTE_ADDR )

RETURN $banned;
}

BannedIP1() always returns false?
the preg echo is always false
the eregi echo is alsways false

can anyone see what I"m doing wrong...I just don't see why it's not working

    here is a little function that does the above and works ok

    <?php
    
    $banned = array('81.77.', '24.4.20');
    
    $ip = '24.4.20.1';
    
    var_dump(BannedIP1($banned, $ip));
    
    function BannedIP1($banarray, $userip)
    {
      foreach($banarray as $ban) {
        if (preg_match('/^' . preg_quote($ban) . '/', $userip) == true) return true;
      }
    
      return false;
    }
    
    ?>
    

    i think part of the issue is the ip's have dots in them which need to be escaped and also you were putting the full ip as the regular expression and the partial as the search targed. the search ip should be the regex and the full user ip should be the string getting searched.

      I'm sorry I feel stupid but I tested it like this, as you said the I had $value and $REMOTE_ADDR reversed.... 😕
      I don't know what the hell I was thinking but thanks very much for the help

      FUNCTION BannedIP1( $bannedIPArr, $REMOTE_ADDR )
      {
            $banned = FALSE;
      
      
          FOREACH ( $bannedIPArr AS $value ) {
      
      
              # Do not use ereri for this type of function it is way to slow!
              # IF ( eregi( $value, $REMOTE_ADDR ) )
              #     $banned = TRUE;
      
               IF ( preg_match( "/^$value/", $REMOTE_ADDR ) ) {
                    $banned = TRUE;
               }
      
           } # [ END ] FOREACH ( $bannedIPArr AS $REMOTE_ADDR )
      
      RETURN $banned;
      } 
      
      

        Another way of writing it would be:

        function BannedIP1( $bannedIPArr, $REMOTE_ADDR )
        {
        	foreach ( $bannedIPArr AS $value )
        	{
        		if ( preg_match( "/^$value/", $REMOTE_ADDR ) )
        		{
        			return true;
        		}
        	}
        	return false;
        }
          Write a Reply...