Jump to content

Recommended Posts

Hey guys,

 

I have a simple (well it should be anyway) problem with the comparison of two IP addresses. I'm trying to implement an IP ban for a mini forum I've got going. I added my own IP to the table in order to test it, but still no luck.

 

$ip = $_SERVER['REMOTE_ADDR'];
$qry = "SELECT IP FROM BANNED WHERE IP = '$ip'";
$result = mysql_query($qry);

if(mysql_num_rows($result) > 0 )
{
header('Location:http://*************.com/ban.php');
}

 

Table is:

 

BANNED

ID INT(24) AUTO_INCREMENT NOT NULL

IP VARCHAR(50) NOT NULL

 

My IP is in there.

 

Any ideas what might be the problem? I know it's probably something simple.

Try

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$qry = "SELECT * FROM `BANNED` WHERE `IP` = '" . mysql_real_escape_string($ip) . "'";
$result = mysql_query($qry);

if(mysql_num_rows($result) > 0 )
{
header('Location:http://*************.com/ban.php');
}
?>

 

Else do some error checking.

 

And just ask if you want me to explain anything.

Solved guys. I found the solution. I converted all IP's to a long number with the ip2long function.

 

$ip = $_SERVER[REMOTE_ADDR];
$ip_long = ip2long($ip);

 

I then inserted it into a row of type int(11).

 

Hope this example helps those who might be taking the same journey as me.

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.