Login system - users in a text file

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
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Login system - users in a text file

Post 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:

Code: Select all

user,password,access
I'm not sure how I should find the password when the user is posting his username.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Login system - users in a text file

Post 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 :).
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Login system - users in a text file

Post by JKM »

Sorry, but I'm totally blank on this one. I'm not used to reading from text files.

Thanks for your help!
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Login system - users in a text file

Post 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.
Post Reply