返回介绍

wp_check_password()

发布于 2017-09-11 11:41:45 字数 3901 浏览 970 评论 0 收藏 0

wp_check_password( string $password,  string $hash,  string|int $user_id = '' )

Checks the plaintext password against the encrypted Password.


description

Maintains compatibility between old version and the new cookie authentication protocol using PHPass library. The $hash parameter is the encrypted password and the function compares the plain text password when encrypted similarly against the already encrypted password to see if they match.

For integration with other applications, this function can be overwritten to instead use the other package password checking algorithm.


参数

$password

(string) (Required) Plaintext user's password

$hash

(string) (Required) Hash of the user's password to check against.

$user_id

(string|int) (Optional) User ID.

Default value: ''


返回值

(bool) False, if the $password does not match the hashed password


源代码

File: wp-includes/pluggable.php

function wp_check_password($password, $hash, $user_id = '') {
	global $wp_hasher;

	// If the hash is still md5...
	if ( strlen($hash) <= 32 ) {
		$check = hash_equals( $hash, md5( $password ) );
		if ( $check && $user_id ) {
			// Rehash using new hash.
			wp_set_password($password, $user_id);
			$hash = wp_hash_password($password);
		}

		/**
		 * Filters whether the plaintext password matches the encrypted password.
		 *
		 * @since 2.5.0
		 *
		 * @param bool       $check    Whether the passwords match.
		 * @param string     $password The plaintext password.
		 * @param string     $hash     The hashed password.
		 * @param string|int $user_id  User ID. Can be empty.
		 */
		return apply_filters( 'check_password', $check, $password, $hash, $user_id );
	}

	// If the stored hash is longer than an MD5, presume the
	// new style phpass portable hash.
	if ( empty($wp_hasher) ) {
		require_once( ABSPATH . WPINC . '/class-phpass.php');
		// By default, use the portable hash from phpass
		$wp_hasher = new PasswordHash(8, true);
	}

	$check = $wp_hasher->CheckPassword($password, $hash);

	/** This filter is documented in wp-includes/pluggable.php */
	return apply_filters( 'check_password', $check, $password, $hash, $user_id );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/compat.php: hash_equals()
  • wp-includes/pluggable.php: wp_set_password()
  • wp-includes/pluggable.php: wp_hash_password()
  • wp-includes/pluggable.php: check_password
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/user.php: wp_authenticate_email_password()
  • wp-includes/user.php: wp_authenticate_username_password()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Example

    
    $user = get_user_by( 'login', $username );
    if ( $user && wp_check_password( $pass, $user->data->user_pass, $user->ID ) ) {
    	echo "That's it";
    } else {
    	echo "Nope";
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文