Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.