Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用YII-属性“进行用户身份验证时出错;用户名称“;没有定义_Php_Authentication_Yii - Fatal编程技术网

Php 使用YII-属性“进行用户身份验证时出错;用户名称“;没有定义

Php 使用YII-属性“进行用户身份验证时出错;用户名称“;没有定义,php,authentication,yii,Php,Authentication,Yii,我正在尝试为一个YII应用程序进行数据库登录,我收到了这个错误。这是我的密码: <?php /** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identity the user. */ class UserIdentity extends CU

我正在尝试为一个YII应用程序进行数据库登录,我收到了这个错误。这是我的密码:

<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    private $_id;
    public function authenticate()
    {
        // $users=array(
        //  // username => password
        //  'username'=>'password',
        // );
        // if(!isset($users[$this->username]))
        //  $this->errorCode=self::ERROR_USERNAME_INVALID;
        // elseif($users[$this->username]!==$this->password)
        //  $this->errorCode=self::ERROR_PASSWORD_INVALID;
        // else
        //  $this->errorCode=self::ERROR_NONE;
        // return !$this->errorCode;

        $record=User::model()->findByAttributes(array('username'=>$this->username));
        print_r($record); 
        echo ($this->password); 
        echo ($record->password); 
         if($record===null)
             $this->errorCode=self::ERROR_USERNAME_INVALID;
         else if($record->password!==$this->password)
             $this->errorCode=self::ERROR_PASSWORD_INVALID;
         else
         {
             $this->_id=$record->id;
             $this->setState('title', $record->title);
             $this->errorCode=self::ERROR_NONE;
         }
         return !$this->errorCode;

    }

    public function getId()
    {
         return $this->_id;
    }
}

您没有在数据库中定义
title
属性。但是,如果要执行登录任务,请尝试以下操作:

else
     {
         $this->_id=$record->id;
         //$this->setState('title', $record->title);
         $this->username = $record->username; //try this code
         $this->errorCode=self::ERROR_NONE;
     }
CMIIW删除
打印($record);
echo($this->password);
echo($record->password)

把第44行改成

if(isset($record->title))
      $this->setState('title', $record->title);
错误“在非对象上调用成员函数getId()”是因为调用Yii::app()->user->id


如果上述修复不起作用,请复制您的用户模型

数据库中没有用户表的“标题”字段。。。您试图将其用作$record->title,因此出现了此错误。

解决方案非常简单。这部分代码导致了它

 else
 {
     $this->_id=$record->id;
     $this->setState('title', $record->title);
     $this->errorCode=self::ERROR_NONE;
 }
确保在用户表或用户模型引用的表中有一个名为“title”的列

有了这条线,

$this->setState('title', $record->title);
您正在尝试创建一个名为“title”的会话变量,并为用户记录的title列赋值

这是一个如何设置会话并在yii中检索会话的示例

Yii::app()->user->setState('username', $record->username); // define
Yii::app()->user->username // retrieve, can be access anywhere in the system
请阅读此处了解更多信息:


希望这有帮助。

观察错误中的
堆栈跟踪(附sceen快照)

CActiveRecord->获取(“标题”)

此错误的含义是“您正在访问模型中未定义的属性”。即,您的
用户
表没有列
标题


请打印并检查
$record
对象。还可以检查输入错误以跟踪错误。

如果用户模型没有标题属性,则设置$record->title将始终发送错误。至于另一个错误(与getId有关),请查看调用getId()方法的代码,该方法在代码中<代码>公共函数getId(){return$this->_id;}
我看到了这个函数,但是你在哪里调用getId()?上面的错误提到对非对象调用getId(),所以我想知道getId在哪里使用?也许在你的用户控制器里?放一个
die()打印后($record->attributes)
并让我们知道结果是什么。