Jump to content

Block IP with Database


xsuck91

Recommended Posts

how to block someone from visiting my page using ip adress ?

this is the case

if someone come to my site then we record the ip address and put it into database.

when the visitor come again with the same IP they cannot see my page (block them) and redirect it to somewhere page.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.