Results 1 to 4 of 4

Thread: Object stored in session not retained

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Object stored in session not retained

    I'm storing a shopping cart object in a session, it works fine when navigating pages in the application, the object is being retained intact, however if I close the browser and go back to a page it doesn't retain. Either the items member of the object is being cleared, or the session is being destroyed. I've used sessions before storing simple strings for a login system, it allowed me to close the browser and return to the page still logged in, thats what I'm trying to achieve.

    Here's how I'm storing the object in the session:
    Code:
    <?
    
    	require_once("clsCart.php");
    
    	session_start();
    	
    	if(!isset($_SESSION['cart']))
    	{
    		//cart doesn't exist
    		$_SESSION['cart'] = new cart;
    	}
    Edit: Sometimes it is working, when I close and open it back up but sometimes not. I'm not leaving any length of time between closing and opening.

    Cheers all
    Last edited by the182guy; Jun 24th, 2007 at 04:36 AM.
    Chris

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Object stored in session not retained

    The default timeout for a session cookie is zero, which means it expires when the browser is closed. If you wish it to persist past this point you need to either alter the expiration time manually, or set your own auto-login cookie. The second option is preferable, but requires that you, at some point, commit data to a database rather than relying on the session being available.

    Most sophisticated shopping cart/order tracking systems store carts and orders in a database so that the user can log in and retrieve them at a later date. This is what I suggest you do.

  3. #3

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Object stored in session not retained

    Cheers. I'll just use the timeout of 20mins or so. Yeah I was going to do that in the first place, store it in a db against the users login, but that means people will have to sign up and arse around validating emails just to buy a few items or shop around. Using sessions allows people to visit the site, add items to a cart and checkout.
    Chris

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Object stored in session not retained

    You can use ini_set to set the session.cookie_lifetime directive within your scripts. But i wouldn't really recommend this. A session, as the name implies is something temporary.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width