A
A
asdasa212020-04-18 21:00:27
PHP
asdasa21, 2020-04-18 21:00:27

How to implement admin panel for multiple users?

When I add a new user with admin role (for example "admin2") the old admin (admin1) cannot login as admin. Login occurs under the user status

<?php
session_start();
require('connect.php');

if (isset($_POST['username']) and isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query ="SELECT * FROM users WHERE username='$username' and password='$password'";
    $query2 ="SELECT * FROM users WHERE role = 'admin'";
    $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
    $result2 = mysqli_query($connection, $query2) or die(mysqli_error($connection));
    $count = mysqli_num_rows($result);

    while ($row = mysqli_fetch_array($result2)) {
         $role = $row['username'];
    }
    if($username == $role){ 
        header('Location:admin.php');
        $_SESSION['t'] = "Вы вошли под админом ";
    }else {
        header('Location: index.html');
        echo "Вы вошли под пользователем ";
    }

    if ($count == 1) {
        $_SESSION['username'] = $username;
    } else {
        $fsmsg = "Ошибка";
    }
}

if (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
    echo "Hello," . $username . "";
    echo "<a href='logout.php' class='btn btn-lg btn-primary'>Выйти</a>";
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="style.css">
    <title>Авторизация</title>
</head>
<body>

<div class="container">
    <form class="form-signin" method="POST">
        <h2>Авторизация</h2>

        <input type="text" name="username" class="form-control" placeholder="Username" required>
        <input type="password" name="password" class="form-control" placeholder="Password" required>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Авторизоваться</button>
        <a href="index.php" class="btn btn-lg btn-primary btn-block">Зарегистрироваться</a>
    </form>
</div>

</body>
</html>

<code>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Edward, 2020-04-18
@asdasa21

asdasa21 theoretically like this:

spoiler
<?php

session_start();
require('connect.php');

if (isset($_POST['username']) and isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "SELECT * FROM users WHERE username='$username' and password='$password'";
    $query2 = "SELECT * FROM users WHERE role = 'admin'";
    $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
    $result2 = mysqli_query($connection, $query2) or die(mysqli_error($connection));
    $count = mysqli_num_rows($result);

    $role = [];

    while ($row = mysqli_fetch_array($result2)) {
        $role[] = $row['username'];
    }
    if (in_array($username, $role)) {
        header('Location:admin.php');
        $_SESSION['t'] = "Вы вошли под админом ";
    } else {
        header('Location: index.html');
        echo "Вы вошли под пользователем ";
    }

    if ($count == 1) {
        $_SESSION['username'] = $username;
    } else {
        $fsmsg = "Ошибка";
    }
}

if (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
    echo "Hello," . $username . "";
    echo "<a href='logout.php' class='btn btn-lg btn-primary'>Выйти</a>";
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
          integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="style.css">
    <title>Авторизация</title>
</head>
<body>

<div class="container">
    <form class="form-signin" method="POST">
        <h2>Авторизация</h2>

        <input type="text" name="username" class="form-control" placeholder="Username" required>
        <input type="password" name="password" class="form-control" placeholder="Password" required>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Авторизоваться</button>
        <a href="index.php" class="btn btn-lg btn-primary btn-block">Зарегистрироваться</a>
    </form>
</div>

</body>
</html>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question