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();
}

?>