cakephp密码验证
此代码不起作用,即使它们匹配,也始终会给出错误消息。此外,当我进行编辑时,由于没有密码字段,会出现以下错误。有什么补救办法吗cakephp密码验证,php,cakephp,passwords,Php,Cakephp,Passwords,此代码不起作用,即使它们匹配,也始终会给出错误消息。此外,当我进行编辑时,由于没有密码字段,会出现以下错误。有什么补救办法吗 var $validate = array( 'password' => array( 'passwordlength' => array('rule' => array('between', 8, 50),'message' => 'Enter 8-50 chars'), 'passwordequal' => a
var $validate = array(
'password' => array(
'passwordlength' => array('rule' => array('between', 8, 50),'message' => 'Enter 8-50 chars'),
'passwordequal' => array('checkpasswords','message' => 'Passwords dont match')
)
);
function checkpasswords()
{
return strcmp($this->data['Airline']['password'],$this->data['Airline']['confirm password']);
}
您正在使用AuthComponent吗?请注意,它会散列所有传入的密码字段(但不是“密码确认”字段,请使用
debug($this->data)
)进行检查),因此这些字段将永远不会相同。检查一下
话虽如此,我还是用了一些东西:
Undefined index: password [APP/models/airline.php, line 25]
这是不好的,原因如下:
- 与表单紧密耦合,始终希望存在一个字段
。如果数据中没有字段白名单,则需要使用字段白名单或禁用验证,即:密码\u控件
$this->User->save($this->data,true,array('field1',field2'))
- 以AuthComponent的方式手动散列密码(因为无法从模型中完全访问组件)。如果更改AuthComponent中使用的算法,也需要在此处进行更改
public $validate = array(
'password' => array(
'confirm' => array(
'rule' => array('password', 'password_control', 'confirm'),
'message' => 'Repeat password',
'last' => true
),
'length' => array(
'rule' => array('password', 'password_control', 'length'),
'message' => 'At least 6 characters'
)
),
'password_control' => array(
'notempty' => array(
'rule' => array('notEmpty'),
'allowEmpty' => false,
'message' => 'Repeat password'
)
)
);
public function password($data, $controlField, $test) {
if (!isset($this->data[$this->alias][$controlField])) {
trigger_error('Password control field not set.');
return false;
}
$field = key($data);
$password = current($data);
$controlPassword = $this->data[$this->alias][$controlField];
switch ($test) {
case 'confirm' :
if ($password !== Security::hash($controlPassword, null, true)) {
$this->invalidate($controlField, 'Repeat password');
return false;
}
return true;
case 'length' :
return strlen($controlPassword) >= 6;
default :
trigger_error("Unknown password test '$test'.");
}
}
我把它改成了
'passwordequal' => array('checkpasswords','message' => 'Passwords dont match')
另外,strcmp函数也有错误,因为它在上述代码中始终返回0(即False)
'passwordequal' => array('rule' =>'checkpasswords','message' => 'Passwords dont match')
这有帮助吗:?这应该考虑密码验证。对于使用身份验证的CakePHP 2.x用户,您可能会注意到“AuthComponent不再自动哈希它能找到的每个密码。”即,上述解决方案可能不是解决2.x问题的正确方法。 以下是我的解决方案: 必须创建名为match的方法(可以随意命名): $validate方法必须如下所示:
public function match($check, $with) {
// Getting the keys of the parent field
foreach ($check as $k => $v) {
$$k = $v;
}
// Removing blank fields
$check = trim($$k);
$with = trim($this->data[$this->name][$with]);
// If both arent empty we compare and return true or false
if (!empty($check) && !empty($with)) {
return $check == $with;
}
// Return false, some fields is empty
return false;
}
其中password2
是比较第一个password
字段的字段
我很高兴与大家分享D用于验证密码、旧密码和确认密码
public $validate = array(
'password' => array(
'match' => array(
'rule' => array('match', 'password2'),
'message' => 'Passwords doesnt match',
),
),
);
$this->datadata
是否有意?如果没有,那就是你的问题。我修复了上面的代码以删除额外的数据,但仍然收到错误。我可以查看发布数据的html表单吗?checkpassword函数中的“确认密码”中是否缺少下划线?strcmp
如果字符串相等,则返回0
<代码>0将被理解为false
,因此它将执行与您期望的完全相反的操作。哦,可怕的冗余!在这种情况下,应该使用返回strcmp(…)==0
。
public $validate = array(
'password' => array(
'match' => array(
'rule' => array('match', 'password2'),
'message' => 'Passwords doesnt match',
),
),
);
class Adminpassword extends AppModel
{
public $name = 'Admin';
public $primaryKey = 'id';
public $validate = array(
'oldpassword' => array(
array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Please Enter Current password'
),
array(
'rule' =>'checkcurrentpasswords',
'message' => 'Current Password does not match'
)
),
'password' => array(
array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Please Enter password'
),
array(
'rule' => array('minLength', 6),
'message' => 'Passwords must be at least 6 characters long.',
)
),
'cpassword' => array(
array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Please Enter Confirm password'
),
array(
'rule' => 'checkpasswords',
'required' => true,
'message' => 'Password & Confirm Password must be match.'
)
)
);
function checkpasswords() // to check pasword and confirm password
{
if(strcmp($this->data['Adminpassword']['password'],$this->data['Adminpassword']['cpassword']) == 0 )
{
return true;
}
return false;
}
function checkcurrentpasswords() // to check current password
{
$this->id = $this->data['Adminpassword']['id'];
$user_data = $this->field('password');
//print_r(Security::hash($this->data['Adminpassword']['oldpassword'], 'sha1', true));
if ($user_data == (Security::hash($this->data['Adminpassword']['oldpassword'], 'sha1', true)))
{
return true;
}
else
{
return false;
}
}
}