在yii中,我使用安全问题创建密码重置功能。首先,用户需要输入他的电子邮件ID。我在views->User1
中创建了emailform.php作为
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'email-form',
'enableClientValidation'=>true,
));
echo CHtml::textField('email');
echo CHtml::submitButton('Send');
$this->endWidget();
在控制器中我创建了方法
public function actionPassword() {
if (isset($_POST['email'])) {
$email = $_POST['email'];
//process email....
//....
}
$this->render('_emailForm');
}
现在我想检查用户表中是否存在此电子邮件ID。如果确实如此,那么我想向他展示一个安全问题。我该如何实现呢?
答案 0 :(得分:5)
这将帮助您入门,您可以在控制器中添加类似于此的方法,并在其上创建一个带有密码字段的视图。
public function actionPassword() {
if(isset($_POST['email'])) {
$record=User::model()->find(array(
'select'=>'email',
'condition'=>'email=:email',
'params'=>array(':email'=>$_POST['email']))
);
if($record===null) {
$error = 'Email invalid';
} else {
$newpassword = 'newrandomgeneratedpassword';
$record->password = $this->hash_password_secure($newpassword);
$record->save(); //you might have some issues with the user model when the password is protected for security
//Email new password to user
}
} else {
$this->render('forgetPassword'); //show the view with the password field
}
}
答案 1 :(得分:3)
如果您使用的是CActiveRecords,检查记录是否存在的正确方法是使用exists()函数,而不是find()函数。
$condition = 'email_d=:emailId';
$params = array(':emailId' => $emailId);
$emailExists = User::model()->exists($condition,$params);
if( $emailExists) )
{
//email exists
}
else
{
// email doesn't exist
}
答案 2 :(得分:1)
要检查数据库中的记录,您可以使用两个选项
$exists = ModelName::find()->where([ 'column_name' => $value])->andWhere(['column_name' => $value])->exists();
//returns true or false
和
$exists = ModelName::findOne(["column_name" => $value,"column_name"=>$value]);
检查
if ($exists)
{
// exit
}
else
{
// not exist
}
答案 3 :(得分:0)
$model = YourModel::model()->find('email = :email', array(':email' => $_POST['email']));
这与PDO一样使用:
$query = 'SELECT * etc.. WHERE email = :email';
$stmt = DB::prepare($query);
$stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
$stmt->execute();
etc...
这不安全吗?!
然后..
if( empty($model) )
{
// exit code
}
else
{
// success block
}