title: Suppress session_start() Warnings (headers already sent)
body: <p>I have a situation where I need to call <code>session_start()</code> in the middle of the file multiple times. Obviously, as <code>session_start()</code> outputs buffer, I get the following warnings:</p>
  
  <ul>
  <li><code>Warning: session_start(): Cannot send session cookie - headers already sent by...</code></li>
  <li><code>Warning: session_start(): Cannot send session cache limiter - headers already sent...</code></li>
  </ul>
  
  <p>There was a lot of written on StackOverflow about header related warnings caused by <code>session_start()</code>. Now before you decide to mark this post as a duplicate, please continue reading - you will see, it is different. </p>
  
  <p>Most of the proposed solutions in the other threads recommend putting <code>session_start()</code> at the beginning of the file, before any PHP output is made. This can be suitable solution if <code>session_start()</code> needs to be called only once per file - however, it is not my case. </p>
  
  <p>I have a file download script (see the code below.) My script regularly, during the file download, updates '$_SESSION' variables with download progress status. I have then another script regularly issued through AJAX requests, which monitors download progress and informs my JavaScript code. </p>
  
  <p>To avoid locking the <code>$_SESSION</code> variables, I therefore have to call <code>session_start()</code> and <code>session_write_close()</code> multiple times per file download, which occurs in a loop in the PHP file. </p>
  
  <p>As every subsequent call to <code>session_start()</code> returns the warnings, which in my case are injected into the downloaded file, is there any programmatically correct way of suppressing them?</p>
  
  <p>Two solutions come to my mind now, but I am not happy with none of them:</p>
  
  <ol>
  <li>calling <code>@session_start()</code> instead of <code>session_start()</code> - this is not considered as a good practice, as it suppresses all warnings and errors returned by the function.</li>
  <li>calling <code>ob_clean()</code> to empty the output buffer at the end of every file write iteration, to remove the warnings injected into the buffer to be written to the file. - in this case, the warnings are still present, I do not correctly manage the situation, I simply erase the warning and ignore it.</li>
  </ol>
  
  <p>So my question, please, how to correctly call <code>session_start()</code> and <code>session_write_close()</code> multiple times per file and avoid getting warnings?</p>
  
  <h2>Code Sample:</h2>
  
  <pre><code>&lt;?php    
  
  //...    
  
  
  // file download in small pieces
  while(!feof($file_source))
  {
      $chunk_data = fread($file_source, $chunk_size);
      print($chunk_data);
      ob_flush();
      flush();
  
      $bytes_already_transferred += strlen($chunk_data);
      $percent_complete = round(($bytes_already_transferred / $file_size) * 100);
  
      // update session variable, but how to avoid warnings?
      if (session_status() == PHP_SESSION_NONE) session_start();
      $_SESSION['DownloadProgress'] = $percent_complete;
      session_write_close();
  
      usleep(1000);
  }
  
  fclose($file_source);
  ?&gt;
  </code></pre>