Jump to content

Block IP with Database


xsuck91

Recommended Posts

Wherever you record the IP, do the check right before that.  If their IP matches, redirect them or die.

 

can you give me a sample code for this case ?

 

<?php 
//Start output buffer so we can use header(); to redirect. Not needed if you are not using sessions.
ob_start();
$current_ip = $_SERVER['REMOTE_ADDR'];
$query = mysql_query("SELECT blocked_ip FROM tbl_name_here WHERE blocked_ip=$current_ip");
//If IP matches DB's IP, redirect.
if(mysql_num_rows($query) == 1){
header("Location:someplace.php");
} else {
//IP is not blocked, continue.	
}
//End output buffer
ob_end_flush();
?>

 

Code is untested, should work.

Storing IP addresses as strings in a database isn't terribly efficient, nor is running a query only to use mysql_num_rows() to get a count.In that event, a COUNT() query would be the way to go. For storing and retrieving IPs, MySQL has the INET_ATON() and INET_NTOA() functions.

How long have those INET functions been around?  I did an IP-based database in 2008 and they weren't available then (or at least my entire team didn't find them).  If the INET functions weren't available I'd say storing addresses as strings was a good idea UNLESS you need to do math on them (like subnetting).

Storing IP addresses as strings in a database isn't terribly efficient, nor is running a query only to use mysql_num_rows() to get a count.In that event, a COUNT() query would be the way to go. For storing and retrieving IPs, MySQL has the INET_ATON() and INET_NTOA() functions.

 

Thanks for the info.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.