如何使它成为只有管理员或管理员用户类型才能看到导航链接的地方

sycxhyv7  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(450)

我要做的是让它对普通用户隐藏链接,它会检查用户类型,如果是管理员或经理级别,它会显示某些链接示例:

<a href="adminarea.php"> Admin Only </a>

在导航栏中,只有用户类型的管理员才能看到上面的链接和下面的链接,或者只有管理员才能看到只有管理员的链接。

<a href="manager.php"> Managers Only </a>

这是什么将显示用户类型我只有三个用户类型,管理员,经理和用户级别。

<?php echo $_SESSION['user']['user_type'];?>

这就是我现在拥有的,只有登录的用户才能查看以下链接:

<?php 
session_start();
if(isset($_SESSION['user']['username'])){
    echo "
        <li class='nav-item dropdown'>
        <a class='nav-link dropdown-toggle' href='#' id='navbarDropdownMenuLink' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'><i class='fa fa-exclamation-triangle'></i>
        Admin/Managment
        </a>
        <div class='dropdown-menu' aria-labelledby='navbarDropdownMenuLink'>
        <a class='nav-link' href='managment/login.php'>Managment Login</a>
        <a class='nav-link' href='admin/login.php'>Admin Login</a> 
        </div>
        </li>
        ";

echo "   </li>
    <li class='nav-item'>
    <a class='nav-link' href='logout.php'><i class='fa fa-power-off'></i> Logout</a>
    </li>";

}else{
}
?>

有什么办法能让这一切发生吗?提前谢谢!

hsgswve4

hsgswve41#

试试这个:

<?php
session_start();
if(isset($_SESSION['user']['username'])){
    if (($_SESSION['user']['user_type'] == 'admin')||($_SESSION['user']['user_type'] == 'manager')){
        if ($_SESSION['user']['user_type'] == 'admin') {
            $link = "<a class='nav-link' href='admin/login.php'>Admin Login</a>";
        } elseif ($_SESSION['user']['user_type'] == 'management') {
            $link = "<a class='nav-link' href='managment/login.php'>Managment Login</a>";
        }
            echo "
                <li class='nav-item dropdown'>
                <a class='nav-link dropdown-toggle' href='#' id='navbarDropdownMenuLink' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'><i class='fa fa-exclamation-triangle'></i>
                Admin/Managment
                </a>
                <div class='dropdown-menu' aria-labelledby='navbarDropdownMenuLink'>".$link."
                </div>
                </li>
                ";

echo "</li>
    <li class='nav-item'>
    <a class='nav-link' href='logout.php'><i class='fa fa-power-off'></i> Logout</a>
    </li>";
}
}else{
}
?>
6ss1mwsb

6ss1mwsb2#

您可以轻松地检查用户类型,然后使用if else语句来显示所需的内容。
这是我对你代码的编辑。。。。

<?php 
session_start();
if(isset($_SESSION['user']['username'])){
    echo "
        <li class='nav-item dropdown'>
        <a class='nav-link dropdown-toggle' href='#' 
          id='navbarDropdownMenuLink' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'><i class='fa fa-exclamation-triangle'></i>
        Admin/Managment
        </a>
        <div class='dropdown-menu' aria-labelledby='navbarDropdownMenuLink'> ";
?>
<?php
        if ($_SESSION['user']['username'] == 'Admin'){
?>
        <a class='nav-link' href='managment/login.php'>Managment Login</a>
<?php 
        } elseif ($_SESSION['user']['username'] == 'Manager'){
?>
            <a class='nav-link' href='admin/login.php'>Admin Login</a>
<?php 
        } 
?> 
</div>
</li>
";

    echo "   </li>
        <li class='nav-item'>
        <a class='nav-link' href='logout.php'><i class='fa fa-power-off'></i> Logout</a>
        </li>";
}else{

}
?>

相关问题