Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Auto Login and Redirect After Registration in yii?

Tags:

php

session

yii

When I use redirect function after auto login this error occur:

session_regenerate_id(): Session object destruction failed

Can anyone hep me?

<?php
public function actionRegister()
{
    $model = new UserProfileForm;

    $this->performAjaxValidation($model,'userProfile-form');

    if(isset($_POST['UserProfileForm']))
    {
        $model->attributes = $_POST['UserProfileForm'];
        if ($model->save()) 
        {
            $u = new LoginForm;
            $u->username = $model->username;
            $u->password = $model->password;
            $u->login();

            $this->redirect(Yii::app()->user->returnUrl);
        }
    }
    $this->render('register',array('model'=>$model,));
}

?>

like image 939
sj59 Avatar asked Jan 28 '26 11:01

sj59


1 Answers

The following works.

if($model->save()){            

    $identity=new UserIdentity($model->username,$model->password);
    $identity->authenticate();
    Yii::app()->user->login($identity);

    $this->redirect(Yii::app()->user->returnUrl);                
}

PS: $model->save() runs $model->validate() so you are repeating yourself

like image 182
topher Avatar answered Jan 29 '26 23:01

topher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!