Saturday, 15 February 2014

php - Query to return rows that match a block of ip addresses? -


I'm trying to block some IP addresses and IP categories by taking certain action. Right now, I was blocked by SELECT * where IP = $ _ server ['REMOTE_ADDR'] and if - & gt; Number returns & gt; 0, I redirect the user.

It works with full IP address, but if I put 199.90. *. * For example, in the database, how can I match him? Also, what would be the best way to store the IP in the database?

To answer your question, use like : Where to replace $ _SERVER ['REMOTE_ADDR'] from

  SELECT * blocking (like IP, '*', '%')   

like Wildcard is '*' instead of '*' , so it changes the asterisks. Percentage I think you're actually '199.199 %%. % '' , but the above version is for clarity.

Although it technically resolves your problem, it has other issues because MySQL can not use any index for these comparisons (see). The effect depends on how large the infrared table is, if it has 100 rows, then this can not be a problem, if it has 100,000, then it is probably.

To use the wildcard for the blocked table, you must be in the form of a FromIP and TOIP column. Then something will be stored as "199.199. . " just "199.199.000.000" and "199.199.999.999" The query will be:

  Where between INIP and TOIP, $ _SERVER ['REMOTE_ADDR']   

and you will have an indicator on blocked ( ) IIP, TOIP).

No comments:

Post a Comment