我已经将一个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)
函数(见上面的代码)来跟踪发送的头,但没有结果。
7条答案
按热度按时间zd287kbt1#
使用了
在文件的开头。
a6b3iqyw2#
一般来说,当我们在回显或打印后发送头时会出现此错误。如果此错误出现在特定页面上,则在调用start_session()之前请确保该页面没有回显任何内容。
不可预测错误示例:
有时,它在本地主机上并不重要,但在远程服务器上却很重要。
1bqhqjot3#
我在Yii Project的main.php开头有一个空行,删除它就解决了这个问题。
brc7rcf04#
从我的经验来看,这主要是一个白色错误。有时在开始的php标签前有一个空格,有时在文件结束后有一个空格。
cwdobuhd5#
在我的例子中,同样是一个Yii应用,在部署到新的基础架构后,在登录到Web应用时也会抛出同样的错误。我能够比较以前和新基础架构之间的配置文件,结果发现我忘记在php-fpm容器的php.ini中启用 * 输出缓冲 *。
This old discussion关于Yii在GitHub上的问题板上的错误信息给了我在配置文件中寻找什么的提示。
现在在我的php.ini中有这样的设置:
希望它也能帮助其他人。
p4tfgftt6#
使用户在使用$model-〉login()的函数中不回显任何内容
flseospp7#
尝试使用
如果这不起作用,请尝试以下操作