2

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.

1
  • 8
    Please show us your code. Commented Apr 1, 2015 at 10:15

1 Answer 1

3

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');
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Anyway u are right, though I am not updating it as in the same file that I access it, the problem was session variables are loaded first and I couldn't get the updated value without reloading it. So I returned session variable from a php file and access it via ajax request, so that worked fine for me. Thank you because u gave me the idea that server code loading first and in the client side I can't get the updated value directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.