php 如何解决“session_regenerate_id(): 无法重新生成会话 id - 标头已发送”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19229055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve "session_regenerate_id(): Cannot regenerate session id - headers already sent"
提问by Igor Savinkin
I have moved a Yii app to another shared host. As the app was running ...index.php?r=site/login with login credentials, I got the warning:
我已将 Yii 应用程序移至另一个共享主机。当应用程序运行 ...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. Yet in my Notepad++ all files are saved as UTF-8 without BOM.
文档和论坛都说可能是 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)
函数(见上面的代码)来跟踪发送的标头,但没有结果。
回答by chandresh_cool
use
用
ob_start()
at the start of the file.
在文件的开头。
回答by Biswadeep Sarkar
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.
有时它在您的本地主机计算机中无关紧要,但在您的远程服务器中却很重要。
回答by Gaurav Sharma
I had an empty line at the beginning of main.php in Yii Project, removing that solved the issue for me.
我在 Yii 项目的 main.php 开头有一个空行,删除它为我解决了这个问题。
回答by TheVigilant
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
回答by rijam
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. I was able to compare the configuration files between the previous and new infrastructure and it turns out that I forgot to enable output bufferingin php.inion the php-fpm container.
在我的例子中,也是一个 Yii 应用程序,在部署到新的基础设施后登录 Web 应用程序时抛出了同样的错误。我能到以前的和新的基础设施之间的配置文件进行比较和事实证明,我忘了,使输出缓冲在php.ini中的PHP-FPM容器上。
This old discussionabout 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.iniI 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.
希望它也能帮助其他人。
回答by Tarun Gupta
Make user you not echoing anything in function where your are using $model->login()
让用户在使用 $model->login() 的函数中不回显任何内容