I can't understnad why this is not working. This code snippt is in my userscontroller.php
Does it need to be elsewhere?
public function actionCreate()
{
$model=new Users;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$model->password = crypt_SHA_512($model->password,'salt');
if($model->save())
$this->redirect(array('view','id'=>$model->users_id));
}
$this->render('create',array(
'model'=>$model,
));
}
You are making one mistake of not handling errors, and this is why you cannot understand why this is not working.
Use this snippet to understand why.
if($model->save())
$this->redirect(array('view','id'=>$model->users_id));
else
print_r($model->getErrors());
The correct way would be to call the model validate() function before saving, and display the error in your view.
if ($model->validate()) {
if ($model->save()) {
$this->redirect(array('view','id'=>$model->users_id));
}
}
$this->render('create',array(
'model'=>$model
));
and in your view :
<?php
$form=$this->beginWidget('CActiveForm' ...);
?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
Generate a salt and encode password by:
$salt = substr(str_shuffle("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0, 8);
$model->password = crypt($passwordInput, '$6$'.$salt);// '$6$' for SHA_512
And for confirmation:
if (crypt($passwordInput, $hashed) == $hashed) {
// Valid action
} else {
// Invalid action
}
Alternatively:
You can use Yii password helper.
Hash your password by:
$model->password = CPasswordHelper::hashPassword($password);
And for confirmation:
if (CPasswordHelper::verifyPassword($password, $hash))
// password is good
else
// password is bad