env3rt Posted October 1, 2007 Share Posted October 1, 2007 I want to make a simple IP ban with an IP ban list which can be added on to using append. It is not working because $IP has to be equal to whatever is in the testFile.php, however I want the testFile.php text to be treated as code so it will work like this if ( ($IP == "77.77.777.7" OR $IP == "99.99.999.9") ) etc.. <?php session_start(); $IP = $_SERVER['REMOTE_ADDR']; $myFile = "testFile.php"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); if ( ($IP == $theData) ) { echo "You have been banned."; } else { //do stuff } ?> inside testFile.php is "77.77.777.7" OR $IP == "99.99.999.9" Please help Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/ Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 Why not just do this? Your text file 77.77.777.7 99.99.999.9 55.55.555.5 <?php $lines = file('ips.txt'); foreach ($lines as $line) { $ips[] = $line; } $ip = "55.55.555.5"; if (in_array($ip, $ips)){ echo "You are banned!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359558 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 It's not working.. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359563 Share on other sites More sharing options...
MmmVomit Posted October 1, 2007 Share Posted October 1, 2007 Make a table in your database of banned IPs. When someone tries to access your site, try to look up their IP in the table. If the number of rows returned is zero, allow access. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359565 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 I just tested it, works perfectly. <?php $lines = file('ips.txt'); foreach ($lines as $line) { $ips[] = $line; } $ip = "55.55.555.5"; if (in_array($ip, $ips)){ echo "You are banned!"; } else { echo "You are not banned."; } ?> I put an else statement in there to help you out. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359566 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 It works but then if I put my IP at the third line it doesnt. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359567 Share on other sites More sharing options...
MmmVomit Posted October 1, 2007 Share Posted October 1, 2007 <?php foreach ($lines as $line) { $ips[] = $line; } ?> The quoted loop seems unnecessary. Why not just do this? <?php if(in_array($ip, $lines) ?> Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359568 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 Make a table in your database of banned IPs. When someone tries to access your site, try to look up their IP in the table. If the number of rows returned is zero, allow access. I'm really a newbie at this I've never used tables, does that involve PHPmyadmin or whatever? Because I'm just uploading php files from my computer. Could you tell me how to do it Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359573 Share on other sites More sharing options...
MmmVomit Posted October 1, 2007 Share Posted October 1, 2007 I just assumed you were using a database (MySQL is the most common choice). What is your website supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359578 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 I'm just testing an IP ban right now. New code: <?php $ip = $_SERVER['REMOTE_ADDR']; $lines = file('lol.txt'); if(in_array($ip, $lines)) { echo "You are banned!"; } else { echo "You are not banned."; } ?> The only problem is when you do multiple lines of ips in the text files it stops working. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359579 Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Your over complicating things. <?php $lines = file('lol.txt'); $ip = $_SERVER['REMOTE_ADDR']; if (in_array($ip, $lines)){ echo "You are banned!"; } else { echo "You are not banned."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359582 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 Lol I have that exact code. I was editing it so I suppose you first read it while it was unedited then posted the same thing I had edited into it Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359585 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 <?php foreach ($lines as $line) { $ips[] = $line; } ?> EDIT - Thorpe beat me to it. The quoted loop seems unnecessary. Why not just do this? <?php if(in_array($ip, $lines) ?> Your right! Sorry about that. <?php $ips = file('ips.txt'); $ip = "55.55.555.5"; if (in_array($ip, $ips)){ echo "You are banned!"; } else { echo 'Not Banned'; } ?> I just tested that code with multiple lines in the text file, it worked just fine. Edit - Thorpe beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359586 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 $ip = "55.55.555.5" I want the ip to be different for each person... If you say the ip is 55.55.555.5 every single time everyone would be banned, if you banned that ip. Try using $ip = $_SERVER['REMOTE_ADDR']; it won't work. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359587 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 Sorry, I kept that in there for testing it. Use $_SERVER['REMOTE_ADDR'] edit - What isn't working about it? Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359588 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 I did try $ip = $_SERVER['REMOTE_ADDR']; and I entered my own ip on the .txt file and it didn't work unless there was only one line on the .txt file. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359591 Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 $ip = "55.55.555.5" I want the ip to be different for each person... If you say the ip is 55.55.555.5 every single time everyone would be banned, if you banned that ip. Try using $ip = $_SERVER['REMOTE_ADDR']; it won't work. What? $_SERVER['REMOTE_ADDRESS'] gets the ip of the current client. Though this can also be tricked, but thats a whole other issue. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359592 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 I know it does that so if their IP is one that is in the file it will ban them. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359593 Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 So... whats your problem then? Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359594 Share on other sites More sharing options...
MmmVomit Posted October 1, 2007 Share Posted October 1, 2007 If you're developing this site on your own machine, try putting the loopback IP in the banned IP file, '127.0.0.1'. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359595 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 If you use $ip = $_SERVER['REMOTE_ADDR']; and write multiple IPs in the .txt it won't work. But if you write $ip = "55.55.555.5"; it will work. I'm hosting it on a free php hoster but I'm just uploading php files not using mySQL or anything I want $ip = $_SERVER['REMOTE_ADDR']; to work with multiple lines of ips. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359597 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 Echo $ip out and see what it gives you, just to make sure you are using the right one. Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359599 Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Is the output of.... <?php echo $_SERVER['REMOTE_ADDR']; ?> what you expect your ip to be? Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359600 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 Yes.. And I found out something new, it works with multiple lines but only if the ip you want to work is at the bottom of the list example: Doesnt work 45.45.455.5 88.88.888.8 <--- ip u want to work 54.45.455.4 45.54.455.4 Works 45.45.455.5 88.88.888.8 <--- ip u want to work but I want all the ips to work. Is it working for anyone else? Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359605 Share on other sites More sharing options...
env3rt Posted October 1, 2007 Author Share Posted October 1, 2007 plz help Quote Link to comment https://forums.phpfreaks.com/topic/71434-solved-help-with-ip-ban-simple/#findComment-359614 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.