Search code examples
phpsessionreload

php session variables not updating


I have a php session variable and I update it in a page and access it in different page, when the variable comes to a threshold I reset it to zero and increment it according to the number of files uploaded.

The problem is after it reaches to the threshold when I reset and reload the page again the file that I access the session variable not updating and shows zero though the files are uploaded.

But actually the session variable is incrementing in the other page. It shows the real value(the value incremented) after only reloading twice.

I want to know why it happens.


Solution

  • First of all, It is critical to show your codes for your question, but for this particular one, the problem is that you are using session variables right in the file that you updated them. sessions load before anything in the page so, when you update them they are already loaded and although they have changed, their value is still the same in current page. To prevent that, you need to either go to another page after updating the session or put a redirect after your session change statement if it is necessary to stay in same address, like this:

    <?php
    if(threshold){
    $_SESSION['your_session']='your new value';
    header('LOCATION sample.php');
    }
    ?>