How can a PHP login system be implemented without using MySQL?
Using a PHP login system without MySQL can be achieved by storing user credentials in a flat file, such as a CSV or JSON file. The PHP script can then read this file to authenticate users during the login process.
<?php
// Function to authenticate user
function authenticateUser($username, $password) {
$users = json_decode(file_get_contents('users.json'), true);
foreach ($users as $user) {
if ($user['username'] === $username && $user['password'] === $password) {
return true;
}
}
return false;
}
// Example usage
if (authenticateUser('john_doe', 'password123')) {
echo 'Login successful!';
} else {
echo 'Invalid username or password.';
}
?>
Related Questions
- How can undefined variable errors be avoided when processing form data in PHP?
- In what scenarios would serializing arrays in a database be a suitable solution for storing data in PHP applications?
- Are there any potential pitfalls or issues to be aware of when setting PDO attributes during initialization in PHP?