How can PHP be used to create a simple user management system without a database?
To create a simple user management system without a database, you can store user information in an array within the PHP code. This array can hold user details such as username, password, and any other relevant information. By using PHP sessions, you can track user login status and perform basic user management operations.
<?php
session_start();
$users = [
'user1' => [
'username' => 'user1',
'password' => 'password1',
'email' => 'user1@example.com'
],
'user2' => [
'username' => 'user2',
'password' => 'password2',
'email' => 'user2@example.com'
]
];
function login($username, $password)
{
global $users;
foreach ($users as $user) {
if ($user['username'] === $username && $user['password'] === $password) {
$_SESSION['user'] = $user;
return true;
}
}
return false;
}
function logout()
{
session_unset();
}
?>
Related Questions
- How can the use of @-sign to suppress errors in PHP code impact debugging and maintenance in the long run?
- What are the potential pitfalls of using regular expressions in PHP, as demonstrated in the provided code snippet?
- In what scenarios would using cookies be a more effective method for controlling email sending frequency compared to IP addresses in PHP?