5

Im getting an error in my wp-debug log saying "PHP Warning: session_start(): Cannot start session when headers already sent in ...hooks.php on line 228". Here is a code snippet from the file

function _action_fw_flash_message_backend_prepare() {
            if ( ! session_id() ) {
                session_start();    
            }
        }

session_start(); is on 228. I have read that session_start should be the first line of code to be executed in the file. Will this not affect the functionality of the code? (I have zero PHP experience)

user2730011
  • 161
  • 1
  • 1
  • 11
  • 1
    calls to session_start should be at the very top of the script before any html content has been output to the browser – Professor Abronsius Feb 28 '18 at 07:22
  • Does this answer your question? [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – showdev May 21 '21 at 18:22

11 Answers11

6

Move the session_start() to top of the page. and ob_flush();in footer or end of the script.

<?php
@ob_start();
session_start();
?>
Rahul
  • 1,617
  • 1
  • 9
  • 18
3

For me The actual problem was a space in start of PHP file befor PHP tag.The following code should be the first line of your PHP file.

<?php session_start(); ?>

The problem occurs when you write some Html code or even a space before above code, PHP send your HTML code to browser that is why it give the error that Header already send to browser.

rtraees
  • 312
  • 2
  • 15
2

Go to public_html->.htacess

open file

paste this

php_flag output_buffering on;
bschlueter
  • 3,817
  • 1
  • 30
  • 48
Kalpesh A. Nikam
  • 324
  • 3
  • 10
  • 2
    PHP Output Buffering changes the PHP behavior to send all data in one go instead of sending as soon as ready. So headers cant be already sent, hence no error. But it may slow down site as PHP has to be done before anything is sent – Jon Apr 19 '21 at 13:51
  • What is the problem and how does your code help? – showdev May 21 '21 at 18:24
2

After Upgrading to PHP 7.4 I started having the same error message even do I had all my tags and scripts in the right place, The problem for me, was in the document encoding, it was as UTF-8, so I changed it to UTF-8 without BOM and that did it. All is fine now.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Samuel Ramzan
  • 1,770
  • 1
  • 15
  • 24
0

I've been using WP Session Manager for all my projects. Takes all the hassle out of creating a session and you can use $_SESSION calls anywhere in your code. Like the other comments have stated, in order to start a session, you need to run the session_start() function before your headers are set so use the hook plugins_loaded to run your function.

function _action_fw_flash_message_backend_prepare() {
    if ( ! session_id() ) {
        session_start();    
    }
}

add_action( 'plugins_loaded', '_action_fw_flash_message_backend_prepare' );
Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44
0

Make it apart. Open and close your php header script like this:

<?php

session_start();

  if($_SESSION['Active'] == false){ /* Redirects user to login.php if not logged in */
    header("location:../index.html");
    exit;

        }
?>
vinuales
  • 31
  • 2
0

Had the same problem, I found the solution for me and it's quite simple:

The php file should start with <?php and nothing else before that. In my case I had a space before <?php; after removing it the error was gone.

kelvin
  • 1,421
  • 13
  • 28
0

start the <?php session_start(); at first line of your code and ensure there is no spacing before the <?php

Thilakraj
  • 1
  • 1
0

I had this problem with session() in the middle of my php-code, because i just start the session first there.
At the first line i had just an "<!-- todo -->" for notes.

That was the problem, "<?php" has to be at the top of all !!!

Solving:
After moving the notes-line to the bottom, and "<?php" at the very first point, all works. Even the session() in the middle of code and a "header ("Location: something.php");" later in code works fine.

Franek69
  • 1
  • 1
  • Please confirm that you intend this to answer the question at the top of this page - and not to present a similar problem along with its solution. – Yunnosch Oct 27 '21 at 08:50
0

I changed the php version for instance from php version 8.1 to php version 8.0 and it worked.

0

please check a Space Before start a session and your starting php tag

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '23 at 15:57