Page 1 of 1
Login system - users in a text file
Posted: Wed Sep 16, 2009 6:23 pm
by JKM
Hi there,
I want to make an easy login system (with sessions) where the users are stored in a text file. It should be like this:
I'm not sure how I should find the password when the user is posting his username.
Re: Login system - users in a text file
Posted: Wed Sep 16, 2009 6:31 pm
by Mirge
I'd much rather use MySQL, but if you have to use a flatfile DB, I'd read the file line by line until I encountered the username.
For example:
Code: Select all
<?php
$fp = fopen("test.txt", "r") or die("Unable to open file!");
while(!feof($fp)) {
$line = trim(fgets($fp));
if(empty($line)) { continue; }
list($username, $password, $access) = explode(",", $line);
print "Username: $username - Password: $password - Access: $access\n";
}
?>
Outputs:
Username: user1 - Password: pass1 - Access: 1
Username: user2 - Password: pass2 - Access: 1
Username: user3 - Password: pass3 - Access: 1
And test.txt contains:
Code: Select all
user1,pass1,1
user2,pass2,1
user3,pass3,1
Surely you can figure out what to do from there

.
Re: Login system - users in a text file
Posted: Wed Sep 16, 2009 7:34 pm
by JKM
Sorry, but I'm totally blank on this one. I'm not used to reading from text files.
Thanks for your help!
Re: Login system - users in a text file
Posted: Wed Sep 16, 2009 7:40 pm
by Mirge
JKM wrote:Sorry, but I'm totally blank on this one. I'm not used to reading from text files.
Thanks for your help!
You don't have to be familiar with reading from text files. I just gave you the code that reads the text file.
All you have to do is compare variables... and act on that.