简体   繁体   English

比较WordPress中的密码始终返回false

[英]Compare password in WordPress always return false

I want to insert new user in WordPress by coding and then compare username & password. 我想通过编码在WordPress中插入新用户,然后比较用户名和密码。 I tried to insert user name and password in two ways. 我试图通过两种方式插入用户名和密码。

1 1个

Insert new user code (Table - wp_users) 插入新的用户代码(表-wp_users)

$email = $_POST['txtEmail'];
$userName = $_POST['txtUserName'];//Sahil
$password = $_POST['txtPassword'];//1212
$hash_value = password_hash($password, PASSWORD_BCRYPT);
$user_id = wp_create_user( $userName, $hash_value, $email);

2 2

Tried to insert password in another way 尝试以其他方式插入密码

$settings = array('cost' => 10, 'salt' => 'rSq.2M7Ikc.QPhVtYlp1Nu');
$hash_value = password_hash($password, PASSWORD_BCRYPT, $settings);
$user_id = wp_create_user( $userName, $hash_value, $email);

I didn't add code to check username & email exists in table. 我没有添加代码来检查用户名和表中是否存在电子邮件。 Above code is working properly and new user is being added in wp_users table and password is being stored in hash format. 上面的代码正常工作,并且在wp_users表中添加了新用户,并且密码以哈希格式存储。 Ex. 例如 $P$BgynwP/kZuQu5p/SAU/dTMA19sbJs3/ $ P $ BgynwP / kZuQu5p / SAU / dTMA19sbJs3 /

I tried to compare it by two function, but it always return false. 我试图通过两个函数比较它,但是它总是返回false。

1 1个

$userName = $_POST['txtUserName'];//Sahil
$password = $_POST['txtPassword'];//1212

$user = get_user_by( 'login', $userName );
$result = wp_check_password( $password, $user->user_pass, $user->ID);
if($result){
        echo 'Correct';
    }else{
        echo 'Wrong'; //Always return wrong
    }

2 2

$userName = $_POST['txtUserName'];//Sahil
$password = $_POST['txtPassword'];//1212
$user = get_user_by( 'login', $userName );
$isPasswordCorrect = password_verify($password, $user->user_pass);
if($isPasswordCorrect){
        echo 'Correct';
    }else{
        echo 'Wrong'; //Always return wrong.
    }

Also tried - 还尝试了-

var_dump(password_verify($password, $user->user_pass));

But it still return false. 但是它仍然返回false。

Let me know, If there is any mistake in my code while comparing password or technique is wrong to do this. 让我知道,如果在比较密码或技术时我的代码中有任何错误是错误的。

Thanks 谢谢

You can used wp_authenticate_username_password() 您可以使用wp_authenticate_username_password()

Authenticate a user, confirming the username and password are valid 验证用户身份,确认用户名和密码有效

$email = $_POST['txtEmail'];
$userName = $_POST['txtUserName'];//Sahil
$password = $_POST['txtPassword'];//1212
$check = wp_authenticate_username_password( NULL, $userName , $password );

You can then simply check the result with 然后,您可以简单地检查结果

if(is_wp_error( $check ))
{
  echo 'Wrong'; 
}
else
{
  echo 'Correct';
}

Create user In WordPress wp_create_user() Password automatic save to database no need convert password_hash 在WordPress中创建用户wp_create_user()密码自动保存到数据库,无需转换password_hash

wp_create_user( "Sahil", "1212", "Sahil@gmail.com" );

您必须在wp_create_user使用普通密码,而不是哈希。

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

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