I'm developing a simple e-commerce module for Drupal. I've been thinking about using the standard e-commerce project, but it is way too elaborate for my needs. Besides, doing it myself is a nice way to get to know the inner workings of Drupal.

My problem: when a user adds a product to the cart, I obviously want to store this somewhere. The Drupal e-commerce project uses a database table (ec_cart) for this, but looking at the sessions table, I see the following serialized data in the session-column:

form|a:6:{s:32:"6dc59b09841cbf352fcb5669e74d3e88";a:2:{s:9:"timestamp";i:1224167714;s:4:"args";a:1:{i:0;s:14:
"system_modules";}}s:32:"176eb31a60a35c8d9a87f333de3c1775";a:2:{s:9:"timestamp";i:1224168124;s:4:"args";a:1
:{i:0;s:14:"system_modules";}}s:32:"2ae6390e331141ad4ca1aa2ef86b6977";a:2:{s:9:"timestamp";i:1224168181;s:4
:"args";a:1:{i:0;s:14:"system_modules";}}s:32:"ee2b99e2873fa5d91c2b32430b8129c5";a:2:{s:9:"timestamp";i:1224
168271;s:4:"args";a:1:{i:0;s:14:"system_modules";}}s:32:"825fef6cafc93f08b85b6953e6ed9c77";a:2:{s:9:"timestam
p";i:1224168831;s:4:"args";a:3:{i:0;s:14:"tax_admin_form";i:1;i:0;i:2;a:5:{s:5:"realm";s:7:"country";s:2:"op";s:4:"N
ext";s:13:"form_build_id";s:32:"cec0efe486aa3ea46facda1753c4d843";s:10:"form_token";s:32:"5c9909272a072ad443
cdf38841fa4431";s:7:"form_id";s:14:"tax_admin_form";}}}s:32:"ca63e4cd715e895bef761fff1b2e308e";a:2:{s:9:"time
stamp";i:1224168900;s:4:"args";a:2:{i:0;s:30:"store_transactions_search_form";i:1;a:2:{s:6:"status";s:3:"1,7";s:8:"
workflow";s:7:"1,2,3,4";}}}}node_overview_filter|a:0:{}

because I only have to store some product-ID's, I thought it would make sense to store them in sessions.session, instead of creating a seperate database table for this.

Questions:

  • is this a good idea and if not, why not?
  • regardless of whether this is a good idea, where is all this data being added to sessions.session?
  • what function(s) is/are used for this. I expected them to be in session.inc, but didn't find anything

Who can give me some insight into this? Many thanks in advance!

Comments

pbarnett’s picture

... as in $_SESSION['your_variable'] = whatever, but bear in mind that this will persist through the rest of the user's session if you don't explicitly unset it after you've used it.

Pete.

Fluffy Convict’s picture

...but then what's the difference with the session data in the sessions table? Messages are set in $_SESSION by use of set_message(), but apparently other session-specific data is not, but rather stored in the database. Why these two approaches? And what is all the data in sessions.session being used for?

yuriy.babenko’s picture

Data stored in the database will not go anywhere until you explicitly delete it. Data stored via $_SESSION is only stored while the session is active (ie. not destroyed).
---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

Fluffy Convict’s picture

...the unique identifier in the sessions table is a 32-character PHP session-id, so when the client browser is closed, the key in the database refers to nothing, so the data stored there is useless anyway. IMHO it would only make sense to keep session-information stored in a database when the unique identifier refers to an id on a persistent cookie on the client. This way, you can read the cookie when a client comes back to your site and fill ie the cart with information stored in the database. I think this is the way Amazon works. When the client comes back, it has a different session-id, but the unique id in the persistent cookie will still be there.

I'm probably missing something here, because Drupal is being developed by much smarter developers than me, so there has to be a reason why:

  • Drupal stores session data in a database with only a session key as an identifier (which expires when the user closes it's browser)
  • The E-Commence module uses a seperate database table to store cart-data in (as opposed to putting it in sessions.session, as I said in the opening post)

Please enlighten me :-)

alexanderpas’s picture

first of all... the sesion table get's garbage collected by cron. (AFAIK)

why use a seperate table? because that way, logged in users can keep their cart between session.

less == more

Fluffy Convict’s picture

...for what purpose?

pbarnett’s picture

If all you need is data that's persistent only for a single session, then $_SESSION is the way to go; I've used it in some PayPal IPN code that I wrote since the Simple Paypal module seemed a bit iffy, the e-commerce module was overkill for what I was trying to do and, dammit, I wanted to understand and implement the PayPal NVP API myself. I learned a lot about cURL in the process, too!

Pete.

Fluffy Convict’s picture

If I login to drupal.org, close my browser, shutdown my PC and browse to it again tomorrow, I don't have to login again. How it that possible if the session is killed upon closing the browser?

pbarnett’s picture

And the session id is stored in a session cookie on yout machine.

Pete.

OpenChimp’s picture

Something like this:

if ($_GET['aff_id']) :
  $_SESSION['aff_id'] = $_GET['aff_id'];
endif;