problem reading from a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Roger22
Forum Newbie
Posts: 8
Joined: Tue Oct 27, 2009 12:31 am

problem reading from a file

Post by Roger22 »

Hello,
I have a text file where i store the lines as

user|password|question|answer

I'm a beginner, now i just wanna work with files, so i wanna make a login system which stores the user, pass, secret question and answer, in a file, not in a database.

i have the code

Code: Select all

 
$fp=fopen($root."\user\users.txt","r");
while(!feof($fp)){
$contents=fread($fp,filesize($root."\user\users.txt"));
fclose($fp);
$info=explode("|",$contents);
$username1=$info[0];
$password1=$info[1];
}
 
When i try to enter the username and pass, it works just regarding to 1st line of the file, the user and pass from other lines does not work.. Why?
How can i check line by line if a user has entered the correct pass?

Thanks!
dhenick
Forum Newbie
Posts: 19
Joined: Tue Oct 20, 2009 10:46 am
Location: Yogyakarta, Indonesia
Contact:

Re: problem reading from a file

Post by dhenick »

i think you must explode the file twice.
first by line or by username and second by |
Iainzor
Forum Newbie
Posts: 4
Joined: Mon Oct 26, 2009 10:54 am

Re: problem reading from a file

Post by Iainzor »

You need to use file()

Code: Select all

 
$lines = file('/path/to/file');
foreach($lines as $line) {
    list($user, $password, $question, $answer) = explode("|", $line);
    echo $user;
}
 
Roger22
Forum Newbie
Posts: 8
Joined: Tue Oct 27, 2009 12:31 am

Re: problem reading from a file

Post by Roger22 »

Iainzor wrote:You need to use file()

Code: Select all

 
$lines = file('/path/to/file');
foreach($lines as $line) {
    list($user, $password, $question, $answer) = explode("|", $line);
    echo $user;
}
 
this does not work, when i write echo $user, nothing is shown..

Code: Select all

 
$lines=file("C:\\xampp\\htdocs\\EARAW\\Lab3 - Autentificare\\user\\users.txt");
    foreach ($lines as $lines){
        list($user, $pass, $question, $answer)=explode("|", $line);
    }
    echo $user;
 
Roger22
Forum Newbie
Posts: 8
Joined: Tue Oct 27, 2009 12:31 am

Re: problem reading from a file

Post by Roger22 »

dhenick wrote:i think you must explode the file twice.
first by line or by username and second by |
with my initial code, how is possible this? to split once by line, and once by "|" ?

Thanks
Roger22
Forum Newbie
Posts: 8
Joined: Tue Oct 27, 2009 12:31 am

Re: problem reading from a file

Post by Roger22 »

no one can help me?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: problem reading from a file

Post by Mark Baker »

Why are you so pushy for an immediate answer, barely 1 1/2 hrs after you posted your thread, you've already received sevral answers, and are still complaining that nobody is helping you.

Why are you echoing $user outside of the foreach loop when Iainzor's code echoed inside the loop? All it takes is a blank last line of the file, and you'll see nothing unless you do what was suggested.
Post Reply