dongsuo9982 2014-09-20 18:02
浏览 34

检查用户是管理员还是正常用户

I am having problems checking whether the user is admin or not in the database. I made it so if admin has the value 1 for the users profile then they are admin and is redirected to the admin page and if not they are redirected to the login page. However I gave my personal account the value of 1 in the database however it is still redirecting me to the login page.

I have given my code below for you to see if I have done anything wrong, please tell me as I have only just started learning 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");
} 
  • 写回答

1条回答 默认 最新

  • duanjing4667 2014-09-20 18:47
    关注

    Correct you code First:

    Try this:

    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"); 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100