为了账号安全,请及时绑定邮箱和手机立即绑定

使用 PHP/MYSQLI 进行多重登录

使用 PHP/MYSQLI 进行多重登录

PHP
幕布斯6054654 2022-06-17 17:17:31
我需要进行多用户登录,因此管理员将登录到 admin.php,而店员将被重定向到 clerk.php。但我有一个问题。到目前为止,我已经对其进行了编码,它可以让我将职员重定向到 clerk.php。但是我的管理员登录只会让我回到 index.php。Github 如果你想要更好的主意。https://github.com/markRichie/MSS这是我下面的代码。函数.php:   if(mysqli_num_rows($result) > 0)    {        while($row =mysqli_fetch_assoc($result))        {            if($row["Role"] == "admin")        {           $_SESSION['User'] = $row["username"];           $_Session['Role'] = $row["Role"];           header('Location: admin.php');        }        else        {            $_SESSION['User'] = $row["username"];            $_Session['Role'] = $row["Role"];            header('Location: clerk.php');        }    }    }    else {        header('Location: index.php');    }}?>管理员.php:<?phpsession_start();if(isset($_SESSION['Role'])){  if($_SESSION['Role'] != 'admin')  {    header('Location: clerk.php');  }}else{  header('Location: index.php');}我的桌子:CREATE TABLE `multilogin` (  `ID` int(11) NOT NULL,  `username` varchar(30) NOT NULL,  `password` varchar(30) NOT NULL,  `Role` varchar(20) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

不是:


$_Session['Role'] = $row["Role"];

反而:


$_SESSION['Role'] = $row["Role"];

$_SESSION您在 file 的两个地方拼错了Functions.php。


处理多个角色的代码:


您需要为每个角色编写一个单独的文件,例如admin.php, clerk,php, doctor.php,patient.php等,然后:


函数.php:


if(mysqli_num_rows($result) > 0) {

    $row = mysqli_fetch_assoc($result);

    $_SESSION['User'] = $row['username'];

    $role = $row['role'];

    $_SESSION['Role'] = $role;

    // $role is 'admin' or 'clerk' or 'doctor' or 'patient' etc.

    header("Location: $role.php");

}

else {

    header('Location: index.php');

}


查看完整回答
反对 回复 2022-06-17
  • 1 回答
  • 0 关注
  • 136 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号