2

I used ZFcuser module for my admin panel . when I login into admin panel its returns error like this :

Warning: session_regenerate_id(): Cannot regenerate session id - headers already sent in /home/public_html/dev/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php on line 260

This works fine in my local windows system but its returns such warning while run in my linux hosting server.

Need help to solve this issue. Thanks in advance.

Chirag Shah
  • 1,463
  • 2
  • 18
  • 40
  • 1
    make sure output buffering is enabled in php.ini on your hosting server – Xerkus Mar 09 '14 at 00:26
  • my best guest would be that 1 of your files has something at the beginning of it that windows(or most likely php in your windows) is ignoring it, but linux(php) is considering it as a output, disable ZFcuser module and try using the session manualy see it it works? – Exlord Mar 09 '14 at 14:23
  • Check the encoding of your files. Are they all UTF-8 without BOM? – func0der Mar 10 '14 at 08:29
  • You could try to track the file that send header befor your session regenerate by throwing an exception in SessionManager::regenerateId(). That wait you can analyze the stacktrace and check classes for a possible output – SmasherHell Mar 10 '14 at 11:22
  • @Shahrokhian, they are different questions. If you work with php you understand that sessions with ZF2 has some issues that doesn't happen with a pure php sessions. – mold Jul 27 '20 at 09:27

3 Answers3

6

You can regenerate your session id with the ZF2 SessionManager:

$manager = new Zend\Session\SessionManager;
$manager->regenerateId();

However, your error is likely not caused by this function, but because of something else. Session management (either session_start() or regenerating session ids) is only possible when you haven't used echo or something similar before that piece of code:

As an example, this will fail:

<?php
echo 'Hello';
session_start();

On the other hand, this will just work:

<?php
session_start();
echo 'Hello';

You have to be very careful with opening and closing php tags. If you have any closing php tag in your classes, remove them. If there is blank line after this, it's interpreted as text which is returned in the response and this causing troubles with sessions. This will fail as well:

<?php
// Do some work
?>
<!-- blank line -->
<?php
session_start();
?>

So: check all your code for echo, print etcetera. Also, check for any closing php tag in your source code. If all above things won't help you, post the piece of code where you are regenerating your session id, that might give some more insights.

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99
  • Hi Jurian thanks for your reply but I used zfcuser module its third party module provide in zf2 . so I dont think there is echo or print is available before session_start() OR php end close error. Might be any another issue is possible? – Chirag Shah Mar 05 '14 at 14:14
  • @Chirag I guess you're not only using ZfcUser as a module. What happens if you turn off **ALL** other modules (including Application)? And if that doesn't solve the issue, what's your index.php file looks like? Also, do you have the required version of php installed? – Jurian Sluiman Mar 05 '14 at 14:22
2

This error typically appears when you dump some characters to PHP standard output. This results in sending the HTTP headers to web browser, so you cannot add a session header anymore.

To resolve the issue, ensure you do not echo or printf any character before you start the session. Also ensure that your PHP class files do not have closing ?> tags. If you have some characters after the ?> tags in your PHP class files, those characters will be dumped to stdout, breaking your session.

OlegKrivtsov
  • 752
  • 4
  • 9
-2

Please check following

protected function _initSession() {
    $config = array(
        'name'  => 'session',
        'primary'  => 'id',
        'modifiedColumn' => 'modified',
        'dataColumn' => 'data',
        'lifetimeColumn' => 'lifetime',
        'lifetime' => 60*60*24*30,
    );

    Zend_Session::setSaveHandler(new F_Session_SaveHandler_DbTable($config));        
}