Login Script... something wrong!

This is a discussion on "Login Script... something wrong!" within the PHP Forum section. This forum, and the thread "Login Script... something wrong! are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack (1) Thread Tools
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old Mar 2nd, 2006, 19:19
Junior Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
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):

PHP: Select all

<?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; Mar 2nd, 2006 at 19:25.
Reply With Quote

  #2 (permalink)  
Old Mar 4th, 2006, 00:13
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Reply With Quote
  #3 (permalink)  
Old Mar 4th, 2006, 11:41
Junior Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Login Script... something wrong!

Contents of the file:
Code: Select all
user1:pass1
user2:pass2
user3:pass3
I've used quite a lot print_r(), so here are the contents:

print_r($filecontents):
Code: Select all
Array ( [0] => user1:pass1 [1] => user2:pass2 [2] => user3:pass3 )
print_r($pieces), written in the loop, so there are 3 lines:
Code: Select all
 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: Select all
 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)?
Reply With Quote
  #4 (permalink)  
Old Mar 4th, 2006, 23:04
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
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.
PHP: Select all

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.
Reply With Quote
  #5 (permalink)  
Old Mar 5th, 2006, 16:51
Junior Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Login Script... something wrong!

Okay, good idea!
This is the output I get:
Code: Select all
"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.
Reply With Quote
  #6 (permalink)  
Old Mar 5th, 2006, 22:29
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Login Script... something wrong!

No idea, but check out trim() it should be able to fix your problem.
Reply With Quote
  #7 (permalink)  
Old Mar 6th, 2006, 15:27
Junior Member
Join Date: Mar 2006
Location: Munich
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
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!!
Reply With Quote
Reply

Tags
login, script, wrong

Thread Tools

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

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

LinkBacks (?)
LinkBack to this Thread: http://www.webforumz.com/php-forum/5521-login-script-something-wrong.htm
Posted By For Type Date
html login script This thread Refback Sep 12th, 2006 12:39

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Login Script / form prydie PHP Forum 0 Mar 27th, 2008 06:55
login script mbarr Databases 13 Mar 12th, 2008 03:39
Login Script Problem Aaron1988 PHP Forum 5 Nov 20th, 2006 06:15
login script help Aaron1988 PHP Forum 2 Oct 25th, 2006 15:03
My first PHP script - what's wrong with it? Hudba PHP Forum 1 Jan 30th, 2006 01:53


All times are GMT. The time now is 05:37.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43