gpt4 book ai didi

php - cakephp密码验证

转载 作者:可可西里 更新时间:2023-11-01 12:42:40 27 4
gpt4 key购买 nike

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']);
}

此代码无效,即使它们匹配,也会始终给出错误消息。此外,当我进行编辑时,由于没有密码字段,我会收到以下错误。有没有办法解决

Undefined index:  password [APP/models/airline.php, line 25]

最佳答案

你在使用 AuthComponent 吗?请注意,它会散列所有传入的密码字段(但不是“密码确认”字段,请使用 debug($this->data) 进行检查),因此这些字段永远不会相同。 Read the manual and use AuthComponent::password做检查。


话虽如此,这是我使用的东西:

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'.");
}
}

这很糟糕,原因如下:

  • 与表单紧密耦合,总是期望字段 password_control 出现。如果数据中没有,则需要使用字段白名单或禁用验证,即: $this->User->save($this->data, true, array('field1', 'field2 ')).
  • 按照 AuthComponent 的方式手动散列密码(因为无法完全访问模型中的组件)。如果您更改 AuthComponent 中使用的算法,您也需要在此处进行更改。

话虽如此,它可以透明地验证密码和密码控制字段并生成正确的错误消息,而无需在 Controller 中添加任何额外代码。

关于php - cakephp密码验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3760663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com