waynew Posted May 21, 2008 Share Posted May 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106606-solved-silly-problem-with-comparison-of-ip/ Share on other sites More sharing options...
DeanWhitehouse Posted May 21, 2008 Share Posted May 21, 2008 try this if($result == $ip) { header('Location:http://*************.com/ban.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/106606-solved-silly-problem-with-comparison-of-ip/#findComment-546391 Share on other sites More sharing options...
waynew Posted May 21, 2008 Author Share Posted May 21, 2008 No, didn't work I'm afraid. Thanks for trying to help though. Quote Link to comment https://forums.phpfreaks.com/topic/106606-solved-silly-problem-with-comparison-of-ip/#findComment-546393 Share on other sites More sharing options...
thebadbad Posted May 21, 2008 Share Posted May 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106606-solved-silly-problem-with-comparison-of-ip/#findComment-546398 Share on other sites More sharing options...
BlueSkyIS Posted May 21, 2008 Share Posted May 21, 2008 are you testing this on a local machine or a remote server? if it's local, chances are your IP address is not your public IP address. it's probably something more like 127.0.0.1 locally. Quote Link to comment https://forums.phpfreaks.com/topic/106606-solved-silly-problem-with-comparison-of-ip/#findComment-546418 Share on other sites More sharing options...
MadTechie Posted May 21, 2008 Share Posted May 21, 2008 echo $ip; to check what ip is being used then check in the database Quote Link to comment https://forums.phpfreaks.com/topic/106606-solved-silly-problem-with-comparison-of-ip/#findComment-546439 Share on other sites More sharing options...
waynew Posted May 21, 2008 Author Share Posted May 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106606-solved-silly-problem-with-comparison-of-ip/#findComment-546645 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.