简体   繁体   English

如何解决“session_regenerate_id():无法重新生成会话ID - 标题已经发送”

[英]How to solve “session_regenerate_id(): Cannot regenerate session id - headers already sent”

I have moved a Yii app to another shared host.我已将 Yii 应用程序移至另一个共享主机。 As the app was running ...index.php?r=site/login with login credentials, I got the warning:当应用程序运行 ...index.php?r=site/login 时,我收到了警告:

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

the actionLogin ''s code: 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));
}    

The docs and forums have said that there may be a problem with the BOM.文档和论坛都说可能是 BOM 有问题。 Yet in my Notepad++ all files are saved as UTF-8 without BOM .然而在我的 Notepad++ 中,所有文件都保存为UTF-8 而没有 BOM

Should I do the special check of the files?我应该对文件进行特殊检查吗? Which ones?哪些? Or might there be another cause of the error?或者可能有其他错误原因?

I've added headers_sent($filename, $linenum) function (see in the code above) to trace the sent headers but with no result.我添加了headers_sent($filename, $linenum)函数(参见上面的代码)来跟踪发送的标头,但没有结果。

use使用

ob_start()

at the start of the file.在文件的开头。

Generally this error arise when we send header after echoing or printing.通常,当我们在回显或打印后发送标头时会出现此错误。 If this error arise on a specific page then make sure that page is not echoing anything before calling to start_session().如果此错误出现在特定页面上,请确保该页面在调用 start_session() 之前没有回显任何内容。

Example of Unpredictable Error:不可预知的错误示例:

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

//your page content

Sometimes it dose not matter in your localhost computer, but matters in your remote server.有时它在您的本地主机计算机中无关紧要,但在您的远程服务器中却很重要。

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

From My experience its mostly an error of white spaces .根据我的经验,它主要是空格错误。 Sometimes a space before the opening php tag or some times a space in the end of your file after the closing ?>有时在打开的 php 标签之前有一个空格,有时在关闭后的文件末尾有一个空格?>

 /* white spaces here*/   <?php

    //YOUR CODE 

 ?> /* white spaces here 

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

In my case, also a Yii app, the same error was thrown upon logging in into the web app after deploying to a new infrastructure.在我的例子中,也是一个 Yii 应用程序,在部署到新的基础设施后登录 Web 应用程序时抛出了同样的错误。 I was able to compare the configuration files between the previous and new infrastructure and it turns out that I forgot to enable output buffering in php.ini on the php-fpm container.我能够比较旧基础设施和新基础设施之间的配置文件,结果是我忘记在 php-fpm 容器上的php.ini 中启用输出缓冲

This old discussion about the error message on Yii's issues board on GitHub gave me the hint on what to look for in the config files.这个关于 GitHub 上 Yii 问题板上的错误消息的旧讨论给了我在配置文件中寻找什么的提示。

Now in my php.ini I have this setting:现在在我的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

Hope it helps others too.希望它也能帮助其他人。

try to use尝试使用

ob_start();

if that won't work then try following如果那不起作用,请尝试以下

ob_start();
ob_clean();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM