This is a discussion on Breaking out of a foreach/function within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I'm working on this (assumed very simple) function that reads data from a textfile and then performs a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
Laiverd.COM wrote:
> Darn, you are really good ;) I did some further research today, and > discovered that exactly what you describe was indeed the problem. Meanwhile > solved using your suggestion. Thanks a million. > > John > > No problem. I've been parsing text files for a while; it's something you just get used to. ;) -- cb |
![]() |
| Thread Tools | |
| Display Modes | |
|
|