473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with simple PHP login Script

I'm new to PHP and I'm trying to write up a simple PHP login page
script to authenticate users from a flat file. I've managed to make
most of it work, except that it only accepts the last user on the
user/password file, and I'm not sure why. (I.e. the user.txt file is:

user1:pass1
user2:pass2
user3:pass3

and it only works for user3:pass3)

Here is my code:

<?php

$auth = false; // Assume user is not authenticated

if (isset( $_POST['txtUsername'] ) && isset( $_POST['txtPassword'] )) {

// Read the entire file into the variable $file_contents

$filename = 'user.txt';
$fp = fopen( $filename, 'r' );
$file_contents = fread( $fp, filesize( $filename ) );
fclose( $fp );

// Place the individual lines from the file contents into an array.

$lines = explode ( "\n", $file_contents );

// Split each of the lines into a username and a password pair
// and attempt to match them to $PHP_AUTH_USER and $PHP_AUTH_PW.

foreach ( $lines as $line ) {

list( $username, $password ) = explode( ':', $line );

if ( ( $username == $_POST['txtUsername'] ) &&
( $password == $_POST['txtPassword'] ) ) {

// A match is found, meaning the user is authenticated.
// Stop the search.

$auth = true;
break;

}
}

}

if (! $auth ) {

?>

<h1>Login</h1>

<form name="form" method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
<p><label for="txtUsername">Username:</label>
<br /><input type="text" title="Enter your Username" name="txtUsername"
/></p>

<p><label for="txtpassword">Password:</label>
<br /><input type="password" title="Enter your password"
name="txtPassword" /></p>

<p><input type="submit" name="Submit" value="Login" /></p>

</form>

<?php

} else {

echo '<P>You are authorized!</P>';
}

?>

Mar 24 '06 #1
1 1822
ca********@cbamedia.com wrote:
I'm new to PHP and I'm trying to write up a simple PHP login page
script to authenticate users from a flat file. I've managed to make
most of it work, except that it only accepts the last user on the
user/password file, and I'm not sure why. (I.e. the user.txt file is:


It's probably because the text file was created in Windows, where the
line separator is \r\n. When you do the explode on \n, all the elements
except the last will have a \r attached at the end, thus not matching
what was entered.

Use trim() on $username should fix the problem.

Mar 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bartosz Wegrzyn | last post by:
I need help with sessions. I createt set of web site for nav with authorization. first I go into main.php which looks like this: <?php //common functions include_once '../login/common.php';...
4
by: Alphonse Giambrone | last post by:
I have a simple login page that has two text boxes, two requiredfieldvalidators, a couple of labels and a button. It has been reduced down to almost nothing to troubleshoot the below problem. ...
2
by: Tom | last post by:
I hope someone can help me figure out what's going on here. I've re-read the section on sessions at php.net and Googled this high and low but I haven't found anything that quite explains my...
4
by: hrawada | last post by:
Hello everyone, I have built a simple website with a simple login page and another page that is the destination page after logging in. The website works fine on my machince however when I...
2
by: Sasquatch | last post by:
I'm having trouble creating a simple login page using the asp:login control. I followed some instructions in a WROX book, "Beginning ASP.NET 2.0," and the instructions are very straight forward,...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
5
by: Tom | last post by:
I have a function that restricts access to a page to logged in users. When a user who isn't logged in goes to the page, it will dynamically generate a login form. I'm trying to use it in...
1
by: quill | last post by:
Hi I am making a chatroom script and it appears that the problem seems to be that my setTimeout's are conflicting. The logic is as follows: Run a login check every x seconds Run a trigger...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.