Проверка, является ли пользователь администратором или нормальным

У меня проблемы с проверкой, является ли пользователь администратором или нет в базе данных. Я сделал это так, что если admin имеет значение 1 для профиля пользователя, то он является admin и перенаправляется на страницу администратора, а если нет, то он перенаправляется на страницу входа. Однако я указал в личном кабинете значение 1 в базе данных, но он все еще перенаправляет меня на страницу входа.

Я дал мой код ниже, чтобы вы могли убедиться, что я сделал что-то не так, пожалуйста, скажите мне, поскольку я только начал изучать PHP.

<?php
session_start();
// First we cubrid_execute(conn_identifier, SQL)te our common code to connection to the database and start the session
require("include/common.php");

$admin = $_POST['admin'];
$user = $_POST['username'];

// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
// If they are not, we redirect them to the login page.
header("Location: login.php");

// Remember that this die statement is absolutely critical.  Without it,
// people can view your members-only content without logging in.
die("Redirecting to login.php");
}

// Everything below this point in the file is secured by the login system

// We can retrieve a list of members from the database using a SELECT query.
// In this case we do not have a WHERE clause because we want to select all
// of the rows from the database table.
$query = "SELECT *
FROM users
";

try
{
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}

// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();

if ($admin == 1) {
$_SESSION['username'] = $user;
header("location: memberlist.php");
}

if ($admin == 0) {
$_SESSION['username'] = $user;
header("location: login.php");
}

0

Решение

Правильный код сначала:

Попробуй это:

if ($admin == 1) {
$_SESSION['admin'] = $admin;   //put you admin in session
header("location: memberlist.php");
}

if ($admin == 0) {
$_SESSION['user'] = $user;      //here put your user in session
header("location: login.php");
}if(empty($_SESSION['user'])) //if user is empty then it redirects to login page
{

header("Location: login.php");die("Redirecting to login.php");
}
else if(!empty($_SESSION['admin']))   //if admin is not empty it goes to admin area
{

header("location: memberlist.php");
}

else if(!empty($_SESSION['user']))   //same here if user is present,then it leads to user area
{header("Location: user.php");
}
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector