Really simple flat file login/authentication

For first try to not ask me why don’t I use database. It’s part of my goal to do a sorta useful site engine using only files.

I have already my own method of registering users and checking if their inputted username and password is correct. Unfortunately I’m still learning and I don’t have luck with session variables and cookies. I need only:

  • login for session
  • remember password in cookie
  • some kind of remembering that you’re still logged in even after 10 minutes
  • authentication e.g. displaying “blahblahblah” instead of content if you’re not logged in

Everytime I search for this kind of help I see only complete login/register scripts that can’t be customized to my needs, so I’m finally asking other guys on the internet about these basic things I can just add to code :]

here is a basic script that you can add to and adapt as you learn new stuff…

It stores the user details in a text file called “users.txt”
to create a session just add $_SESSION[‘firstname’] = $_POST[‘firstname’]; where it says in script…
and session_start(); to the top of each page.
// read data
$file = “users.txt”;
// Attempt to open the file
if($fp = fopen($file, “rb”))
// If opened, loop through the file until reach the end
while (!feof($fp) )
// Get each line individually
$line_of_text = fgets($fp, 1024);
// Trim any spaces off
$text[] = trim(stripslashes(str_replace("\t", ’ ', $line_of_text)));
// Close the file

	// check if the user is valid
	if(in_array($_POST['firstname'] . ' ' . $_POST['lastname'], $text))
		// login successful
		echo '<p>You logged in successfully';
		// do something here like set a cookie or a session
	else // user is not valid
		// login UNsuccessful
		echo '<p>Your log in failed!';
		// do something here like redirect the user.


Lastname :


Hope this helps get you on your way

Red :wink:

to check if the user is logged in using sessions = [php]
// yes the user is logged in.
// put your members only code here.
// the user is not logged in
// show a message or non member content here.

Sponsor our Newsletter | Privacy Policy | Terms of Service