Page 1 of 1

problem reading from a file

Posted: Tue Oct 27, 2009 12:34 am
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!

Re: problem reading from a file

Posted: Tue Oct 27, 2009 12:41 am
by dhenick
i think you must explode the file twice.
first by line or by username and second by |

Re: problem reading from a file

Posted: Tue Oct 27, 2009 12:45 am
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;
}
 

Re: problem reading from a file

Posted: Tue Oct 27, 2009 1:22 am
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;
 

Re: problem reading from a file

Posted: Tue Oct 27, 2009 1:22 am
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

Re: problem reading from a file

Posted: Tue Oct 27, 2009 1:50 am
by Roger22
no one can help me?

Re: problem reading from a file

Posted: Tue Oct 27, 2009 5:06 am
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.