Capturing IP address & blocking IP address

They have: 2 posts

Joined: Jul 2009

I'm using a php script for my web site which has registration, log-in, etc.
When I asked how I might be able to capture the registrants IP address upon registration, I was given this code and this advice:

$userip = $_SERVER[REMOTE_ADDR];
mysql_real_escape_string($userip);

"Remember when you store these values in the database to use these two functions INET_ATON() and INET_NTOA(). One is for inserting the IP and one is for extracting it."

I don't know what INET_ATON() and INET_NTOA() are, but my goal is to be able to have a users IP address just in case he spams my site, then he can be blocked, based on the thinking that typically one user has one computer.

can someone tell me how I can block an Ip address?
Any thoughts/replies/suggestions will be welcome.

He has: 698 posts

Joined: Jul 2005

Maybe I'm just ignorant, but I've never used these functions and I've been able to block IP addresses before.

Just set up an array (these can be extracted from a MySQL database if there will be a lot, and to prevent having to edit the PHP file itself) with all of the IP addresses that need to be banned, and then just redirect from that page if the user's IP address is in the array. Something like this:

<?php
$banned
= array("x.x.x.x","y.y.y.y","z.z.z.z");
if (
in_array($_SERVER['REMOTE_ADDR'],$banned)) {
 
header("Location: differentPage.php");
}
else {
 
//Normal page here
}
?>

Kurtis

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

INET_ATON() and INET_NTOA() are functions that convert IPs to their integer representation.

I wouldn't use them, unless you're storing thousands of IPs.

I would use kazimmerman's method for blocking IPs. There's no reason to complicate things with those INET functions.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

kazimmerman's method is best if just blocking a few IP's. If you want that list to dynamically update, you may want to store them in an external text file or database.

If you want to block a range of IP's, then you may want to use the ip2long() function.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.