Wordpress密码如何比较用户密码到wordpress用户表密码?

时间:2016-05-12 08:26:04

标签: php wordpress

自定义注册用户

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 );

自定义登录用户

    $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 );

比较登录密码时出错。如何比较登录密码? wp_check_password()无效。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:5)

将已经散列的密码与其纯文本字符串进行比较,以下是示例。

stopClosingWindows = false;
try
{
    // Create a new thread that should execute the method ParallelCloseWindows in parallel
    var thread = new Thread(ParallelCloseWindows);

    // set isBackground to true so that this thread would not prevent your application from closing when 
    // we forget to terminate it
    thread.IsBackground = true;

    // and start the new thread
    thread.Start();

    EXCELApplicationObj.GetType().InvokeMember("Run", 
            System.Reflection.BindingFlags.Default 
            | System.Reflection.BindingFlags.InvokeMethod,
            null, EXCELApplicationObj, oRunArgs);
}
finally
{
    stopClosingWindows = true;
}

根据https://developer.wordpress.org/reference/functions/get_user_by/

的文档,我猜您需要使用<?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"; } ?> 代替$user->data->user_pass而不是$user->user_pass;

我只是猜测,因为我不知道print_r($user);返回什么

希望它有所帮助!

答案 1 :(得分:1)

wp_create_user调用内部使用wp_insert_user的{​​{1}}。

实际上,您的代码最终会以

结束
wp_hash_password

wp_hash_password(wp_hash_password('balan')); 应该提供原始密码。