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