Yii Framework Session Not Saved After Login

风格不统一 提交于 2019-12-13 06:07:39

问题


We have the following function in /rpotected/components/UserIdentity.php:

public function authenticate()
    {
            $username = $this->username;
            $password = $this->password;

            $user = Users::model()->find('username=? AND password=?', array($username, $password));
            if($user === NULL){
                    $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;

            }else{
                    $this->username = $user->username;
                    sess('SESS_USER_INFO', $user->attributes);
                    //print_r(sess('SESS_USER_INFO'));
                    $this->errorCode=self::ERROR_NONE;
            }

            return !$this->errorCode;
    }

Here's a snippet from /protected/models/Users.php:

public function login()
{            
        if($this->_identity===null)
        {
                $username = $this->username;
                $password = md5($this->password);
                //echo "Username: ".$username."<br />Password:".$password;
                $this->_identity=new UserIdentity($username, $password);
                $this->_identity->authenticate();

        }
        if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
        {
                $duration=$this->rememberMe ? 3600*24*30 : 60*20; // 30 days
                //print_r($this->_identity);
                Yii::app()->user->login($this->_identity,$duration);

                //echo "Login Successful";
                return true;
        }
        else{
                //echo "Error";
                $this->addError('password','Incorrect username or password.');
                return false;
        }

Problem: Upon login, clicking on My Profiles link prompts for login again. Thus, it seems that the session is not storing/saving the login credentials and carrying them through the login lifespan.

How should the authenticate function be modified so that the session information is stored and so that the login credentials are carried forward?


回答1:


please check "session.cookie_lifetime" value in php.ini

session.cookie_lifetime = 2592000



回答2:


You can store login credentials in a variable of CWebUser class through this way $this->setState(loogedInUser,$user); using setState function.

This information is stored in cookies, not is session and you can access it using Yii::app()->user->loogedInUser at any where.



来源:https://stackoverflow.com/questions/11750033/yii-framework-session-not-saved-after-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!