你如何隐藏特定用户类的下拉菜单 [英] How do you hide dropdown many for specific user class

查看:50
本文介绍了你如何隐藏特定用户类的下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有基本注册和登录系统的网站,我的页面只有管理员才能访问.

我的帐户数据库有一个角色列,其中 1 个用户分配为管理员,另一个分配为用户

我有一个带有下拉菜单的导航栏,我也有一个包含管理员用户的登录系统,我想做的是使导航栏的下拉菜单部分对标准用户隐藏,只对管理员可见,

<ul class="nav justify-content-center"><li class="nav-item"><a class="nav-link" style="color: white" href="Home.php">首页</a><li class="nav-item"><a class="nav-link" style="color: white" href="Suppliers.php">Suppliers</a><li class="nav-item"><a class="nav-link" style="color: white" href="Products.php">Products</a><li class="nav-item"><a class="nav-link" style="color: white" href="Contact.php">联系我们</a><li class="nav-item"><a class="nav-link" style="color: white" href="FindUs.php">找到我们</a><li class="nav-item"><a class="nav-link" style="color: white" href="reviews.html">评论</a><li class="nav-item"><a class="nav-link" style="color: white" href="profile.php">Profile</a><li class="nav-item"><a class="nav-link" style="color: white" href="logout.php">注销</a><li class="nav-item"><a href="#" class="dropdown-toggle" data-toggle="dropdown">管理员</a><div class="下拉菜单"><a href="createPoll.php" class="dropdown-item">创建投票</a><a href="polls.php" class="dropdown-item">民意调查</a><a href="read.php" class="dropdown-item">联系人</a>

authenticate.php

prepare('SELECT id, password, role FROM accounts WHERE username = ?')) {//绑定参数(s = string, i = int, b = blob, etc),在我们的例子中,用户名是一个字符串,所以我们使用s"$stmt->bind_param('s', $_POST['username']);$stmt->execute();//存储结果,以便我们可以检查该帐户是否存在于数据库中.$stmt->store_result();如果 ($stmt-> num_rows > 0) {$stmt->bind_result($id, $password, $role);$stmt->fetch();//帐户存在,现在我们验证密码.//注意:记得在你的注册文件中使用 password_hash 来存储散列的密码.if (password_verify($_POST['password'], $password)) {//验证成功!用户已登录!//创建会话,以便我们知道用户已登录,它们基本上就像 cookie,但记住服务器上的数据.session_regenerate_id();$_SESSION['登录'] = TRUE;$_SESSION['name'] = $_POST['username'];$_SESSION['id'] = $id;$_SESSION['role'] = $role;header('位置:home.php');} 别的 {echo '密码错误!';}} 别的 {echo '用户名不正确!';}$stmt->close();}?>

停止访问某些网站的代码

这就是我目前所拥有的

解决方案

您需要将 IF 语句切换为相反的语句,因此块中的所有内容仅向管理员显示.您当前的 IF 语句仅向非管理员显示.

您还应该检查 $_SESSION['role'] 是否为空,然后再检查它是否等于 admin 以防止在用户未登录时出错.

试试这个:

//角色变量不是空的 - 而且 - 它等于管理员"if(!empty($_SESSION['role']) && $_SESSION['role'] == 'admin') {?><div class="header"><ul class="nav justify-content-center"><li class="nav-item"><a class="nav-link" style="color: white" href="Home.php">首页</a><li class="nav-item"><a class="nav-link" style="color: white" href="Suppliers.php">Suppliers</a><li class="nav-item"><a class="nav-link" style="color: white" href="Products.php">Products</a><li class="nav-item"><a class="nav-link" style="color: white" href="Contact.php">联系我们</a><li class="nav-item"><a class="nav-link" style="color: white" href="FindUs.php">找到我们</a><li class="nav-item"><a class="nav-link" style="color: white" href="reviews.html">评论</a><li class="nav-item"><a class="nav-link" style="color: white" href="profile.php">Profile</a><li class="nav-item"><a class="nav-link" style="color: white" href="logout.php">注销</a><li class="nav-item"><a href="#" class="dropdown-toggle" data-toggle="dropdown">管理员</a><div class="下拉菜单"><a href="createPoll.php" class="dropdown-item">创建投票</a><a href="polls.php" class="dropdown-item">民意调查</a><a href="read.php" class="dropdown-item">联系人</a>

<?php}

