0

I've had a look at some posts with similar titles checking status of session with session id, and How to tell if a session is active?, but they don't seem to be specific to what I am trying to achieve.

I am building a cart in which users are able to add to it regardless of whether they are logged in or not - by using $_SESSION and session_id().

Adding items to the cart add entries to the shopping cart table with the session id.

Long story short, how do I figure out from a list of session IDs, which sessions are active and which are not (for use in a crontab) so I can remove redundant items from the shopping cart table in the database?

Community
  • 1
  • 1
PavKR
  • 1,591
  • 2
  • 11
  • 26
  • u want both active and inactives in the session? – Sougata Bose Sep 15 '14 at 06:17
  • Well technically I only need to know which ones are inactive so I can remove them from the DB. – PavKR Sep 15 '14 at 06:20
  • 1
    I'm not sure if you can get a list of active/expired sessions in PHP. But this is what you can do: Once a cart is initialized, store the session id in a table (I guess you are already doing that) **with the timestamp** (I guess you are not doing _that_ yet). And whenever user loads a page update the timestamp (If the user spends lot of time in a page you can send ajax calls to refresh the session periodically). Define a threshold for old sessions (e.g. 2 hours), and then delete sessions older than this threshold (probably using a cron job) from database. Let me know if that works. – sampathsris Sep 15 '14 at 06:22
  • why not save the state for all the items? – Sougata Bose Sep 15 '14 at 06:27
  • Alternatively, maybe **you don't _have_ to store cart items in the database**. Store the cart items in session variables. And the problem is solved. When the session dies garbage collector will automatically clean up the memory. And it saves a trip to database as well. – sampathsris Sep 15 '14 at 06:29
  • @Krumia, Thanks, seems like that's the way to do it - I just thought that since PHP cleans up sessions, there may have been a way to detect if a session exists or not with a session id. The only reason I don't want to use just sessions is because I want a member to be able to log back in to their account and still have the items they selected in their shopping cart. – PavKR Sep 15 '14 at 06:31
  • you should add your items with cookies. What happen if you close the browser? If you dont store card items in the database you'll lost last changes. – hakki Sep 15 '14 at 06:46
  • @hakiko, Yes, for non-members, the cart will be based on sessions (with a time-based expiry), for members, there will be no cart-expiry as the data will be in the database attached to their user id. – PavKR Sep 15 '14 at 06:50
  • another nice approach is local storage. If you use html5... Maybe you want to see it http://diveintohtml5.info/storage.html – hakki Sep 15 '14 at 06:56
  • Yeah, I thought about local storage but I'd rather go with db & sessions due to support for older browsers. – PavKR Sep 15 '14 at 06:59

1 Answers1

1

You can do this by using a SessionHandler, if you have sufficient privileges. (Works in PHP > 5.4.0)

class MySessionHandler extends SessionHandler
{    
    public function __construct()
    {
        // database initialization code
    }

    public function destroy($id)
    {
        $ret = parent::destroy($id);
        // database uninitialization code
        return $ret;
    }
}

Then in your code:

ini_set('session.save_handler', 'files');
$handler = new MySessionHandler();
session_set_save_handler($handler, true);
session_start();

Before using this, I advice you to thoroughly read the documentation I linked above.


However, since you are building a shopping cart, it would be easier to just store the cart information in session variables.

Community
  • 1
  • 1
sampathsris
  • 21,564
  • 12
  • 71
  • 98
  • Hmm, interesting - I do have full server access so I'll give this a go. The only reason I don't want to use only sessions is because I want a member to be able to log back in to their account and still have the items they selected in their shopping cart. – PavKR Sep 15 '14 at 06:47
  • @thePav: If you store it in DB or in session variables, either way you will have to make your sessions not expire for a long time. – sampathsris Sep 15 '14 at 06:48
  • For non-members, the cart will be based on sessions (with a time-based expiry of about 2 hours of non-activity), for members, there will be no cart-expiry as the data will be in the database attached to their user id (so logged in members won't need a cart session). – PavKR Sep 15 '14 at 06:57