|
Posted by Christoph Burschka on 03/23/07 07:24
Laiverd.COM schrieb:
> Hi,
>
> I'm working on this (assumed very simple) function that reads data from a
> textfile and then performs a check for a valid login. Problem is that - as I
> see it - the function should immediately return true if a valid login is
> found, but it doesn't. This has been staring me in the face for the second
> evening nou, and I don't see it.
> It seems like the foreach loop is never stopped, and goes all the way
> thorugh, while it shouldn't. Forgive me if I'm simply missing the obvious ;)
> I also tried to end the loop with a break, and tried a non-strict
> comparison. Both to no avail.
>
> Of course I'm open to any way this function can be improved, but am most of
> all curious as to why it wouldn't work now.
>
>
> This is the function:
> function isValidLogin()
> {
> global $passFile;
> // get lines of file into array
> $fp = file($passFile);
> // loop through the array
> // print_r($fp);
> foreach($fp as $line){
> // turn each array element into an array of its own
> $words = explode(",",$line);
> //print_r($words)."<br />";
> if($words[0]==$_POST['loginname']){
> if (($words[1]==$_POST['password'])){
> echo "<br />login correct";
> return true;
> }
> }
> }
> echo "login incorrect";
> return false;
> }
>
> Thanks for any input,
> John
>
>
Is it returning false when the login is valid?
If so, this is probably because of the linebreak at the end of each
line. The way it is now, it ends up attached to the password value,
which then messes up the check.
So if you added this line:
$line=rtrim($line); // removes trailing whitespaces
just after the foreach statement, this problem might be solved.
--
Christoph Burschka
Navigation:
[Reply to this message]
|