# Session\_start(): Session cannot be started

**URL:** <https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804>\
**Category:** Elements API\
**Created:** [March 20, 2025, 4:28pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804 "2025-03-20T16:28:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 20, 2025, 4:28pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804/1 "2025-03-20T16:28:06Z")

</div>

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

```auto
@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:

```auto
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:

```auto

<?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.

---

<div class="post-metadata">

**Author:** ![dan](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/dan/32/28516_2.png) [@dan](https://forums.realmacsoftware.com/u/dan)\
**Post date:** [March 20, 2025, 5:34pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804/2 "2025-03-20T17:34:16Z")

</div>

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](elementsapp://downloadDocument/KMgsHo7haXeM).

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

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 20, 2025, 6:08pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804/3 "2025-03-20T18:08:10Z")

</div>

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

```auto

<?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:

```auto
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.  
🤔

---

<div class="post-metadata">

**Author:** ![dan](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/dan/32/28516_2.png) [@dan](https://forums.realmacsoftware.com/u/dan)\
**Post date:** [March 20, 2025, 6:16pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804/4 "2025-03-20T18:16:46Z")

</div>

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:

 ![CleanShot 2025-03-20 at 6 .14.40@2x](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/5/1/51aa74fb55f70d475bb0c0acaaa0acfe1a642d12.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 20, 2025, 6:46pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804/5 "2025-03-20T18:46:45Z")

</div>

> [@dan](#):
>
> 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. 🙂

---

<div class="post-metadata">

**Author:** ![tpbradley](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/tpbradley/32/25503_2.png) [@tpbradley](https://forums.realmacsoftware.com/u/tpbradley)\
**Post date:** [March 24, 2025, 12:12pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804/6 "2025-03-24T12:12:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 24, 2025, 4:59pm UTC](https://forums.realmacsoftware.com/t/session-start-session-cannot-be-started/46804/7 "2025-03-24T16:59:27Z")

</div>

Thanks @tpbradley - no more whitespace before the PHP code. 👍
