A few options on the tables - you can store the IP as a human-readable string (4 dotted bytes) or in it's native long number that cidr_match uses. Assuming we've stuck with human readable (to reuse cidr_match
from the linked page)
function testIP($ip=$_SERVER['REMOTE_ADDR']) {
//Returns TRUE - valid, FALSE - denied
/* Get the data from the database and return it in rows
* this could be real-time retrieved, or pulled into an array
* first
* assumes: deny_ranges(ip VARCHAR(45) not null,mask INT not null default 0)
* eg: $denyips= get "SELECT ip,mask FROM deny_ranges" from database
*/
foreach($denyips as $row) {
if ($row["mask"]==0) {
//No need to use overhead of CIDR_MATCH
//Exact match - reject
if ($row["ip"]==$ip) return false;
} elseif (cidr_match($ip,$row["ip"]."/".$row["mask"])==true) {
//In denied range - reject
return false;
}
}
//Got through all rejected ranges+ips - it's good
return true;
}
(You can find cird_match
on the other post)