限制 php 中角色的访问


Restrict access for a role in php

你会怎么编码?我有可以将某些角色重定向到页面的部分,但在限制他们查看特定页面时遇到问题。

我试过这个:

session_start();
if($_SESSION['username'] == "a@a.com"); 
{
header('location: student_listing');
die();
} 
else {
header('location: student_entry'); 
}

但它会说该网站由于重定向太多而崩溃。

只需颠倒条件,根据您的代码,您尝试在用户名 a@a.com 时重定向用户。它应该是!=而不是==

<?php
$allowedUserNames = array('abc', 'ced', 'efj');
if( !in_array($_SESSION['username'], $allowedUserNames) )
{
 // redirect to unrestricted page
 header("location: UnRestrictedPage.php");
}
header("location: restrictedPage.php");

您可以通过设置这样的 if 条件来限制:

if($role=='specify role here')
{
    ----
}
else {
}

你能发布你的代码以更清楚地了解你到底做了什么吗.....