|
|
 |
March 2nd, 2006, 03:19 PM
|
#1
|
|
New Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Login Script... something wrong!
Hi!
Okay, first of all, I'm a beginner of PHP... I've thought of a login script for my site, it begins with a form where one has to fill in username and password, and the action file of this form is the following (user authentication is done with a flat file):
Code:
<?PHP $user = $_POST['username']; $pass = $_POST['passwd'];
$file = 'data/userfile.txt'; $filecontents = file($file) or die('Can\'t open/read file'); foreach ($filecontents as $fileline) { $pieces = explode(':', $fileline); if(strtolower($pieces[0]) == strtolower($user) && $pieces[1] == $pass) { echo "YES!!!"; break; } } ?>
As you can see, I just wanted the script to display a message to see if it works. Well, it does, but just if I use the last username and password of the user file... I'm sure it has to be something with the foreach() part, but I just can't figure out what it could be :-(
I guess it is easy for you to find the error, please be patient, I'm really a beginner!!
Thanks in advance if you can help!
Last edited by snowangel; March 2nd, 2006 at 03:25 PM..
|
|
|
March 3rd, 2006, 08:13 PM
|
#2
|
|
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 25
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Login Script... something wrong!
What are the contents of the file?
As far as I can tell the problem is that you are treating the file contents as if they are an array, and as far as I cant tell you have not done anything to convert the data your loading into an array.
|
|
|
March 4th, 2006, 07:41 AM
|
#3
|
|
New Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Login Script... something wrong!
Contents of the file:
Code:
user1:pass1
user2:pass2
user3:pass3
I've used quite a lot print_r(), so here are the contents:
print_r($filecontents):
Code:
Array ( [0] => user1:pass1 [1] => user2:pass2 [2] => user3:pass3 )
print_r($pieces), written in the loop, so there are 3 lines:
Code:
Array ( [0] => user1 [1] => pass1 )
Array ( [0] => user2 [1] => pass2 )
Array ( [0] => user3 [1] => pass3 )
print_r($pieces) at the very end of the script, after the last "}":
Code:
Array ( [0] => user3 [1] => pass3 )
What do you mean by "converting the data I'm loading into an array"? I just can't figure out where the bug is... it works only when I'm using user3's data... maybe because only those data are left after the foreach() loop (look at the last output)?
|
|
|
March 4th, 2006, 07:04 PM
|
#4
|
|
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 25
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Login Script... something wrong!
Well, looks like you have done everything correctly...
I'm going to guess the problem is with the if statement.
put something like this before the if statment, so you can compair the two varables yourself.
Code:
echo '"'.strtolower($pieces[0]).'" == "'.strtolower($user).'" && "'.$pieces[1].'" == "'.$pass.'")';
I have a suspision you might end up with something like
"pass1
"
Where it has a line break after it.
Other then the varable not being equal... I can't figure out what might be wrong.
|
|
|
March 5th, 2006, 12:51 PM
|
#5
|
|
New Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Login Script... something wrong!
Okay, good idea!
This is the output I get:
Code:
"user1" == "user1" && "pass1 " == "pass1""user2" == "user1" && "pass2 " == "pass1""user3" == "user1" && "pass3" == "pass1"
I think I see the problem! after pass1 and pass2, there's a free space! But why? In the userfile, I just pressed enter, there isn't anything after the data.
|
|
|
March 5th, 2006, 06:29 PM
|
#6
|
|
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 25
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Login Script... something wrong!
No idea, but check out trim() it should be able to fix your problem.
|
|
|
March 6th, 2006, 11:27 AM
|
#7
|
|
New Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0
|
Re: Login Script... something wrong!
Okay, stupid question, but I don't know all the functions, anyway, thanks!
It's working!!! Perfectly!!! I love you guys, thank you so much for being so patient and helping soooo much!!
|
|
|
LinkBacks (?)
LinkBack to this Thread: http://webforumz.com/php/5521-login-script-something-wrong.htm
|
| Posted By |
For |
Type |
Date |
| html login script |
This thread |
Refback |
September 12th, 2006 08:39 AM |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|