PHP $_SESSION overwritten variable not passing to next page

102 Views Asked by At

Problem: in the code below when I move from page from sandbox1 to sandbox2 the new value of $_SESSION[page_name] is not being passed on and reverts back to the initial value of index. If after entering the page I reload the page the new value seems to magically show up. Is there something wrong with the code? Could you explain why the new value of $_SESSION is not propagating?

Page index.php

<?php
session_start(); 
$_SESSION['page']="index";
echo "This page: ".$_SESSION['page']."<br><br>";
?>

Page sandbox1.php

<?php
session_start(); 
echo "From page: ".$_SESSION['page']."<br>";    
$_SESSION['page']="Sandbox1";
echo "This page: ".$_SESSION['page']."<br><br>";
?>

Page sandbox2.php

<?php
session_start(); 
echo "From page: ".$_SESSION['page']."<br>";
$_SESSION['page']="Sandbox2";
echo "This page: ".$_SESSION['page']."<br><br>";
?>

On loading index.php it outputs:

This page: index

as expected.

Hyperlink from index.php to Sandbox1 output

From page: index

This page: Sandbox1

(new value $_SESSION['page']) as expected.

Hyperlink from Sandbox1 to Sandbox2 output:

From page: index

($_SESSION revert to value index) expected to see

From page: sandbox1

This page: Sandbox2

(new value)as expected.

Additional information PHP version 5.3. Register global is set to "OFF"

2

There are 2 best solutions below

6
Len_D On

On sandbox1 and sandbox 2, move session_start() above the line that reads

echo "From page: ".$_SESSION['page']."<br>";
0
Sam5596 On

Issue resolved. The PHP code is functioning correctly. The problem seems to only affect IE 11 and is related to the refresh cycle of this browser. Other browsers displayed the expected out. The problem was correct by changing the Apache server .htaccess file to suppress catching in test files used for debugging.

Investigating whether the problem is with IE-11 or specific test-bench configuration of this browser.