Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Storing Objects in Session, unserialized.


fhk

Recommended Posts

Hello.

 

I have a large number of sites running PHP with a common implementation. Specifically, all my sites

share a common 'auto_prepend_file' file that sets up session and such. As a practice I've used to

avoid problems with Objects stored in sessions is to 'serialize' them first, unserialize them when the session

is restored. This avoids the problem of the session attempting to restore the PHP object before the class that defines it has been included in the session. Adding this practice to the osCommerce project would

help make it easier to integrate with existing infratructures.

 

Specifically, I noticed that the '$navigator' variable is an object stored in the session.

 

Thanks.

 

/Frank

Link to comment
Share on other sites

There were only two objects that were stored in the session, $navigation and $cart.

 

I made these changes in includes/application_top.php:

 

// create the shopping cart & fix the cart if necesary

if (tep_session_is_registered('cart') && is_object($cart)) {

if (! is_object($cart) ) $cart = unserialize($cart); #### This line

if (PHP_VERSION < 4) {

$broken_cart = $cart;

$cart = new shoppingCart;

$cart->unserialize($broken_cart);

}

} else {

tep_session_register('cart');

$cart = new shoppingCart;

}

 

and

// navigation history

if (tep_session_is_registered('navigation')) {

if (! is_object($navigation) ) $navigation = unserialize($navigation); #### This line

if (PHP_VERSION < 4) {

$broken_navigation = $navigation;

$navigation = new navigationHistory;

$navigation->unserialize($broken_navigation);

}

} else {

tep_session_register('navigation');

$navigation = new navigationHistory;

}

$navigation->add_current_page();

 

 

Then in includes/application_bottom.php, I added this:

 

// Serialize any sessionable objects.

if (tep_session_is_registered('navigation') && is_object($navigation)) $navigation = serialize($navigation);

if (tep_session_is_registered('cart') && is_object($cart)) $cart = serialize($cart);

 

 

 

Please consider these changes in the distribution or comment on how to better handle the problem. Thanks.

 

/Frank

Link to comment
Share on other sites

I see your code in my application_top.php ( $Id: application_top.php,v 1.280 2003/07/12 09:38:07 hpdl Exp $)

 

but not the later code in my application_bottom.php ($Id: application_bottom.php,v 1.14 2003/02/10 22:30:41 hpdl Exp $)

 

I gather I should put your bottom (code) at the top of my bottom (.php) on top of tep_session_close() function call?

 

I'm completely new to the sessions concept. Reading tutorials now...

Link to comment
Share on other sites

Update. The code to reserialize the $cart and $navigation are not put into the application_bottom.php

as I first thought. The needed to closer to where the session close was performed, so that redirections

that occur in the code are sure to serialize the objects.

 

The changes I made are to includes/application_top.php and to includes/functions/sessions.php

 

 

in application_top.php:

 

// create the shopping cart & fix the cart if necesary
 if (tep_session_is_registered('cart')) {
   if (! is_object($cart) ) $cart = unserialize($cart); # This is the line I added.
   if (PHP_VERSION < 4) {
     $broken_cart = $cart;
     $cart = new shoppingCart;
     $cart->unserialize($broken_cart);
   }
 } else {
   tep_session_register('cart');
   $cart = new shoppingCart;
 }

 

Also, in application_top.php:

 

// navigation history
 if (tep_session_is_registered('navigation')) {
   if (! is_object($navigation) ) $navigation = unserialize($navigation); # This line I added.
   if (PHP_VERSION < 4) {
     $broken_navigation = $navigation;
     $navigation = new navigationHistory;
     $navigation->unserialize($broken_navigation);
   }
 } else {
   tep_session_register('navigation');
   $navigation = new navigationHistory;
 }
 $navigation->add_current_page();

 

Then in includes/functions/sessions.php, in the method tep_session_close(), these:

  function tep_session_close() {
   global $navigation,$cart; # I added this line.
 
   if (tep_session_is_registered('navigation') && is_object($navigation)) $navigation = serialize($navigation); #  I added this line.
   if (tep_session_is_registered('cart') && is_object($cart)) $cart = serialize($cart); #  I added this line.

   if (PHP_VERSION >= '4.0.4') {
     return session_write_close();
   } elseif (function_exists('session_close')) {
     return session_close();
   }
 }

 

 

/Frank

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...