Php 用户名或密码总是不正确。-Yii2

Php 用户名或密码总是不正确。-Yii2,php,yii2,yii2-advanced-app,Php,Yii2,Yii2 Advanced App,我已经安装了Yii2高级应用程序,我根据我的用户数据库定制了注册。注册后,我尝试登录,它显示“用户名或密码不正确”,我的密码是qwerty,而且我已经检查了很多次,仍然无法使用 注册模式 class SignupForm extends Model { public $username; public $email; public $password; public $first_name; public $middle_name; public $last_name; public $co

我已经安装了Yii2高级应用程序,我根据我的用户数据库定制了注册。注册后,我尝试登录,它显示
“用户名或密码不正确”
,我的密码是qwerty,而且我已经检查了很多次,仍然无法使用

注册模式

class SignupForm extends Model
{
public $username;
public $email;
public $password;
public $first_name;
public $middle_name;
public $last_name;
public $contact;
public $birth_date;
public $type;
public $external_type;
public $status;
public $region_id;
public $barangay_id;
public $province_id;
public $city_municipal_id;


/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        ['username', 'trim'],
        ['username', 'required'],
        ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
        ['username', 'string', 'min' => 2, 'max' => 255],

        ['email', 'trim'],
        ['email', 'required'],
        ['email', 'email'],
        ['email', 'string', 'max' => 255],
        ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

        ['password', 'required'],
        ['password', 'string', 'min' => 6],

        ['first_name', 'required'],
        ['first_name', 'string', 'max' => 45],

        ['middle_name', 'string', 'max' => 45],

        ['last_name', 'required'],
        ['last_name', 'string', 'max' => 45],

        ['contact', 'required'],
        ['contact', 'string', 'max' => 11],

        ['birth_date', 'required'],

        ['type', 'required'],
        ['type', 'string', 'max' => 45],

        ['external_type', 'string', 'max' => 45],

        ['status', 'string', 'max' => 45],

        ['region_id', 'required'],
        ['barangay_id', 'required'],
        ['province_id', 'required'],
        ['city_municipal_id', 'required'],
    ];
}

/**
 * Signs user up.
 *
 * @return User|null the saved model or null if saving fails
 */
public function signup()
{
    if (!$this->validate()) {
        return null;
    }

    $user = new User();
    $user->username = $this->username;
    $user->email = $this->email;
    $user->setPassword($this->password);
    $user->generateAuthKey();

    $user->first_name = $this->first_name;
    $user->middle_name = $this->middle_name;
    $user->last_name = $this->last_name;
    $user->contact = $this->contact;
    $user->birth_date = $this->birth_date;
    $user->type = $this->type;
    $user->external_type = $this->external_type;
    $user->status = $this->status;
    $user->region_id = $this->region_id;
    $user->barangay_id = $this->barangay_id;
    $user->province_id = $this->province_id;
    $user->city_municipal_id = $this->city_municipal_id;

    return $user->save() ? $user : null;
}
}
我在哪里出错了?令人困惑的是,我认为我的代码没有错,因为我遵循了Yii2高级应用程序的正确安装设置

登录模型

public function login()
{
    if ($this->validate()) {
        return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
    }

    return false;
}

虽然您尚未添加注册或注册的操作,但在从模型调用
$model->signup()
函数时,必须在
if
语句中检查它,然后添加对
\Yii::$app->user->login($userModel)的调用内部,注册后您将登录

signup()
函数在表中插入用户后返回用户模型对象

请参阅下面的代码示例

if(($userModel=$model->signUp())!==null){
   \Yii::$app->user->login($userModel);
}

希望它能帮到你

虽然你还没有为注册或注册添加操作,但是在你从模型中调用
$model->signup()
函数时,你必须在
if
语句中检查它,然后添加对
\Yii::$app->user->login($userModel)的调用内部,注册后您将登录

signup()
函数在表中插入用户后返回用户模型对象

请参阅下面的代码示例

if(($userModel=$model->signUp())!==null){
   \Yii::$app->user->login($userModel);
}

希望对您有所帮助

您是否尝试过设置用户模型类以实现标识界面

首先像这样声明您的类

class MyUser extends ActiveRecord implements IdentityInterface
现在,您需要在类的某个地方实现这些方法

public static function findIdentity($id)
    {
        return self::findOne(['id'=>$id]);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException("Validation method not supported as of yet.");
    }

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

    public function getAuthKey()
    {
        return $this->auth_key;
    }

    public function validateAuthKey($authKey)
    {
        return $this->auth_key === $authKey;
    }
接下来转到/config/web.php的components部分,搜索
'user'
组件

'user' => [
        'identityClass' => 'app\models\MyUser', //<---Your model class
        'enableAutoLogin' => true,
    ],
“用户”=>[
'identityClass'=>'app\models\MyUser',//true,
],

我想你的话很好。如果不起作用,请告诉我。

您是否尝试设置用户模型类以实现标识界面

首先像这样声明您的类

class MyUser extends ActiveRecord implements IdentityInterface
现在,您需要在类的某个地方实现这些方法

public static function findIdentity($id)
    {
        return self::findOne(['id'=>$id]);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException("Validation method not supported as of yet.");
    }

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

    public function getAuthKey()
    {
        return $this->auth_key;
    }

    public function validateAuthKey($authKey)
    {
        return $this->auth_key === $authKey;
    }
接下来转到/config/web.php的components部分,搜索
'user'
组件

'user' => [
        'identityClass' => 'app\models\MyUser', //<---Your model class
        'enableAutoLogin' => true,
    ],
“用户”=>[
'identityClass'=>'app\models\MyUser',//true,
],


我想你的话很好。如果这不起作用,请告诉我。

检查phpmyadmin中的用户表,查看状态列,如果此值不等于10,可以更改为10。

检查phpmyadmin中的用户表,查看状态列,如果此值不等于10,可以更改为10。

首先,Yii2的正确安装与代码错误无关。第二,您仅在此处粘贴了
signup()
函数,因此,如果您询问
login()
,我们如何解决您的问题?因为当您注册时,yii2使用户已经正确登录?但在注册后,它会将我重定向到索引页,而用户没有登录。我已经尝试使用yii迁移使用默认用户表,它工作正常,但当我使用自己的用户表时,它不再工作。在注册代码后,我看不到记录用户。用适当的代码更新你的问题,目前我们帮不了你。我想这是我只能给出的代码,我只根据用户表列定制了注册表单模型属性。无论如何,感谢从您的控制器获得singup/register功能首先,Yii2的正确安装与您的代码错误无关。第二,您仅在此处粘贴了
signup()
函数,因此,如果您询问
login()
,我们如何解决您的问题?因为当您注册时,yii2使用户已经正确登录?但在注册后,它会将我重定向到索引页,而用户没有登录。我已经尝试使用yii迁移使用默认用户表,它工作正常,但当我使用自己的用户表时,它不再工作。在注册代码后,我看不到记录用户。用适当的代码更新你的问题,目前我们帮不了你。我想这是我只能给出的代码,我只根据用户表列定制了注册表单模型属性。无论如何,谢谢从您的控制器添加singup/register功能此功能已经在Yii2高级用户类中自动此功能已经在Yii2高级用户类中自动此功能实际上它已经在SiteController@noobkoder中您可以添加
$User->setPassword()函数虽然我可以看到密码在表中被散列,但当您在注册后被重定向到登录页面时,是否会显示错误的用户名/密码消息?尝试在
login()
函数中添加
var\u dump($this->validate())
print\r($this->attributes)
,查看打印内容。注册后,它会将我重定向到索引页,但用户未登录,但是如果我尝试登录,它会说凭据是错误的。实际上它已经在SiteController@noobkoder中了。你能添加
$user->setPassword()吗函数虽然我可以看到密码在表中被散列,但当您在注册后被重定向到登录页面时,是否会显示错误的用户名/密码消息?尝试在
login()
函数中添加
var\u dump($this->validate())
print\r($this->attributes)
,查看它打印的内容。注册后,它会将我重定向到索引页,但用户未登录,但如果我尝试登录,它会说凭据错误。