Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have made a program that lets you enter the username and password then it stores it in a text file and when i want to login i want to make it so it loops through the text file that has the usr/pass in to find if you entered your credentials in correctly. I'm not sure how to do this. please can somebody help

What I have tried:

i do not know how to do it!!!!
Posted
Updated 1-Oct-18 1:44am
Comments
Mohibur Rashid 29-Sep-18 20:29pm    
How about read line by line?
summiya1 1-Oct-18 7:46am    
Please don't save user information specially username & password in textfile. Its not recommended and you can use Sqlite.

1 solution

if (isset($_POST['submit_btn']))
{
$username = $_POST['name'];
$password = $_POST['pwd'];
if (empty($username))
{
echo "Please enter a username";
} else $username = $username;

if (empty($password))
{
echo "Please enter a password";
} else $password = $password;
$text = $username . "," . $password . "\
";
$fp = fopen('accounts.txt', 'a+');
$path = 'accounts.txt';
if (file_exists($path))
{
$contents = file_get_contents($path);
$contents = explode("\
", $contents);
$users = array();
foreach ($contents as $value) {
$user = explode(',', $value);
$users[$user[0]] = $user[1];
}
if (isset($users[$_POST['name']])) {
echo "that user already exists please enter a different user name";
}
}
else
{
echo "NO";
}
if(!empty($username) && !empty($password) && fwrite($fp, $text) && !isset($users[$_POST['name']])) {
header('Location: login.html');
}
fclose ($fp);
$path = 'accounts.txt';
}
?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900