Session_start(): Session cannot be started

While using Elements Preview my Elements page with a DevPack component that has PHP code at the start like this:

@portal(pageStart, id: "com.logrunner.listblogposts", includeOnce: true)
<?php
// Version: 1.4.0 Logrunner Blog
error_log("THIS IS ANOTHER ELEMENTS TEST");
$debug = false;  // Set debugging true | false
session_start();
...

I am getting an error in the Elements Web Server Logs:

16:19:41: Error:2 on line 7: session_start(): Session cannot be started after headers have already been sent

Looking at the PHP code in index.php in the Preview document root, I see:



<?php
// Version: 1.4.0 Logrunner Blog
error_log("THIS IS ANOTHER ELEMENTS TEST");
$debug = false;  // Set debugging true | false
session_start();
...

There are two blank lines being inserted in front of the <?php statement.

If I edit index.php in the Preview document root and delete these two blank lines and then save it, the error goes away.

We’ve taken a quick look at this issue, and it seems to be working here on the latest build. Can you try this sample document.

Does it work for you? or perhaps you can get it to break and send it back?

Still getting the error. The output index.php file looks like this:



<?php
// Version: 1.4.0 Logrunner Blog
error_log("THIS IS AN ELEMENTS TEST");
$debug = false;  // Set debugging true | false
session_start();
echo "<div>Test PHP Output:<br>PHP OK</div>";
?>


<!doctype html>
<html class="scroll-smooth" lang="en">
 <head> 
  <meta charset="utf-8" /> 
  <meta http-equiv="x-ua-compatible" content="ie=edge" /> 
  <meta name="viewport" content="width=device-width" /> 
  <meta content="RapidWeaver Elements 0.6.7 (23385)" name="formatter" /> 
  <meta property="og:type" content="website" /> 
  <meta property="og:site_name" content="My Website" /> 
  <meta property="og:title" content="Untitled Page" /> 
  <meta property="og:url" content="https://www.com/index.php" /> 
  <title>Untitled Page</title> 
  <link href="index.css" media="all" rel="stylesheet" type="text/css" /> 
  <link rel="stylesheet" type="text/css" media="all" href="rw/styles/consolidated.css" /> 
 </head> 
 <body> 
  <div>
   Test Inline HTML:
   <br />HTML OK
  </div> 
  <script>if (window.RWPreviewingInternally == undefined) {var RWWebServerPageRequiresRefreshing = false;window.onblur = function () {	RWWebServerPageRequiresRefreshing = true;};window.onfocus = function (){	if (RWWebServerPageRequiresRefreshing === false) {return;}	var xmlhttp = new XMLHttpRequest();	xmlhttp.onreadystatechange = function() {		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {			location.reload();		}	};	xmlhttp.open("GET", "/RWWebServerIsAliveAndKicking.txt", true);	xmlhttp.send();};}</script>   
 </body>
</html>



Still getting two blank lines at the front and this is the error when I preview it:

17:59:07: Error:2 on line 7: session_start(): Session cannot be started after headers have already been sent

In case it is relevant, I am running Elements (0.6.7) on a Mac mini M4 Pro and Sequoia 15.3.2.
:thinking:

Can you try removing the space in the template to see if that fixes it for you, here’s an example of how it shoudl look:

We’ll obviously issue a proper fix, but this workaround might help for now while you’re working on your component.

Let me know how it goes.

It is still getting the error but there is a minor improvement in the output as there is now only one blank line at the front. This is on both my file and yours. :slight_smile:

I’ve just added a fix for this so any whitespace is trimmed from the page content. Hopefully the next build will solve this!

1 Like

Thanks @tpbradley - no more whitespace before the PHP code. :+1:

1 Like