简体   繁体   English

WordPress的密码如何比较用户密码和WordPress的用户表密码?

[英]Wordpress password how to compare user password to wordpress user table password?

Custom Register user 自定义注册用户

wp_create_user (works) wp_create_user(有效)

    $username='balan';
    $user_pass ='balan';
    $user_email= 'random_email@email.com';
    $password = wp_hash_password( $user_pass );
    $user_id = wp_create_user( $user_name, $password, $user_email );

Custom Login user 自定义登录用户

    $username='balan';
    $user = get_user_by( 'login', $username );
    $pass ='balan';
    $hash = wp_hash_password($pass);
    if (wp_check_password( $pass, $user->data->user_pass, $user->ID ) ) {
        echo "<br>Correct";
    } else {
        echo "<br>Wrong";
    }
    **WordPress login function** 
    $creds = array();
    $creds['user_login'] = 'balan';
    $creds['user_password'] ='balan';
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );

I get error when comparing login password. 比较登录密码时出现错误。 How to compare login password? 如何比较登录密码? wp_check_password() is not working. wp_check_password()不起作用。 Please help me solve this problem. 请帮我解决这个问题。

Compare an already hashed password with its plain-text string, Here is the example. 比较一个已经散列的密码和它的纯文本字符串,这是示例。

<?php
$wp_hasher = new PasswordHash(8, TRUE);

$password_hashed = '$P$B55D6LjfHDkINU5wF.v2BuuzO0/XPk/';
$plain_password = 'test';

if($wp_hasher->CheckPassword($plain_password, $password_hashed)) {
    echo "YES, Matched";
} else {
    echo "No, Wrong Password";
}
?>

I guess instead of $user->data->user_pass you need to use $user->user_pass; 我想不是$user->data->user_pass您需要使用$user->user_pass; as per the documentation here https://developer.wordpress.org/reference/functions/get_user_by/ 根据此处的文档https://developer.wordpress.org/reference/functions/get_user_by/

I am only guessing because I don't know what print_r($user); 我只是猜测,因为我不知道什么print_r($user); returns 回报

Hope it helps ! 希望能帮助到你 !

wp_create_user calls wp_insert_user which uses wp_hash_password internally. wp_create_user调用wp_insert_user它使用wp_hash_password内部。

In essence with your code you end up with 本质上,您的代码最终会导致

 wp_hash_password(wp_hash_password('balan'));

wp_create_user should be provided with the raw password. 应该为wp_create_user提供原始密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM