SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Norrad's Avatar
    Join Date
    Nov 2003
    Location
    Thailand
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Storing array data in a session?

    Hi All,

    I'm reworking a simple shopping cart I'm working on so that cart items are stored in a session till the user checks out.

    The array looks something like this:
    PHP Code:
    $cart = array(
       
    item1 => 5,
       
    item2 => 2,
       
    item3 => 1,
       
    item4 => 17
    ); 
    The array stores the item id and the quantity.

    Now, while I have no problem storing this data in a database, is there a way to store it in a session instead?

    That way if the user leaves the site and the session is destroyed there will be no un-processed carts cluttering up my database.

  2. #2
    get into it! bigduke's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll assume that you're familiar with the basic session handling functions.

    PHP Code:
    session_start();
    if(!
    session_is_registered("cart"))
       
    session_register("cart");
    $cart = array( 
       
    'item1' => 5
       
    'item2' => 2
       
    'item3' => 1
       
    'item4' => 17 
    ); 
    You now have the array stored as a session variable

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I prefer:
    PHP Code:
    session_start();
    $_SESSION['cart'] = $cart

    a few quotes from the session_register() function page:

    Quote Originally Posted by PHP Manual
    Caution

    If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.

    register_globals: important note: Since PHP 4.2.0, the default value for the PHP directive register_globals is off. The PHP community encourages all to not rely on this directive but instead use other means, such as the superglobals.

    Caution

    This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below.

    Caution

    If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
    It does not seem to be a good practice to recommend using a function that is disabled in the default/recommended configuration from php.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Zealot IznSe7en's Avatar
    Join Date
    Jun 2004
    Location
    Around
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    I prefer:
    PHP Code:
    session_start();
    $_SESSION['cart'] = $cart

    a few quotes from the session_register() function page:



    It does not seem to be a good practice to recommend using a function that is disabled in the default/recommended configuration from php.
    Plus its less bloody confusing.
    ><

  5. #5
    get into it! bigduke's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang! I still work with register_globals on


Bookmarks

Posting Permissions

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