LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2005, 10:01 AM   #1
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Rep: Reputation: 45
php read from file and compare.


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(......)
 
Old 07-07-2005, 10:07 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Maybe in the array, there are some hidden characters, when read from a file? Check into that.
 
Old 07-07-2005, 10:23 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You could also use :
PHP Code:
$ip $_SERVER['REMOTE_ADDR'];
$out=`grep $ip banlist.txt`;

if(!empty(
$out)) {
    echo 
"You have been banned!";
    exit();

 
Old 07-07-2005, 10:30 AM   #4
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Excellent! =))

that did the job keefaz. Many thanks =) And i'm currently testing the bash script, will post about it asap.
 
Old 07-10-2005, 04:24 AM   #5
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Hmm, it doesn't seem to work 100% when i add it to any of my html/php files...

If i stick it by itself in a test.php, it works. But if i add it to any other file, it just says success, weather i stick my ip in the list or not.

PHP Code:
    $ip $_SERVER['REMOTE_ADDR'];
    
$out=`grep $ip banlist.txt`;
    if(!empty(
$out)) {
        echo 
"You have been banned!";
        exit();
    }
    echo 
$out;
    echo 
"success"
I stuck echo $out; to as a check, and its empty (with or witout my ip in the list). Also, how would i open banlist.txt if it were 2 folders up?

Many thanks.
 
Old 07-11-2005, 10:47 PM   #6
msound
Member
 
Registered: Jun 2003
Location: SoCal
Distribution: CentOS
Posts: 465

Rep: Reputation: 30
lets say:
/www/html/banlist.txt
and you're in /www/html/forum/template, the following should work:
Code:
$out = `grep $ip ../../banlist.txt';
 
Old 07-12-2005, 03:32 AM   #7
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Quote:
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 =/
 
Old 07-12-2005, 07:46 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
It is weird...
Here is a deeper test :
PHP Code:
// give the full path to banlist.txt, 
// just to make sure...

$banfile '/home/www/banlist.txt'
$ip $_SERVER['REMOTE_ADDR'];
$iptest false;

if(empty(
$ip)) die('ip is empty...');

$fd fopen($banfile'r'
    or die(
"Could not open $banfile to read in");

while(
$buffer fgets($fd,4096)) {
    
$buffer trim($buffer);
    echo 
"is $buffer equal to $ip ? ";
    if(
strcmp($ip$buffer) == 0) {
        echo 
"yes! <br>";
        
$iptest true;
        break;
    }
    echo 
"no <br>";
}

fclose($fd);

echo (
$iptest) ? "You have been banned" "SUCCESS"
 
Old 07-12-2005, 11:45 AM   #9
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Sorry for the late reply,

Heh, ok, this one seems to work.
Code:
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
Messy

Last edited by xushi; 07-12-2005 at 11:49 AM.
 
Old 07-12-2005, 01:24 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
quick fix :
PHP Code:
while($buffer fgets($fd,4096)) {
    
$buffer trim($buffer); 
    if(empty(
$buffer)) continue;
 ... 
 
Old 07-13-2005, 09:43 AM   #11
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Quote:
Originally posted by keefaz
quick fix :
PHP Code:
while($buffer fgets($fd,4096)) {
    
$buffer trim($buffer); 
    if(empty(
$buffer)) continue;
 ... 
Hmm... nope..
That fix gives me,
is 192.168.0.69 equal to 192.168.0.69 ? no
SUCCESS

PHP Code:
$banfile '/full/path/to/banlist.txt';
$ip $_SERVER['REMOTE_ADDR'];
$iptest false;

if(empty(
$ip)) die('ip is empty...');

$fd fopen($banfile'r')
    or die(
"Could not open $banfile to read in");

while(
$buffer fgets($fd,4096)) {
    
$buffer trim($buffer);
    echo 
"is $buffer equal to $ip ? ";
    
//if(strcmp($ip, $buffer) == 0) {
    
if(empty($buffer)) {
       echo 
"yes! <br>";
        
$iptest true;
        exit(
'Banned');
        break;
    }
    echo 
"no <br>";
}

fclose($fd);
echo (
$iptest) ? "You have been banned" "SUCCESS"
edit, didn't think you literally meant 'continue;', but even if i changed the code to the following, it doesn't work properly
PHP Code:
$banfile '/full/path/to/banlist.txt';
$ip $_SERVER['REMOTE_ADDR'];
$iptest false;

if(empty(
$ip)) die('ip is empty...');

$fd fopen($banfile'r')
    or die(
"Could not open $banfile to read in");

while(
$buffer fgets($fd,4096)) {
    
$buffer trim($buffer);
    if(empty(
$buffer)) continue;
}

fclose($fd); 

Last edited by xushi; 07-13-2005 at 09:48 AM.
 
Old 07-14-2005, 01:10 PM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP - read a random line from a file? Erik Thorsson Programming 3 12-10-2004 10:31 AM
how to compare 2 text files by using php code antony_csf Programming 3 10-14-2004 05:52 AM
php read file creation, modified date problem antony_csf Programming 3 08-18-2004 06:46 AM
PHP Calculate, Compare.. Pts Gerardoj Programming 8 05-26-2004 12:54 AM
php date compare omarswan Programming 2 10-02-2002 04:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration