0
I have developed a system with PHP and I am having problems with session variables ($_SESSION). One page assigns a value to a session variable and calls another page. This other page receives the value correctly only on the first call. From the second call, the value of the session variable remains the previous one, even if it was updated with a different value. If I run a refresh of the page in the browser, the value of the variable shows the correct value. However this value is what will remain until a new refresh runs in the browser. This occurs in both Chrome and Internet Explorer. I have no idea why this is happening and how to resolve or circumvent the situation.
I created two pages that faithfully reproduce the problem:
//PAGINA 1:
header('Cache-Control: no cache');
session_cache_limiter('private_no_expire');
session_start();
echo '<br>ANTES';
if (isset($_SESSION['var1']))
echo '<br>var1=' . $_SESSION['var1'];
else
echo '<br>var1 não existe';
echo '<br>';
$_SESSION['var1'] = 'T';
echo '<br>conteudo var1 alterado para T<br>';
echo '<br>DEPOIS';
if (isset($_SESSION['var1']))
echo '<br>var1=' . $_SESSION['var1'];
else
echo '<br>var1 não existe';
echo '<br>';
echo '<br><a href="pagina2.php"><input type="button" value="Vai p/ pagina 2"></a>';
//PAGINA 2:
header('Cache-Control: no cache');
session_cache_limiter('private_no_expire');
session_start();
echo '<br>ANTES';
if (isset($_SESSION['var1']))
echo '<br>var1=' . $_SESSION['var1'];
else
echo '<br>var1 não existe';
echo '<br>';
$_SESSION['var1'] = '';
echo '<br>conteudo var1 alterado para NULL<br>';
echo '<br>DEPOIS';
if (isset($_SESSION['var1']))
echo '<br>var1=' . $_SESSION['var1'];
else
echo '<br>var1 não existe';
echo '<br>';
echo '<br><a href="pagina1.php"><input type="button" value="Vai p/ pagina 1"></a>';
In your question, put the result that is appearing in the example so that we understand what you expect to leave and what is being displayed.
– Felipe J. R. Vieira
On the first page 1 call, the var1 variable presents: ANTES: "var1 does not exist" DEPOIS: "var1=T" After clicking the "Go p/pagina2" button page 2 presents: ANTES: "var1=NULL" <-- should display the value "T" assigned by page 1 (this is the problem) AFTER: "var1=NULL"
– Luiz Bordoni
I have just reproduced the steps described and page 2 presented:
ANTES var1=T
.– Gabriel Faria