PHP/Smarty - Random Session data loss between 2 pages

I'm facing the weirdest issue I've ever seen with PHP. I will try to be really exhaustive because each detail can count.

I have 3 PHP pages, and their HTML is generated using Smarty. They all call session_start() at their top.

  • the first page can be heavy to load, it displays a big amount of clickable pictures that are previews of PHP complex objects
  • if a picture is chosen it leads to the second page with a specific URL.This page will generate a PHP object related to the URL info (so this page does not need info from previous page to work. If you copy-paste this URL at anytime it will work). This object will be stored into $_SESSION with a unique id.
  • the third page is included as an img src in the second page's HTML code (generated with Smarty). This is a PHP page having the object's session id in URL parameter and reading the object's info in $_SESSION, creating it's image preview and turns into an image with header('Content-type: image/jpeg').

To sum up : heavy first page => second page creating and storing a PHP object into $_SESSION => third page, included in second page's HTML code, searching for this object and creating a preview.

The problem is, sometimes, this third page is trying to load the object in $_SESSION and it's just not there !

Some facts :

  • the problem never occurs when the second page is loaded as a stand-alone with its own URL
  • the problem only occurs randomly when the heavy first page starts loading images and we furiously click on one of the first ones to appear
  • whatever the situation is, if I dump my $_SESSION variables at the very end of the second page I can ALWAYS see the generated object in my session

So it seems that the issue is located between the very end of the second page and the beginning of the third, but all of this related to the activity of the first one ! All I know is that it is linked to a fast navigation between pages.

I've tried almost everything :

  • session_write_close() everywhere it's possible (at the end of my pages before/after smarty display, then at the beginning before session_start())
  • add exit() at the end of my scripts
  • even sleep(1) at the beginning of the third in case session close operation needs more time

Nothing works. I have no more clues ... Maybe Smarty ? Maybe some odd PHP session behaviour named bug #4454 somewhere ?

Thanks a lot in advance for helping me with this.

Edit : piece of code after serialize() chat

/* ===== Page2.php ===== */

/*creating object $card and setting some values ...*/

/*calling the function that gives the card preview*/
$assigns['front'] = $card->getPreviewURL();

/*other stuff ... assign $assigns to smarty ... etc*/

/* ===== Card object class ===== */

function getPreviewURL()
  $_SESSION['products'][$this->getObjectId()] = serialize($this);
  $url = '/page3.php?s='.$this->getObjectId();
  return $url;

/* ===== Page2.html ===== */

img src="{$front}" alt="toto"

/* ===== Page3.php ===== */

/*getting id value and reading session*/

if(!empty($_GET['s'])) {
  $session = new Session;
  if(!empty($_SESSION['products'][$_GET['s']])) {
     $product = unserialize($_SESSION['products'][$_GET['s']]);
  else {
     log('$_SESSION[products]['.$_GET['s'].'] does NOT exist');
     header("Status: 404 Not Found");

and when bug occurs, I get :

page2.php debug log :
Session value : [o20aee110e0853e74da4d17c9b7ab3075]=>O:8:"Postcard":19:{s:4:"tmpl";O:16:"PostcardTemplate":20:{s:2:"id";s:3:"152";s:2:"or";i:0;s:3:"ord";s:2:"14";s:11:"description";s:0:"" ... etc
page3.php debug log :
$_SESSION[products][o20aee110e0853e74da4d17c9b7ab3075] does NOT exist


Probably, the problem is, that PHP tries to unserialize your object but cannot find the class definition.

You can do one of the following two things:

serialize() the object manually before you put it into the session and unserialize() it after you loaded the class definition.

Or: Use autoloading.

Need Your Help

Why would jQuery.get() submit POST request?

jquery ajax post dialog get

I have some code that calls a backend method called /partner/settings to perform one of two tasks:

Request.getParameterMap values not castable to string

java java-ee servlets

i am trying to get the complete parameter map from the request object and iterate over it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.