Home > Software design >  How to differentiate between user type in php without database?
How to differentiate between user type in php without database?

Time:11-10

I have a PHP task which I made a login page without a database and I have a button in the project file index.php i want to be unavailable for a normal user and available to an admin user. how can I check or differentiate between user types in this case??

I made a login page which I save the username and password in an array like "john" => "123"

CodePudding user response:

Even without a database, the way you store the data matters. Taking the idea of a users table, your array should look like a table with rows. Each row is a user. And the columns are id, name, password, email, userType, etc...

$users = [
    [
        "id" => 1,
        "name" => "sam",
        "password" => "123",
        "userType" => "admin",
    ],
    [
        "id" => 2,
        "name" => "dog",
        "password" => "123",
        "userType" => "user",
    ],
    [
        "id" => 5,
        "name" => "cat",
        "password" => "123",
        "userType" => "user",
    ],
];

CodePudding user response:

If the array you used to store login details can be accessed globally, you can check the user type this way

// assuming your table looks like this
$users = [
  0 => [
    'username' => 'user1',
    'password' => 'password123',
    'type' => 'admin'
  ],
  1 => [
    'username' => 'user2',
    'password' => 'password123',
    'type' => 'user'
  ],
];

// check type of user with id: 1
if($users[1]['type'] == 'admin')
{
    echo 'user is admin';
}
else
{
    echo 'user is not an admin';
}
  • Related