NO BUG: Session variables defined within a Code element do not persist

Browser: Chrome 110
OS: Windows
URL: Board Of Directors – Kiwanda Shores Maintenance Association (password = ‘pass’)
Video: Jam

Session variables defined within a Code element do not persist across pages. When a function that defines session variables is called within the Code element, those session variables will not carry over to subsequent pages. This issue occurs regardless of whether the function is declared within functions.php or the same Code element.

To replicate the issue on my website, enter “pass” for password on the Board page to see the session variables that were defined.

<?php
  
  $_SESSION['not_in_function'] = 77;
  
  function set_session_data__tap() {
    $_SESSION['user__tap'] = 'BruceWayne';
    $_SESSION['cart__tap'] = ['item1', 'item2'];
}

	set_session_data__tap();

    echo 'Session ID: ' . session_id();
    var_dump($_SESSION);  // Inspect session data
?>

Then proceed to the Contact page to see how the session variables do not persist.

<?php 
  
    echo 'Session ID: ' . session_id();
    var_dump($_SESSION);  // Inspect session data
?>

Thank You,

Travis

Are you sure you started the session with session_start() before any output?
You can’t start a session within a Code element. At this time, there has already been some output which prevents the session cookie header to be set.

See PHP: Basic usage - Manual

@MattHias
The code below is at the top of my functions.php. Does that not suffice?

add_action('init', function() {
    if (!session_id()) {
        session_start();
    }
}, 1);

Thanks!

I was dealing with a similar issue yesterday. I added this snippet into advanced scripts (could add to functions.php) with a priority of 1 to initiate the php session. Then I give “v” a default value, but this may not be needed for you.

<?php
function register_my_session() {
  if( !session_id() ) {
      session_start();
      if(!isset($_SESSION['v'])) {
         $_SESSION['v'] = 'b';
      }
  }
}
add_action('init', 'register_my_session');

Hi! Thanks for the advice. The snippet did not help.

This issue persists after updating to 1.12.2.

Hello @Travis,

I’ve tried to replicate this locally. First, I used the same init filter as you.

Then I opened two pages, and in first one, I added code element with $_SESSION['test'] = "DEMO";

And on second one, I’ve added another code element, with echo $_SESSION['test'];

After visiting page one, and then page two, the session variable was correct.

Can you try to replicate this simple one? And then let’s see if it works for you.

Matej

Now getting undefined key errors on page 2.

I apologize for forgetting to mention that the session variables were being defined in a template. That template is inserted by the Template component on page 1. I updated my tests.

Page 1 with Template component: Board Of Directors – Kiwanda Shores Maintenance Association

Code in template before password form:

<?php 
  
  $_SESSION['test_in_template_component'] = "DEMO";
  echo "echoing SESSION['test_in_template_component'] in template = " . $_SESSION['test_in_template_component'] . '<br>';
  
?>

Code in page 1 after password form:

<?php
  
  echo "echoing SESSION['test_in_template_component'] in page 1 = " . $_SESSION['test_in_template_component'] . '<br>';

	$_SESSION['test_in_page_1'] = "DEMO2";
	echo "echoing SESSION['test_in_page_1'] in page 1 = " . $_SESSION['test_in_page_1'] . '<br>';

?>

Page 2: Contact Us – Kiwanda Shores Maintenance Association

Code in page 2:

<?php 
  
  echo "echoing SESSION['test_in_template_component'] in page 2 = " . $_SESSION['test_in_template_component'] . '<br>';

	echo "echoing SESSION['test_in_page_1'] in page 2 = " . $_SESSION['test_in_page_1'] . '<br>';
?>

What do you think?

I needed to migrate the tests to other pages.

Page 1 with Template component embedded: Session Variables Page 1 – Kiwanda Shores Maintenance Association

Code in template:

<?php 
  
  $_SESSION['test_in_template_component'] = "DEMO";
  echo "echoing SESSION['test_in_template_component'] in template = " . $_SESSION['test_in_template_component'] . '<br>';
?>

Code in page 1:

<?php
  
  echo "echoing SESSION['test_in_template_component'] in page 1 = " . $_SESSION['test_in_template_component'] . '<br>';

	$_SESSION['test_in_page_1'] = "DEMO2";
	echo "echoing SESSION['test_in_page_1'] in page 1 = " . $_SESSION['test_in_page_1'] . '<br>';

?>

Page 2: Session Variables Page 2 – Kiwanda Shores Maintenance Association

Code in page 2:

<?php 
  
  echo "echoing SESSION['test_in_template_component'] that was defined in a template embedded on page 1 = " . $_SESSION['test_in_template_component'] . '<br>';

	echo "echoing SESSION['test_in_page_1'] that was defined on page 1 = " . $_SESSION['test_in_page_1'] . '<br>';

?>

After viewing page 1 in an incognito window, I get these errors on page 2:

Warning : Undefined array key “test_in_template_component” in /home1/kiwanda/public_html/wp-content/themes/bricks/includes/elements/code.php(225) : eval()'d code on line 3
echoing SESSION[‘test_in_template_component’] that was defined in a template embedded on page 1 =
Warning : Undefined array key “test_in_page_1” in /home1/kiwanda/public_html/wp-content/themes/bricks/includes/elements/code.php(225) : eval()'d code on line 5
echoing SESSION[‘test_in_page_1’] that was defined on page 1 =

The variables now persist when not in incognito mode. Is this normal?

Hi,

I’m still not sure how to reproduce your issue. Please try to disable all pluging and check for any server settings.

My test:

  1. I’ve set up a template with the following code element:

  2. I’ve inserted the template to the page like this:
    CleanShot 2025-03-03 at 13.15.27@2x
    and I’ve selected the template I’ve created in step 1.

  3. I’ve also added a code element on the page (as you can see in the screenshot above), with the content like this:

When I open a page, I can see the correct output from template, and from page.
CleanShot 2025-03-03 at 13.16.44@2x

Can you try with same setup?

Matej

I APOLOGIZE. Piotnet Grid caused the errors I described in my last post. Otherwise, session variables persist.

Hi,

thanks for your response :slight_smile: I’ll then mark the topic as no-bug. Best to then contact their support about this.

Best regards,
Matej