首先:这个问题是有关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-登录页面,那么他们将被重定向回https://example.com/my-admin/index.php,你的common.php
现在认识is_logged_in()
为真...所以我们进入下一个elseif,它将检查用户是否是管理员。如果没有,那么它将终止认证会话,并失败,并带有wp_die我们终止auth会话,这样如果他们重新加载页面,它们将被带回到登录页面以输入管理员的凭证,并且不会重复显示wp_die
页面,您可以调整,但是你希望,因为这可能不是所需的行为(可能重定向到/ wp-admin/...或在wp_die中为常规/ wp-admin /提供链接)如果他们确实有管理员角色,那么请求的执行将继续,并且仪表板将可访问。
请注意,为了这项工作,您需要让您的仪表板与您的WordPress安装在同一个域中运行......否则Cookie不会转移。
滚动您自己的登录页面。这是不好的做法
这很难做...这个例子不包括随机数,蜜罐,或任何其他安全功能,你将与标准的登录表单获得(或者使用默认核心或额外的插件)
<?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文件。这应该是你应该有的东西。在那个文件中,你应该有类似于我所概述的代码 – edhurtig
好吧,所以我将你的答案的第一部分添加到了我的常见php中,但它似乎不起作用。在该代码中,它说的是“如果凭证是正确的,请转到我的管理员站点”? –