您会注意到,在 IF 语句中,我结束了 PHP 标记,并在最后重新开始了它.这使您无需使用 echo 即可编写纯 HTML.

I have created a website that has a basic registration and login system, I have pages that I only admin can access.

My database for the accounts has a role column with 1 user assigned as admin and the other assigned as user

I have a navbar with a drop menu, i also have a logiin system in place which iincludes admin users, what id like to do is make the drop down menu part of the nav bar hidden for standard users and only visible for admins,

<div class="header">
  <ul class="nav justify-content-center">
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="Home.php">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="Suppliers.php">Suppliers</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="Products.php">Products</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="Contact.php">Contact us</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="FindUs.php">Find Us</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="reviews.html">Reviews</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="profile.php">Profile</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" style="color: white" href="logout.php">Logout</a>
    </li>
    <li class="nav-item">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin</a>
      <div class="dropdown-menu">
          <a href="createPoll.php" class="dropdown-item">Create a Poll</a>
          <a href="polls.php" class="dropdown-item">Polls</a>
          <a href="read.php" class="dropdown-item">Contacts</a>
      </div>
    </li>
  </ul>
</div>

authenticate.php

<?php
session_start();
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'feedbackdb';

// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
    // If there is an error with the connection, stop the script and display the error.
    exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}

// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
    // Could not get the data that should have been sent.
    exit('Please fill both the username and password fields!');
}

// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
if ($stmt = $con->prepare('SELECT id, password, role FROM accounts WHERE username = ?')) {
    // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    // Store the result so we can check if the account exists in the database.
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
    $stmt->bind_result($id, $password, $role);
    $stmt->fetch();
    // Account exists, now we verify the password.
    // Note: remember to use password_hash in your registration file to store the hashed passwords.
    if (password_verify($_POST['password'], $password)) {
        // Verification success! User has loggedin!
        // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
        session_regenerate_id();
        $_SESSION['loggedin'] = TRUE;
        $_SESSION['name'] = $_POST['username'];
        $_SESSION['id'] = $id;
        $_SESSION['role'] = $role;
        header('location: home.php');


    } else {
        echo 'Incorrect password!';
    }
} else {
    echo 'Incorrect username!';
}

    $stmt->close();
}
?>

code to stop access to certain sites

<?php
Session_start();
if($_SESSION['role'] !== 'admin') {
    //block user access
    die("You do not have permission to view this page.");
}

?>

thats what i have gotso far

解决方案

You need to switch your IF statement to be the opposite, so everything inside the block is only shown to admins. Your current IF statement is only shown to non-admins.

You should also check if $_SESSION['role'] is not empty before checking if it's equal to admin to prevent errors if the user is not logged in.

Try this:

//role variable IS NOT EMPTY - AND - it IS EQUAL TO "admin"
if(!empty($_SESSION['role']) && $_SESSION['role'] == 'admin') {
    ?>
        <div class="header">
            <ul class="nav justify-content-center">
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="Home.php">Home</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="Suppliers.php">Suppliers</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="Products.php">Products</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="Contact.php">Contact us</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="FindUs.php">Find Us</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="reviews.html">Reviews</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="profile.php">Profile</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" style="color: white" href="logout.php">Logout</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin</a>
                    <div class="dropdown-menu">
                        <a href="createPoll.php" class="dropdown-item">Create a Poll</a>
                        <a href="polls.php" class="dropdown-item">Polls</a>
                        <a href="read.php" class="dropdown-item">Contacts</a>
                    </div>
                </li>
            </ul>
        </div>
    <?php
}

You will notice that, inside the IF statement, I end the PHP tag and also start it again at the end. This allows you to write plain HTML without having to use echo.

这篇关于你如何隐藏特定用户类的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