0

i have a PHP application that uses $_SESSION global to manage data inside the app. However i have a little problem when trying to update the values of $_SESSION, this is what i have done so far:

  1. do stuff
  2. unset($_SESSION['var'])
  3. do stuff
  4. $_SESSION['var']=newValue();

I call the php using $.post() like this:

$('#id').click(function(){
    $.post("page.php", function(data){
        //do stuff
        window.location = "goto.php";
    })
})

goto.php grabs the content from $_SESSION['var'] and construct a new html.

But, when the webpage goes to goto.php it loads the html that was created using the last version of $_SESSION['var']. Note that if i refresh the webpage, then it load the new content created with the new $_SESSION['var'].

Also this is not a regular behavior, sometimes it loads right without the page reload.

What i'm i doing so fundamentally wrong in here?

KoU_warch
  • 2,160
  • 1
  • 25
  • 46
  • Have you checked that it is loading the latest page by clearing your cache? – Sablefoste Jul 06 '12 at 19:27
  • Yes only by doing a page refresh it gets the right content. @SableFoste – KoU_warch Jul 06 '12 at 20:07
  • I am not an expert, but apparently the jQuery $.post does update your $_SESSION variable, but because it is an AJAX request, the current page displayed doesn't change until refreshed. Is your original page (that you call the $.post) goto.php, or page.php, or another page altogether? – Sablefoste Jul 06 '12 at 20:43
  • @SableFoste the page from which the $.post is called it's actually a different page. – KoU_warch Jul 06 '12 at 21:05

1 Answers1

1

This is almost certainly a cache issue. You can send certain headers to the browser to force it not to cache the page, and thus ensure a reload everytime.

You can do this in PHP using:

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

Source: How to control web page caching, across all browsers?

Community
  • 1
  • 1
Tushar
  • 8,019
  • 31
  • 38
  • hm, i asume this answer means that nothing is cached, for example images. Is there no half way to define what to cache and what not to cache? – KoU_warch Jul 06 '12 at 20:04
  • It's only for that specific page. Do you have images or anything of that sort on this page? – Tushar Jul 06 '12 at 20:24