在WordPress中主要用到两个函数wp_hash_password()
和 wp_check_password()
来对文本进行生成密文和对密文的验证。
wp_hash_password()
把一个文本生成加密密文。
- functionxa0wp_hash_password($password)xa0{
- xa0xa0xa0xa0globalxa0$wp_hasher;
- xa0xa0xa0xa0ifxa0(xa0emptyempty($wp_hasher)xa0)xa0{
- xa0xa0xa0xa0xa0xa0xa0xa0require_once(xa0ABSPATHxa0.xa0WPINCxa0.xa0'/class-phpass.php');
- xa0xa0xa0xa0xa0xa0xa0xa0//xa0Byxa0default,xa0usexa0thexa0portablexa0hashxa0fromxa0phpass
- xa0xa0xa0xa0xa0xa0xa0xa0$wp_hasherxa0=xa0newxa0PasswordHash(8,xa0true);
- xa0xa0xa0xa0}
- xa0xa0xa0xa0returnxa0$wp_hasher->HashPassword(xa0trim(xa0$passwordxa0)xa0);
- }
wp_check_password()
xa0通过把生成的密文和文本密码进行比对来验证密码。
- functionxa0wp_check_password($password,xa0$hash,xa0$user_idxa0=xa0'')xa0{
- xa0xa0xa0xa0globalxa0$wp_hasher;
- xa0xa0xa0xa0ifxa0(xa0emptyempty($wp_hasher)xa0)xa0{
- xa0xa0xa0xa0xa0xa0xa0xa0require_once(xa0ABSPATHxa0.xa0WPINCxa0.xa0'/class-phpass.php');
- xa0xa0xa0xa0xa0xa0xa0xa0//xa0Byxa0default,xa0usexa0thexa0portablexa0hashxa0fromxa0phpass
- xa0xa0xa0xa0xa0xa0xa0xa0$wp_hasherxa0=xa0newxa0PasswordHash(8,xa0true);
- xa0xa0xa0xa0}
- xa0xa0xa0xa0$checkxa0=xa0$wp_hasher->CheckPassword($password,xa0$hash);
- xa0xa0xa0xa0returnxa0apply_filters(xa0'check_password',xa0$check,xa0$password,xa0$hash,xa0$user_idxa0);
- }
简易的使用方法
- //生成加密密文
- $passwordxa0=xa0wp_hash_password("qcqx");
- //把密文和文本验证
- echoxa0wp_check_password("qcqx",$passwordxa0)xa0;
- //输出 1,对比成功
评论
0 访客 0