Jump to content

[SOLVED] Silly problem with comparison of IP


waynew

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.

Archived

This topic is now archived and is closed to further replies.

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