PHP - Session variable does not display new content

0

I developed a system with PHP and am having problems with session variables ($ _SESSION). A 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 same, even though it was updated with a different value. If you perform a refresh of the page in the browser, the value of the variable displays the correct value. However this value is what will remain until a new refresh is executed in the browser. This occurs in both Chrome and Internet Explorer. I have no idea why this is occurring or how to resolve or circumvent the situation.

I've 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>';
    
asked by anonymous 04.07.2018 / 23:23

2 answers

0

In the example you mentioned here is appearing.

If this problem is occurring on your system make sure that some part of the code is assigning the empty NULL or "" value to the session. "

And make sure you are calling session_start () at the beginning of the code.

With me already occurred error of for example I assign the value of the session in a variable, after modifying the variable changed the value of the session as well. Make sure you did not do this too.

//Exemplo do que citei acima
$login = $_SESSION['login'];

$login = false; 
//No sistema o valor da sessão também foi alterado

I hope I have helped.

    
05.07.2018 / 19:16
0

First of all, thank you to everyone who answered my question. I have re-edited the published codes in this question, putting the two lines below:

header ('Cache-Control: no cache'); session_cache_limiter ('private_no_expire');

And it was these two statements that I omitted in the original question and that caused the problem in the behavior of the session variables. After having been removed in my code, the session variables passed the expected behavior.

    
08.07.2018 / 19:45