use*_*556 8 php mysql authentication wordpress
我开发了一个使用PHP和MySQL的网站,它已经有一个登录和注册表单.(myweb.com)
我在这个网址上添加了wordpress myweb.com/blog
我想在WordPress上禁用登录和注册页面并强制用户使用我的.基本上将我的登录与WordPress集成,以便用户将在两个站点上登录.
我的网站members表看起来像这样.并且所有注册用户都存储在此处.我的数据库中的密码使用哈希值md5()
id | name | email | password
Run Code Online (Sandbox Code Playgroud)
和WordPress结构是这样的,目前是空的
ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name
Run Code Online (Sandbox Code Playgroud)
我尝试按照这里提到的步骤
但是我在第254行得到了这个错误 var_dump($user);
object(WP_Error)#620 (2) {
["errors"]=>
array(1) {
["invalid_username"]=>
array(1) {
[0]=>
string(166) "<strong>ERROR</strong>: Invalid username. <a href="http://localhost/dev/blog/wp-login.php?action=lostpassword" title="Password Lost and Found">Lost your password</a>?"
}
}
["error_data"]=>
array(0) {
}
}
Run Code Online (Sandbox Code Playgroud)
此外,所有用户信息都存储在members我的网站上的表格中,而不是存储在WordPress的数据库中.
这是我的网站的登录代码,我最近也添加了WordPress登录.
/*
* Login
*
* $email = email of user
* $pass = user password (must already be in md5 form)
* $url = url of page they are login from
*/
function login($email = '', $pass = '', $url = '', $sticky = false)
{
global $lang, $_db, $mod, $template_style;
// Replace nasty things to stop sql injection
$email = addslashes(strtolower($email));
$email = strip_tags($email);
$email = htmlspecialchars($email, ENT_QUOTES);
//get user id
$sql = "SELECT `id`, `name`, `username`
FROM `members`
WHERE `email`='".mysql_real_escape_string($email)."'
AND `pass` = '" . mysql_real_escape_string($pass) . "'
LIMIT 0,1";
$q = $_db->query($sql);
list($uid, $name, $username) = $_db->fetch_array($q);
$login_check = $_db->num_rows($q);
if ($login_check <= '0') //check if login matches
{
echo '0'; //login failed
die;
}
/*
* wordpress login
*
* read:
* http://codex.wordpress.org/Function_Reference/wp_update_user
*/
$credentials = array();
$credentials['user_email'] = $email;
$credentials['user_password'] = $pass;
$credentials['remember'] = $sticky; // true/false
$secure_cookie = false; // true / false
$user = wp_authenticate($credentials['user_email'], $credentials['user_password']);
if ( is_wp_error($user) ) {
if ( $user->get_error_codes() == array('empty_email', 'empty_password') ) {
//$user = new WP_Error('', '');
$user = wp_update_user(array ( 'user_login' => $name, 'user_email' => $email, 'user_pass' => $pass ));
}
}
var_dump($user);
wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
do_action('wp_login', $user->user_login, $user);
/*
set login cookies
*/
set_login_cookie($uid, $pass, $sticky);
//lock check
lock_checker($uid);
update_thisuser_online();
}
Run Code Online (Sandbox Code Playgroud)
我是否必须从members表格中复制所有内容并将其填入wp_users或者是否有办法登录到wordpress而不会在2个不同的表格中存在重复数据?我不想在两个网站上都有2个登录和2个注册表单.
为什么wp_authenticate()我的代码不会在上面验证?
尝试更改文件中的选项wp-config.php以指向您的members数据库;使用 MySQL 创建一个单独的用户/密码并授予其对您的成员数据库的权限可能是一个好主意。您可以使用该文件设置/更改许多有用的选项。我建议您阅读有关它的文档以获取更深入的内容。如果您决定尝试上述方法,请确保也更改该行$table-prefix以匹配您现有的数据库。
另外,看看这篇博客文章是否有任何帮助,因为它直接处理外部身份验证。有一个可用的 php 脚本,您可以复制/粘贴或以其他方式根据您的特定需求进行调整。特别注意 和include_once("../wp-config.php");线条include_once("../wp-includes/class-phpass.php");。
| 归档时间: |
|
| 查看次数: |
944 次 |
| 最近记录: |