Reply to Re: Breaking out of a foreach/function

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация