如何在只有管理员或经理用户类型才能看到导航链接的地方

时间:2018-01-18 01:59:17

标签: php mysql

我要做的是让链接对regualr用户隐藏,并检查用户输入的内容,如果是管理员或管理员级别,它会显示某些链接示例:< / p>

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

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

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

这将显示用户类型I只有三种用户类型,管理员,经理和用户级别。

 <?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{
}
?>

关于我如何能够实现这一目标的任何想法?先感谢您!

2 个答案:

答案 0 :(得分:1)

试试这个:

<?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{
}
?>

答案 1 :(得分:0)

您可以轻松检查用户类型,然后使用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{

}
?>