How to solve "session_regenerate_id(): Cannot regenerate session id - headers already sent"
Asked Answered
E

7

6

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:

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

the actionLogin''s code:

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.

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.

Epidermis answered 7/10, 2013 at 15:45 Comment(1)
you have to further look into if you have read/write access to runtime and any cache file issueDogooder
C
13

use

ob_start()

at the start of the file.

Capping answered 7/10, 2013 at 15:47 Comment(2)
Thank you, chandresh_cool. i did it and it's helped, yet! Is the output preventing going to safely influence the rest of the code? I do not inclute ob_end().?Epidermis
Hey Chandresh, thanks for your answer. I find it incomplete thought. Could you please explain why using ob_start() solves the issue?Hallowell
F
7

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().

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.

Fenelia answered 3/2, 2015 at 4:30 Comment(0)
P
1

I had an empty line at the beginning of main.php in Yii Project, removing that solved the issue for me.

Prud answered 7/10, 2013 at 15:45 Comment(0)
U
1

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 ?>

 /* white spaces here*/   <?php

    //YOUR CODE 

 ?> /* white spaces here 
Unyielding answered 1/1, 2016 at 5:16 Comment(0)
M
1

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 buffering in php.ini on the php-fpm container.

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.

Now in my php.ini I have this setting:

; 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.

May answered 12/8, 2018 at 6:9 Comment(1)
This did help me. Also a possible indication that you don't have any php.ini loaded at all, as the default output buffering setting is 0. In my case I was setting up an environment to test an old PHP app and I'd forgotten this step.Mirilla
B
0

Make user you not echoing anything in function where your are using $model->login()

Bayly answered 9/6, 2014 at 11:50 Comment(0)
P
0

try to use

ob_start();

if that won't work then try following

ob_start();
ob_clean();
Pericycle answered 26/6, 2020 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.