ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm making a simple php script to read a list of IPs from a file into an array, and ban - exit() - the user if the ip matches. When the IPs are manually typed in the script, it works, i.e.
$banned_ip[] = '1.2.3.4';
$banned_ip[] = '5.6.7.8';
...
But when i stick the addresses in a file, it doesn't seem like its doing the if check.. Here's my code
Code:
<?php
$banned_ip = file("banlist.txt");
$ip = $_SERVER['REMOTE_ADDR'];
// Ban if IP matches.
if(in_array($ip,$banned_ip)) {
echo "You have been banned!";
exit();
}
//else continue...
echo "success";
?>
where banlist.txt contains an ip address per line. If i add echo $banned_ip[5]; or echo $ip; i get the values i want... so i guess the problem is with the if(in_array(......)
Originally posted by msound lets say:
/www/html/banlist.txt
and you're in /www/html/forum/template, the following should work:
Code:
$out = `grep $ip ../../banlist.txt';
Ignoring the typo ' , But i've already tried it. It just won't read banlist.txt at all. Even if its in the current directory or any other. All i get is 'success' even if i stick my ip address in the list, delete the list, rename it, whatever =/
is 192.168.0.69 equal to 192.168.0.69 ? yes!
You have been banned
I guess i could stick to this script, although i'm itching to know why the other one refused to work in certain cases.
edit: hmm, i'd also want to stick an exit(); code with the banned part.
Probably in the if condition, before the break; would be the best place
edit: 2
an addition
Code:
...
...
...
...
...
is 211.46.196.124 equal to 192.168.0.69 ? no
is 193.194.84.198 equal to 192.168.0.69 ? no
is 200.30.146.150 equal to 192.168.0.69 ? no
is 200.205.145.70 equal to 192.168.0.69 ? no
is 209.161.5.194 equal to 192.168.0.69 ? no
is 62.245.167.6 equal to 192.168.0.69 ? no
is 204.83.150.196 equal to 192.168.0.69 ? no
is 207.234.208.66 equal to 192.168.0.69 ? no
is equal to 192.168.0.69 ? no
SUCCESS
I should have been more precise
I didn't mean to replace the test :
if(strcmp($ip, $buffer) == 0)
with :
if(empty($buffer))
As you pointed out, the script originally tested the empty lines
so I thought that before the test :
if(strcmp($ip, $buffer) == 0)
We could add :
if(empty($buffer)) continue;
So the loop skips the empty lines
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.