PHP - Session variable does not display new content

Asked

Viewed 54 times

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>';
  • 1

    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.

  • 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"

  • I have just reproduced the steps described and page 2 presented: ANTES var1=T.

2 answers

0

First of all, thank you to everyone who answered my question. I have reissued the codes published 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 from the original issue that caused the problem in the behavior of session variables. After they were removed in my code, session variables started to behave as expected.

0

In the example you quoted here is appearing.

If this problem is occurring in your system check if it is some part of the code is assigning the empty NULL or ""value to the session.

And make sure you’re making the session_start() call at the beginning of the code.

With me it has already occurred error for example I assign the session value in a variable, after modifying the variable changed the session value as well. Make sure you didn’t do it 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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.