猿问

使用 PHP/MYSQLI 进行多重登录

我需要进行多用户登录,因此管理员将登录到 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:



<?php

session_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;



幕布斯6054654
浏览 137回答 1
1回答

潇潇雨雨

不是:$_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) {&nbsp; &nbsp; $row = mysqli_fetch_assoc($result);&nbsp; &nbsp; $_SESSION['User'] = $row['username'];&nbsp; &nbsp; $role = $row['role'];&nbsp; &nbsp; $_SESSION['Role'] = $role;&nbsp; &nbsp; // $role is 'admin' or 'clerk' or 'doctor' or 'patient' etc.&nbsp; &nbsp; header("Location: $role.php");}else {&nbsp; &nbsp; header('Location: index.php');}
随时随地看视频慕课网APP
我要回答