@asdasa21

Как реализовать админку для нескольких пользователей?

Когда добавляю нового пользователя с ролью admin (например "admin2") старый admin (admin1) не может войти под статусом админа. Вход происходит под статусом user

<?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>
  • Вопрос задан
  • 80 просмотров
Решения вопроса 1
@IS-Builder
~ PHP-8 & REGEXP - This is a really powerful kit ~
asdasa21 теоретически так:
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>
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы