'If' not working then...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mynameisbob
Forum Newbie
Posts: 2
Joined: Thu Oct 26, 2006 3:33 pm

'If' not working then...

Post by mynameisbob »

I am just learning php and am creating a login page. After clicking submit, login.php handles the form data. login.php loads the account data from a text file and receives the data from the main form fine, but when I check to see if the entered data matches the data from text file, it should display the welcome message otherwise access denied. Even though the variables appear to be equal, it never passes the test. Here is the code I am writing.

Code: Select all

<?php
	$x="0";
	$datasize = "5";
	$success = "0";
	$userbase="users.txt";
	$fh=fopen($userbase,'r');
	while($x<5){
	$users[$x]=fgets($fh);
	$pass[$x]=fgets($fh);
	++$x;
	}
	fclose($fh);
	$username=$_POST['Username'];
	$password=$_POST['Password'];
	echo "$username $password<br>";
	$x = "0";
	while( $x < $datasize && $success=="0" ){
		echo "$users[$x] $pass[$x]<br>";
		if($username==$users[$x]&&$password==$pass[$x])$success="1";
		++$x;
	}
	if($success=="1")echo "Welcome $username<br>";
	else echo "Access denied<br>";
		
?>
text file:

Code: Select all

Adam
1928x
Chris
1235
mike
4912
harry
12000
lynn
00123
Thanks for your help.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

put rtrim () around each fgets()!

Code: Select all

rtrim(fgets($fh));
printf
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

As a quick debug mechanism, try to echo out your compared data instead of comparing to see what PHP is seeing.
mynameisbob
Forum Newbie
Posts: 2
Joined: Thu Oct 26, 2006 3:33 pm

Post by mynameisbob »

Thanks for the help guys.
Post Reply