php登陆页的密码处理方式分享
程序员文章站
2022-06-29 16:24:15
控制器里面:elseif(!$model->validatepassword($data->password))
复制代码 代码如下:
<?php
class xbasemodel extends cactiverecord
{
/**
* 检测用户密码
*
* @return boolean
*/
public function validatepassword ($password)
{
return $this->hashpassword($this->password) === $password;
}
/**
* 密码进行加密
* @return string password
*/
public function hashpassword ($password)
{
return md5($password);
}
}
public function validatepassword($password) {
return $this->hashpassword($password, $this->salt) === $this->password;
}
public function hashpassword($password, $salt) {
return md5(md5($password) . $salt);
}
public function generatesalt() {
$str = '1234567890abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
$len = strlen($str) - 1;
$string = '';
for ($i = 0; $i < 6; $i++) {
$string .= $str[mt_rand(0, $len)];
}
return $string;
}
public function validatepassword($password) {
return $this->hashpassword($password,$this->salt)===$this->password;
}
public function hashpassword($password,$salt)
{
return md5($salt.$password);
}
protected function generatesalt()
{
return uniqid('',true);
}
控制器里面:elseif(!$model->validatepassword($data->password))
复制代码 代码如下:
<?php
class xbasemodel extends cactiverecord
{
/**
* 检测用户密码
*
* @return boolean
*/
public function validatepassword ($password)
{
return $this->hashpassword($this->password) === $password;
}
/**
* 密码进行加密
* @return string password
*/
public function hashpassword ($password)
{
return md5($password);
}
}
或是:
if ($user && $user->password == $user->hashpassword($this->password, $user->salt)) {
复制代码 代码如下:
public function validatepassword($password) {
return $this->hashpassword($password, $this->salt) === $this->password;
}
public function hashpassword($password, $salt) {
return md5(md5($password) . $salt);
}
public function generatesalt() {
$str = '1234567890abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
$len = strlen($str) - 1;
$string = '';
for ($i = 0; $i < 6; $i++) {
$string .= $str[mt_rand(0, $len)];
}
return $string;
}
或是:
复制代码 代码如下:
public function validatepassword($password) {
return $this->hashpassword($password,$this->salt)===$this->password;
}
public function hashpassword($password,$salt)
{
return md5($salt.$password);
}
protected function generatesalt()
{
return uniqid('',true);
}
注意:如果有salt,数据库里面字段要有salt。。