2015-06-02 14:57:08 4217次浏览 2条回答 0 悬赏 10 金钱

登录能够成功,但是就是登陆后右上角logout(admin)括号中的名称为空,登录的对象是自己创建的User对象

对象代码如下
不知道哪里错了,求各位大神帮助QAQ。

/**
* @inheritdoc
*/
class User extends \app\core\base\BaseActiveRecord implements \yii\web\IdentityInterface
{
    public $username;	
    public $password;
    public $rememberMe;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_name', 'user_pw'], 'required'],
            [['user_name', 'user_pw'], 'string', 'max' => 64]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_name' => 'User Name',
            'user_pw' => 'User Pw',
            'Email' => 'Email',
            'regtime' => 'Regtime',
        ];
    }

    public function scenarios()
    {
    	$parent = parent::scenarios();
    	$parent['login'] = ['username','password'];
    	$parent['register'] = ['user_name','user_pw','Email'];
    	return $parent;
    }
    

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAssignments()
    {
        return $this->hasMany(Assignment::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getItemNames()
    {
        return $this->hasMany(Item::className(), ['name' => 'item_name'])->viaTable('{{%assignment}}', ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLeavewords()
    {
        return $this->hasMany(Leaveword::className(), ['userid' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getReplywords()
    {
        return $this->hasMany(Replyword::className(), ['userid' => 'id']);
    }
    
    public static function findIdentity($id)
    {
    	return User::findOne(['id'=>$id]);
    }
    
    public static function findIdentityByAccessToken($token, $type = null)
    {
    	foreach (self::$users as $user) {
    		if ($user['accessToken'] === $token) {
    			return new static($user);
    		}
    	}
    
    	return null;
    }
    
    public function getId()
    {
    	return $this->id;
    }
    public function getAuthKey()
    {
    	return $this->auth_key;
    }
    
    public function validateAuthKey($authKey)
    {
    	return $this->auth_key === $authKey;
    }
    
    public function validatePassword($password,$password_hash)
    {
    	return Yii::$app->security->validatePassword($password, $password_hash);
    }
    
    public function login(){
    	if (!$this->validate()) {
    		return  false;
    	}
    	$user = User::findOne(['user_name'=>$this->username]);
    	if($user!==null)
    	{
    		if ($this->password === $user->user_pw) {
    			return Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0);
    		}
    		return false;
    	}
    	else {
    		return false;
    	}
    }
}

最佳答案

  • 回答于 2015-06-02 16:16 举报

    php.ini 里面设置一下session.auto_start=1

    1 条回复
    回复于 2015-06-02 18:15 回复

    解决方法找到了。。。虽然不是session。。。但是还是感谢你

您需要登录后才可以回答。登录 | 立即注册
zkh90644
助理

zkh90644

注册时间:2015-05-11
最后登录:2015-12-23
在线时长:2小时34分
  • 粉丝0
  • 金钱150
  • 威望0
  • 积分170

热门问题