Breaking out of a foreach/function

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-22-2007
Laiverd.COM
 
Posts: n/a
Default Breaking out of a foreach/function

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


Reply With Quote
  #2 (permalink)  
Old 03-23-2007
Christoph Burschka
 
Posts: n/a
Default Re: Breaking out of a foreach/function

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
Reply With Quote
  #3 (permalink)  
Old 03-23-2007
Laiverd.COM
 
Posts: n/a
Default Re: Breaking out of a foreach/function

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


Reply With Quote
  #4 (permalink)  
Old 03-26-2007
Christoph Burschka
 
Posts: n/a
Default Re: Breaking out of a foreach/function

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:39 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0