【问题标题】:Using WordPress authentication while checking if user is an administrator在检查用户是否是管理员时使用 WordPress 身份验证
【发布时间】:2018-08-02 16:10:18
【问题描述】:

我正在尝试对使用 PHP 创建的管理站点使用 wordpress 身份验证。我只希望具有管理员角色的用户能够登录到该站点。这就是我所拥有的:

if ( in_array( 'administrator', (array) $user->roles ) ) {
    echo "<script type='text/javascript'>alert('User is an admin!')</script>";
    if( !wp_authenticate($user, $pass) ){
        echo "<script type='text/javascript'>alert('Wrong password!')</script>";
    }elseif(!wp_authenticate($user, $pass) ){
        echo "<script type='text/javascript'>alert('Correct password!')</script>";
    }
}else{
    echo "<script type='text/javascript'>alert('You're not an admin!')</script>";
}

这就是我查询数据库以获取用户 ID 的方式:

$link = mysqli_connect($host, $username,$password ,$db );
$q1 = " SELECT ID FROM wp_users WHERE 
    user_email=?";
$stmt = mysqli_prepare($link, $q1);
if($stmt){

        mysqli_stmt_bind_param($stmt, "s", $email);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $id);
        mysqli_stmt_fetch($stmt);
        //echo $id;
        $user = get_userdata( $id );   

}

我收到警报“用户是管理员”,然后它显示一个空白页面。我做错了什么?

【问题讨论】:

    标签: php wordpress authentication


    【解决方案1】:

    首先:这个问题与Check if user is an admin by username or email only有关

    第二:你的第一个块中的逻辑很奇怪......这就是应该的样子

    使用 WordPress 登录工作流程

    您应该始终使用内置的 WordPress 登录表单和身份验证工作流程。使用并滚动您自己的登录页面并不难,除非您做到完全正确

    ,否则您的应用程序非常不安全

    要使用默认的 WordPress 登录流程,请在仪表板的通用 PHP 文件中添加类似内容

    <?php 
    require_once 'path/to/wp-load.php';
    // Before every page load
    if ( ! is_user_logged_in() ) {
        // Not logged in at all... redirect to WP Login Page
        auth_redirect();
        exit(); // Exit to be safe
    } elseif ( ! in_array( 'administrator', wp_get_current_user()->roles ) ) {
        wp_logout(); // Destroy their login session
        wp_die('You must be an administrator'); // Die with a nice error page
    }
    

    让我解释一下这个工作流程:

    首先我们导入wp-load.php。这也有点不受欢迎,可能有一种更轻松的方法,但现在它应该工作得很好

    现在,假设用户加载了https://example.com/my-admin/index.php,您的common.php 将首先检查用户是否已登录。如果没有,那么它将调用auth_redirect() 并退出,这将重定向用户给https://example.com/wp-login.php?return_url=https%3A%2F%2Fexample.com%2Fmy-admin%2Findex.php

    用户将使用 wp-login 页面登录,然后他们将被重定向回 https://example.com/my-admin/index.php,您的 common.php 现在会将 is_logged_in() 识别为 true...所以我们进入下一个 elseif 位置将检查用户是否是管理员。如果不是,那么它将终止他们的身份验证会话并以 wp_die 失败我们终止身份验证会话,以便如果他们重新加载页面,他们将被带回登录页面以输入管理员的凭据,并且不会重复显示 wp_die 页面,您可以随意调整它,因为这可能不是所需的行为(可能重定向到 /wp-admin/... 或在 wp_die 中为常规 /wp-admin/ 提供链接)

    如果他们确实具有管理员角色,则将继续执行请求并且可以访问仪表板。

    请注意,要使其正常工作,您需要让仪表板与您的 WordPress 安装在同一个域上运行...否则 cookie 将无法传输。

    滚动您自己的登录页面。这是不好的做法

    这很难做到...这个示例不包括 Nonce、蜜罐或任何其他您将通过标准登录表单(使用默认核心或来自其他插件)获得的安全功能

    <?php 
    // Your login.php page
    
    // Really, no really, this should just be done with WordPress's default login page with an auth_redirect()
    // Really this is not recommended, it's really bad practice, and much less secure
    // You've been warned
    
    // If they provided a username and password, then check them
    if ( isset( $_POST['username'] ) && isset( $_POST['password'] ) ) {
      $username = $_POST['username']
      $username = $_POST['password']
      // Check the username and password with WordPress to see if they are for any user
      $user = wp_authenticate( $username, $password );
      $error = '';
      if ( is_a( $user, 'WP_User' ) ) {
          // Verify that the user is an admin
          if ( in_array( 'administrator', (array) $user->roles ) ) {
              // Redirect them to the dashboard
              wp_redirect('dashboard.php');
              exit();
          } else {
              $error = "Correct password, but not an Admin : (";
          }
      } else {
          $error = "Wrong password :(";
      }
    }
    // The Login Page
    ?>
    <html>
    <head>
      <title> My Login </title>
    </head>
    <body>
      <form action="login.php" method="POST">
        <?php if ( $error ): ?>
           <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        <label>
        Username:
          <input type="text" name="username" placeholder="username" required />
        </label>
        <label>
        Password:
          <input type="password" name="password" placeholder="password" required />
        </label>
      </form>
    </body>
    </html>
    

    在您加载除登录页面之外的任何其他页面之前,您应该拥有此代码

    <?php
    if ( ! is_user_logged_in() || ! in_array( 'administrator', wp_get_current_user()->roles ) ) {
        wp_redirect('login.php?error=Please%20Login');
        exit();
    }
    // now you can do your stuff
    

    【讨论】:

    • 好的,那么如何使用 WordPress 的默认登录页面作为我网站的登录页面?
    • 我在回答中概述了如何使用它,您需要在自定义仪表板/管理中的每个页面都包含一个通用 php 文件。无论如何,这应该是您应该拥有的东西。在该文件中,您应该有类似于我概述的代码
    • 好的,所以我将您的答案的第一块添加到我的常见 php 中,但它似乎不起作用。在该代码中,它在哪里说“如果凭据正确,请转到我的管理站点”?
    • 使用你的代码的第二块让它工作,但仍然想在上一条评论中了解我的问题的答案
    • 编辑了更多解释
    猜你喜欢
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2011-08-06
    • 2019-01-20
    • 2013-11-17
    • 1970-01-01
    • 2016-07-11
    相关资源
    最近更新 更多