1

I want to change timeout for sessions. I set the timeout 1 second.

whenever I sign in to system , I can use the signout service after even a minute, while it's just for logged-in users.

Note : I write my own code for sign in and sign out.

here is my code for signin and signout service :

public function actionSignin()
{
    $model= new Users();
    $model->scenario ="signin";

    if(isset($_POST['Users']))
      {

        $model->attributes=$_POST['Users'];
        $model->validate();

        if($model->hasErrors()){
            if($model->hasErrors("username"))
                $result=array("status"=>$model->getError("username"));
            else if($model->hasErrors("password"))
                $result=array("status"=>$model->getError("password"));
        }else{

            $user = $model->get_user();
            $result['user'] =  $user;
            //// creating session
            $session=new CHttpSession;
            $session->open();
            $session['name']=$user['username']; 
            $result=array("status"=>ErrorManager::get("OK");                    
        }
    }else{
        $result=array("status"=>ErrorManager::get("no_data_submitted"));        
    } 
    $this->renderPartial("/print_result",array("result"=>$result,));
}




public function actionSignout(){
    $session=new CHttpSession;
    if(isset($session['name']))
    {
        $result = array("status"=>ErrorManager::get("OK"));     
        $session->destroy();
        $this->renderPartial("/print_result",array("result"=>$result);
    }
}

and there is session configuration in my config file (main.php) :

'components'=>array(
    'session' => array (
        'class'=>'CHttpSession',
        'cookieMode' => 'allow',
        'timeout' => 1
    ),

Please tell me What the problem is...

Alireza Amiri
  • 511
  • 5
  • 24

1 Answers1

0

I recommend you use the exist login function which Yii has done to handle it. Just open file protected/model/LoginForm.php to see what you missing. Look at

public function login()

and this is what you were missing

Yii::app()->user->login($this->_identity,$duration); // $duration is what you have to set

If you still want use the session to handle your stuff by your self, problem with session timeout, I think it was not just Yii problem, it should be refer to

How do I expire a PHP session after 30 minutes?

Community
  • 1
  • 1
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39