yii 如何求解“session_regenerate_id():无法重新生成会话ID -标头已发送”

cunj1qz1  于 2022-11-09  发布在  其他
关注(0)|答案(7)|浏览(165)

我已经将一个Yii应用程序移到了另一个共享主机上。当应用程序使用登录凭证运行时,我收到了警告:

session_regenerate_id(): Cannot regenerate session id - headers already sent

actionLogin的代码:

public function actionLogin($name = null )
{
    $model=new LoginForm;
    if ($name) $model->username = $name;
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if (headers_sent($filename, $linenum))
               {
            echo "Headers have been sent in {$filename} line number is {$linenum}\n"
            exit;
        }

        if($model->validate() && $model->login())
            $this->redirect(Yii::app()->user->returnUrl);
    }
    // display the login form
    $this->render('login',array('model'=>$model));
}

文档和论坛都说BOM可能有问题,但在我的记事本++中,所有文件都保存为UTF-8,没有BOM
我应该对文件做特殊检查吗?哪些文件?或者可能有其他错误原因?
我添加了headers_sent($filename, $linenum)函数(见上面的代码)来跟踪发送的头,但没有结果。

zd287kbt

zd287kbt1#

使用了

ob_start()

在文件的开头。

a6b3iqyw

a6b3iqyw2#

一般来说,当我们在回显或打印后发送头时会出现此错误。如果此错误出现在特定页面上,则在调用start_session()之前请确保该页面没有回显任何内容。
不可预测错误示例:

<?php //a white-space before <?php also send for output and arise error
session_start();
session_regenerate_id();

//your page content

有时,它在本地主机上并不重要,但在远程服务器上却很重要。

1bqhqjot

1bqhqjot3#

我在Yii Project的main.php开头有一个空行,删除它就解决了这个问题。

brc7rcf0

brc7rcf04#

从我的经验来看,这主要是一个白色错误。有时在开始的php标签前有一个空格,有时在文件结束后有一个空格。

/* white spaces here*/   <?php

    //YOUR CODE 

 ?> /* white spaces here
cwdobuhd

cwdobuhd5#

在我的例子中,同样是一个Yii应用,在部署到新的基础架构后,在登录到Web应用时也会抛出同样的错误。我能够比较以前和新基础架构之间的配置文件,结果发现我忘记在php-fpm容器的php.ini中启用 * 输出缓冲 *。
This old discussion关于Yii在GitHub上的问题板上的错误信息给了我在配置文件中寻找什么的提示。
现在在我的php.ini中有这样的设置:

; Note: Output buffering can also be controlled via Output Buffering Control
;   functions.
; Possible Values:
;   On = Enabled and buffer is unlimited. (Use with caution)
;   Off = Disabled
;   Integer = Enables the buffer and sets its maximum size in bytes.
; Note: This directive is hardcoded to Off for the CLI SAPI
; Default Value: Off
; Development Value: 4096
; Production Value: 4096
; http://php.net/output-buffering
output_buffering = 4096

希望它也能帮助其他人。

p4tfgftt

p4tfgftt6#

使用户在使用$model-〉login()的函数中不回显任何内容

flseospp

flseospp7#

尝试使用

ob_start();

如果这不起作用,请尝试以下操作

ob_start();
ob_clean();

相关问题