簡體   English   中英

Yii2登錄功能不起作用

[英]Yii2 login Function not working

我已經按照 nenad/yii2-basic-template 的教程進行操作,應用程序似乎已完美設置。 除了登錄腳本之外的所有內容。 我懷疑實際代碼有問題,但我的主機設置有問題。 首次設置應用程序時,我創建了一個超級管理員用戶沒問題,之后創建用戶也沒有問題,數據庫中的所有內容都已檢查並正確無誤。 當我完成登錄過程時,它似乎沒有正確存儲會話值。 如果我從站點控制器內的登錄功能退出應用程序並返回 Yii::$app->user->identity 輸出是 id 期望看到的,但是如果我允許應用程序呈現視圖並且我輸出視圖中的相同變量返回 null。

我在運行 apache2、php 5.6、mysql 14.4 和其他無關包的流浪盒上運行應用程序。 如果有人願意,我可以上傳任何代碼或配置文件。 提前致謝。

編輯

第一部分是 _protected/controllers/siteController.php 中的登錄函數,第二部分是從 _Protected/views/site/index.php 開始的視圖文件

/**
 * Logs in the user if his account is activated,
 * if not, displays appropriate message.
 *
 * @return string|\yii\web\Response
 */
public function actionLogin()
{
    if (!Yii::$app->user->isGuest) 
    {
        return $this->goHome();
    }

    // get setting value for 'Login With Email'
    $lwe = Yii::$app->params['lwe'];

    // if 'lwe' value is 'true' we instantiate LoginForm in 'lwe' scenario
    $model = $lwe ? new LoginForm(['scenario' => 'lwe']) : new LoginForm();

    // now we can try to log in the user
    if ($model->load(Yii::$app->request->post()) && $model->login()) 
    {
        var_dump(Yii::$app->user->identity);
        // var_dump($_SESSION);
        // if (!is_writable(session_save_path())) {
        //     echo 'Session path "'.session_save_path().'" is not writable for PHP!'; 
        // } else {
        //     echo 'Session Path "'.session_save_path().'" IS writable for PHP!';
        // }
        // phpinfo();
        exit;
        return $this->goBack();
    }
    // user couldn't be logged in, because he has not activated his account
    elseif($model->status === User::STATUS_NOT_ACTIVE)
    {
        // if his account is not activated, he will have to activate it first
        Yii::$app->session->setFlash('error', 
            Yii::t('app', 'You have to activate your account first. Please check your email.'));

        return $this->refresh();
    }    
    // account is activated, but some other errors have happened
    else
    {
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}



<?php

/* @var $this yii\web\View */
$this->title = Yii::t('app', Yii::$app->name);
var_dump(Yii::$app->user->identity);
?>
<div class="site-index">

    <div class="jumbotron">
        <h1>Congratulations!</h1>

        <p class="lead">You have successfully installed Yii2 improved application template</p>

        <p><a class="btn btn-lg btn-success" href="http://www.freetuts.org/tutorial/view?id=6">Read our tutorial</a></p>
    </div>

    <div class="body-content">

        <div class="row">
            <div class="col-lg-3">

如您所見,我在登錄函數中退出應用程序,var 轉儲中的數據是 id 期望看到的,但是如果我讓應用程序轉發到視圖,用戶身份將返回為 null。

如果您在本地主機上使用 Yii2,則默認情況下STATUS_ACTIVE的值(在可以在公共文件夾中找到的用戶文件中)設置為 10。例如:

const STATUS_ACTIVE = 10;

將此值更改為const STATUS_ACTIVE = 9;

在此之后,您將能夠完美登錄。

最簡單的解決方案是更改數據庫表中的狀態值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM