Problem-Users Cant Login Without Setting The Remember Me Option

The main problem is that the user cant login the site without the remember the option true. I have modified login function to check the user access and redirect to their particular action but it doesnt work if the user dont choose the remember me option at the time of login.Here is my code of login action.


if(isset($_POST['LoginForm']))

    {

        $model->attributes=$_POST['LoginForm'];

        if($model->validate() && $model->login()){

            $user_id = Yii::app()->user->id;


            $record=Users::model()->findByPk($user_id);

            if($record->masrole->name == 'Admin'){


                $this->redirect(array('//users/admin'));}

            if($record->masrole->name == 'Merchant'){ $this->redirect(array('//users/Description','user'=>$record->username));

            }


        }

    }

and here is my config file.


'components'=>array(


            'user'=>array(

                'class' => 'WebUser',

                'allowAutoLogin'=>true,

                'autoRenewCookie' => true,

                'identityCookie' => array('domain' => '.xxxxx.com'),

                'loginUrl'=>'http://xxxxx.com/login',

                ),

            'session' => array(

                    'class' => 'CDbHttpSession',

                    'cookieParams' => array('domain' => '.xxxxxx.com'),

                    'timeout' => 3600,

                'connectionID' => 'db',

                    'sessionName' => 'session',

                 ),

And this is my model login function


Public function login()

{

    if($this->_identity===null)

    {


        $this->_identity=new UserIdentity($this->email,$this->password);


        $this->_identity->authenticate();

    }

    if($this->_identity->errorCode===UserIdentity::ERROR_NONE)

    {


        $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

        Yii::app()->user->login($this->_identity,$duration);

        return true;

    }

    else{ 

        return false;

    }

}

This is Web User


class WebUser extends CWebUser {




private $_model;


 function isAdmin(){

   $user = $this->loadUser(Yii::app()->user->id);

   return intval($user->mas_role_id) == 1;


    }


   function isMerchant(){

    $user = $this->loadUser(Yii::app()->user->id);

    return intval($user->mas_role_id) == 2;

    }


 }


// Load user model.

  protected function loadUser($id=null)

  {


  if($this->_model===null)

  {

      if($id!==null)

      $this->_model=Users::model()->findByPk($id);

  }

  return $this->_model;

  }

}

This is User Identity


class UserIdentity extends CUserIdentity

{


private $_id;




public function authenticate()

{

    $ctiteria = new CDbCriteria;

    $ctiteria->condition = "email = '".$this->username."' OR username =     '".$this->username."'";

    $record=Users::model()->find($ctiteria);


    if($record===null)

        $this->errorCode=self::ERROR_USERNAME_INVALID;

    else if ($record->status != 1)

        $this->errorCode=self::ERROR_USERNAME_INVALID;


    else if($record->password !== md5($this->password))

        $this->errorCode=self::ERROR_PASSWORD_INVALID;

    else

    {

        $this->_id = $record->id;

        $this->setState('title', $record->email);

        $this->errorCode=self::ERROR_NONE;

    }

    return !$this->errorCode;


}

public function getId()

{

    return $this->_id;

}

}

function isAdmin(){

$user = $this->loadUser(Yii::app()->user->id);

return intval($user->mas_role_id) == 1;


}


 function isMerchant(){

  $user = $this->loadUser(Yii::app()->user->id);

  return intval($user->mas_role_id) == 2;

}

 protected function loadUser($id=null)

  {


  if($this->_model===null)

  {

      if($id!==null)

      $this->_model=Users::model()->findByPk($id);

  }

  return $this->_model;

    }

}

It looks like your issue is most directly related to this line.


$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

When the rememberMe field is set, the duration is set to 30 days, when not set, 0 days. That’s fine in itself, as if a value greater then 0 is passed through (remember me set), a cookie based authentication will be used, and if not, a session based authentication will be used. Seeing as how you’ve seemingly overrode some of the session configuration in your config file, I’d double check sessions are all working properly, as I’m betting that’s where the problem is.

Try clearing all of your browser’s cookies for the domains that you’ve specified and any that you’ve used to access the site. If you’ve changed the cookie domain, old cookies may cause conflicts.

Already cleared the cookies and and i didnt changed the cookie domain…I’ve already tried on another machine (to make sure there is no cookie or cache conflict).

This is my session setting.Why its not working??


 'session' => array(

                    'class' => 'CDbHttpSession',

                    'cookieParams' => array('domain' => '.xxxxxx.com'),

                    'timeout' => 3600,

                'connectionID' => 'db',

                    'sessionName' => 'session',

                 ),

You’re using the CDbHttpSession class. Are you checking to make sure sessions are being stored correctly in your database?

Sessions are stored in YiiSession table

Have you actually verified whether sessions are working properly? Try setting a session value with:


Yii::app()->session['hello'] = 'world';

And see if you can store and retrieve it properly across your application.

Are you saying that you can’t login from a login page without selecting a remember me option. is it so ?? If yes then could you please confirm your login form validation rules also. Might be remember is required on this form. So, what i mean check your validation rules for login model first. Is it working correctly or not.

Just to reply if someone is having the same problem yet.

It could well be just that you have a space in your sessions name in config/main.php

Remove spaces in name and set ‘cookieMode’ => ‘allow’ or ‘cookieMode’ => ‘only’.

Then clear your cookies and it should be working without remember me.

The validation rules are fine nothing an finally i got the solution…This is from the hosting server.They informed me that the cookies are not saving due to their technical problems.

The code is working fine now.