xsuck91 Posted October 21, 2011 Share Posted October 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/ Share on other sites More sharing options...
ManiacDan Posted October 21, 2011 Share Posted October 21, 2011 Wherever you record the IP, do the check right before that. If their IP matches, redirect them or die. Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281152 Share on other sites More sharing options...
xsuck91 Posted October 21, 2011 Author Share Posted October 21, 2011 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281154 Share on other sites More sharing options...
ManiacDan Posted October 21, 2011 Share Posted October 21, 2011 I won't do your work for you, no. The whole thing I described is maybe 8 lines of code. Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281161 Share on other sites More sharing options...
Far Cry Posted October 21, 2011 Share Posted October 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281164 Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2011 Share Posted October 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281175 Share on other sites More sharing options...
ManiacDan Posted October 21, 2011 Share Posted October 21, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281178 Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2011 Share Posted October 21, 2011 I'm really not sure which version added them Dan. I started using them probably about 3 yrs ago. Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281191 Share on other sites More sharing options...
Far Cry Posted October 21, 2011 Share Posted October 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249539-block-ip-with-database/#findComment-1281200 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.