TEENFRONT Posted June 14, 2008 Share Posted June 14, 2008 Hey Im wanting to build a banned IP list to stop abusive users accessing my site, i know this will only stop some people, but it will do for now lol. I have a text file called banned.txt which is set out simply like this 1.2.3.4 5.6.3.4 1.2.3.4 5.6.3.4 Just with a banned ip on each new line... and i want to put each of the lines of the text file into one global var so that $banned could equal any of the lines from the text file. So i can use if($banned == $user_ip) Im not sure how i go about setting $banned to cover each of the text lines, any help? Very much appreciated.. Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/ Share on other sites More sharing options...
hansford Posted June 14, 2008 Share Posted June 14, 2008 try fopen() fgets() and then iterate through each line of the file trying to find a match with strpos() Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565563 Share on other sites More sharing options...
.josh Posted June 14, 2008 Share Posted June 14, 2008 banning users on the script level may keep them from getting to see your content/use your services, but every time they try to access it, bandwidth and processing resources are used. They could still be free to for instance make a bot to stage a dns attack. Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565564 Share on other sites More sharing options...
thebadbad Posted June 14, 2008 Share Posted June 14, 2008 Yea, listen to Crayon Violent. But you could do it this way: <?php $user_ip = $_SERVER['REMOTE_ADDR']; $banned = file('path/to/file/banned.txt'); foreach ($banned as $ip) { if (trim($ip) == $user_ip) { die('Banned.'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565572 Share on other sites More sharing options...
TEENFRONT Posted June 14, 2008 Author Share Posted June 14, 2008 Hey Thanks thebadbad, will give that a go now. I'll use this to block access to our online game, i still want to show the page, but in place of the game if there banned via ip, then show a banned message. i dont intend to use it to 100% block page access, just certain aspects of the page. Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565600 Share on other sites More sharing options...
TEENFRONT Posted June 14, 2008 Author Share Posted June 14, 2008 Hey Didnt quite do what i wanted. Hopefully someone can help me further <?php $user_ip = $_SERVER['REMOTE_ADDR']; $banned = file('/home/baller8/public_html/codetests/banned.txt'); foreach ($banned as $ip) { if (trim($ip) == $user_ip) { $userban = "YES"; } else { $userban = "NO"; } } if($userban == "YES") { echo "Your banned"; } if($userban == "NO") { echo "Wooo Your not banned"; } ?> my text file is set up like this 1.2.3.4 5.6.7.8 9.10.11.12 etc.. So the above code applied to that text file will overwrite $userban everytime. so it will check line 1 which is 1.2.3.4 if its not my ip it will set $userban to NO. but then on line to 5.6.7.8 is my ip, so it sets $userban to YES, but then it continues to loop to the 3rd line and it isnt my ip so $userban is again set to NO. How, once an ip is found in the banned.txt can i stop the loop and assign a var to say this user is banned, so i simply end up with $userban YES or NO. Many thanks :-) Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565605 Share on other sites More sharing options...
wildteen88 Posted June 14, 2008 Share Posted June 14, 2008 To stop the loop if a match is made, add break; after $userban = "YES"; Also Change if($userban == "YES") { echo "Your banned"; } if($userban == "NO") { echo "Wooo Your not banned"; } to if($userban == "YES") { echo "Your banned"; } else { echo "Wooo Your not banned"; } Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565608 Share on other sites More sharing options...
TEENFRONT Posted June 14, 2008 Author Share Posted June 14, 2008 wildteen88 - genius :-) Works like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565623 Share on other sites More sharing options...
Kieran Menor Posted June 14, 2008 Share Posted June 14, 2008 I would like to add that using that method with a large IP ban list can be slow. I would assume something like this would be faster: <?php $banned = file_get_contents("ipbans.txt"); if(strstr($banned, $_SERVER['REMOTE_ADDR']) !== false) { // user is banned } ?> Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565631 Share on other sites More sharing options...
.josh Posted June 14, 2008 Share Posted June 14, 2008 well if you want to nickel and dime here you should use a db because that's like, what they are good for. Quote Link to comment https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/#findComment-565641 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.