Yii login


[/code]When I login to my site the moment I go to another page, the user information is lost and acts as if I'm a regular member. What is wrong with my code?


Code for controller login action:

[code]public function actionLogin()

    {

        $model=new LoginForm;


        // if it is ajax validation request

        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

        {

            echo CActiveForm::validate($model);

            Yii::app()->end();

        }


        // collect user input data

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

        {

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

            // validate user input and redirect to the previous page if valid

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

            {

                  $this->render('page1');

                  //exit;

            }

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

                

        }

        // display the login form

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

    }

Code for my UserIsentity component:


class UserIdentity extends CUserIdentity

{

    private $_id;


    /**

     * Authenticates a user using the User data model.

     * @return boolean whether authentication succeeds.

     */

    public function authenticate()

    {

        $user=User::model()->findByAttributes(array('username'=>$this->username));

        if($user===null)

        {

            $this->errorCode=self::ERROR_USERNAME_INVALID;

        }

        else 

        {

            if($user->password!==md5($this->password))

            {

                $this->errorCode=self::ERROR_PASSWORD_INVALID;

            }

            else

            {

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

                if(null===$user->last_login_time)

                {

                    $lastLogin = time('Y-m-d h:i:s');

                }

                else

                {

                    $lastLogin = strtotime($user->last_login_time);

                }

                $this->setState('lastLoginTime', $lastLogin); $this->errorCode=self::ERROR_NONE;

            }

        }

        return !$this->errorCode;

    }

 

    public function getId()

    {

        return $this->_id;

    }


}



I hope this is all you need to help me.

Can anyone help?

I wrote a lengthy reply here which did not answer her question. Removed!

Thank you for taking the time to answer my question. I just don’t see how that answers my problem.

I render a view file instead of redirecting the browser.

Anyways, I tried changing it to your way and it still didn’t work =<.

As for your question please elaberate so I can try to brainstorm.

I got it working! Just figured I’d share it with all of those experiencing similar difficulties.

In my protected/config/main.php I had a piece of code like this:


'session' => array(

            'sessionName' => 'Site Session',

            'class' => 'CHttpSession',

            'autoStart' => 'true',

        ),  

which I just commented out and - v’oula - it worked!!

Not sure why - but so long as it’s working…

Emily Dickinson:

It’s too bad you removed your answer because I bookmarked this thread because of it. An answer may not help the OP but it may help other people. Sad :frowning: