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
  • 143
  • 1
  • 1
  • 10
  • 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

9 Answers9

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,587
  • 1
  • 8
  • 18
2

Alright Guys,

Here is Answer which work for me on 000webhost

If it's work for you dont forgot to leave comment and tap upvote

GO to public_html->.htacess

open file

paste this

php_flag output_buffering on;

BOOM it's work.

Kalpesh A. Nikam
  • 194
  • 1
  • 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
1

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
  • 26,923
  • 21
  • 73
  • 125
Samuel Ramzan
  • 1,660
  • 1
  • 15
  • 24
1

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
  • 282
  • 2
  • 14
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
  • 3,769
  • 2
  • 17
  • 41
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,241
  • 10
  • 27
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