Jump to content

Recommended Posts

I am really new to PHP and my first projects are working with PHP and txt files - I am not yet using MYSQL. I created a registeredusers.txt file that contains user names, emails, and passwords. The users will then fill in a login in page with their email address and password. This form will them process so that another PHP file will open and be available to them. I am having trouble writing the login script that will check the txt file to confirm they have a email address and password on file.  I am being told to write it in a function that will be placed in a pinc file that will be included on my loginprocess page.

 

Are there any tutorials that help with writing login functions that work with txt files rather than databases or can someone help me write a function that will work?

 

I appreciate any help.

Link to comment
https://forums.phpfreaks.com/topic/42841-login-help/
Share on other sites

Sure -

 

It is being stored like this firstname:lastname:email:password

 

Here is the code that is putting the data into the registeredusers file:

 

<?php

 

//Escapes and enquotes variables

function clean($var){

    return trim(addslashes($var));

}

//Variable declaration

 

    $fname = clean($_POST['first_name']);

    $lname = clean($_POST['last_name']);

    $email = clean($_POST['email_address']);

    $pw = clean($_POST['password']);

 

//Declare the file

    $file = "/staff/seebetra/homepage/ciss225/registered_users.txt";

 

//Open File in append mode

$fp = fopen ($file, "a+");

 

//Append Data to file

fwrite($fp, $email);

fwrite($fp, ":");

fwrite($fp, $pw);

fwrite($fp, ":");

fwrite($fp, $fname);

fwrite($fp, ":");

fwrite($fp, $lname);

 

echo 'Done';

 

// Close the file

fclose($fp);

 

 

?>

 

Thanks for looking at this!

TJ

Link to comment
https://forums.phpfreaks.com/topic/42841-login-help/#findComment-207963
Share on other sites

Something like this should do it... I think there shouldn't be a problem.

 

<?php

if(!isset($_POST['submit']))
{
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
echo "Your Email: <input type=\"text\" name=\"email\"><br>";
echo "Your Pass: <input type=\"password\" name=\"pass\"><br>";
echo "<input type=\"submit\" name=\"submit\" value=\"Login!\">";
echo "</form>";
die;
}

$email = trim($_POST['email']);
$pass = trim($_POST['pass']);

$file = "/staff/seebetra/homepage/ciss225/registered_users.txt";
$lines = file($file);
$logged = FALSE;
foreach($lines as $line)
{
if(preg_match("/^.+?:.+?:".preg_quote($email).":".preg_quote($pass)."$/s", trim($line)))
{
	list($first, $last, $f_email, $f_pass) = explode(":", trim($line));
	echo "Hello ".$first." ".$last."!";
	$logged = TRUE;
	break;
}
}
if(!$logged)
echo "Sorry, you didn't match!";

?>

 

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/42841-login-help/#findComment-207966
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.