我正在尝试对我使用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>";
}
这就是我查询db以获取用户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 );
}
我收到警报&#34;用户是管理员&#34;然后它显示一个空白页面。我做错了什么?
答案 0 :(得分:2)
首先:这个问题与Check if user is an admin by username or email only
有关第二:你的第一个街区的逻辑非常奇怪......这应该是类似的
您应始终使用内置的WordPress登录表单和身份验证工作流程。使用起来并不难,滚动自己的登录页面会使您的应用程序非常不安全,除非您这样做完全正确
要使用默认的WordPress登录流程,请在Dashboard的常用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()
识别为真...所以我们一步到下一个elseif,它将检查用户是否是管理员。如果没有,则它将终止其身份验证会话并使用wp_die失败我们终止auth会话,这样如果他们重新加载页面,他们将被带回登录页面以输入管理员的凭据,而不是重复显示{{1}你可以调整这个,但这可能不是所希望的行为(可能重定向到/ wp-admin / ...或者在wp_die中为常规/ wp-admin /提供一个链接)
如果他们确实具有管理员角色,则会继续执行请求,并且可以访问仪表板。
请注意,为此,您需要让您的信息中心与WordPress安装在同一个域中运行...否则Cookie不会转移。
很难做到......这个示例不包括Nonces,蜜罐或任何其他安全功能,您将使用标准登录表单(使用默认核心或其他插件)获得
wp_die
在您加载登录页面之外的任何其他页面之前,您应该拥有此代码
<?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>