dousi4148 2013-08-18 15:26
浏览 85
已采纳

创建新用户时,在POST参数中显示用户名和密码

I need to hash and store a password from user input in login form in Yii. If I get them thru POST parameters like this:

$model->username=$_POST['User']['username'];
$model->password=crypt($_POST['User']['username']);// salt might be added
if($model->save())
  $this->redirect(array('view','id'=>$model->id));

this way I expose the uncrypted password in POST request. Other way is to them directly from login form like this:

public function actionCreate2()
{
    $model=new User;
    $model->username = $form->username;
    $model->password = crypt($form->password);
    if($model->save())
            $this->redirect(array('view','id'=>$model->id));

    $this->render('create',array(
        'model'=>$model,
    ));
}

but this does not work in my case with authenticating a saved user. The auth function:

public function authenticate()
{
    $users = User::model()->findByAttributes(array('username'=>$this->username));

    if($users == null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    elseif ($users->password !== crypt($this->password, $users->password))
    //elseif($users->password !== $this->password)
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
        $this->errorCode=self::ERROR_NONE;
    return !$this->errorCode;
}

How to do it in a proper way?

The more troubles appeared as i followed suggest of Samuel - the validating alarm message even before i enter anything, along with hashed password in input field.(see the picture): more trouble

When I still enter my username and my password instead of 'proposed' and press 'Create' the form is being sent with not crypted values (from POST request sniffing):

Form Data   view source   view URL   encoded
YII_CSRF_TOKEN:9758c50299b9d4b96b6ac6a2e5f0c939eae46abe
User[username]:igor23
User[password]:igor23
yt0:Create

but nothing is actually stored in db, nor crypted not uncrypted...

  • 写回答

1条回答 默认 最新

  • duanqi5333 2013-08-19 05:31
    关注

    Change your create method to:

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate() {
        $model = new User;
    
        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];
            $model->password = crypt($model->password, 'mysalt123');
    
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->primaryKey));
        }
    
        // Reset password field
        $model->password = "";
    
        $this->render('create', array(
            'model' => $model,
        ));
    }
    

    Change that elseif from this:

    elseif ($users->password !== crypt($this->password, $users->password))
    

    To this:

    elseif (strcmp(crypt($this->password, 'mysalt123'), $users->password))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题