Skip to main content
  • Home
  • Work
  • Photography
  • Contact
  • Client Login
New Rivers Digital
Home Blogs Eric Weik's blog

Session Data in Drupal: DO NOT USE sess_read() and sess_write()!

In:
  • Drupal
  • Drupal Recipes
11Nov2009

When developing a module in Drupal, reading and writing session data is a fairly common pattern. However, it isn't immediately obvious what the best method is for doing this. If you need to read or write to the session in D6, the preferred method is to use the $_SESSION[] superglobal variable as follows:

<?php
function module_setsessionvar() {
 
// Write to the session
 
$_SESSION['module_sessionvar'] = 'example value';
}

function

module_getsessionvar() {
 
// Read from the session
 
$sessionvar = $_SESSION['module_sessionvar'];
}
?>

Do not use the sess_write() and sess_read() Drupal core functions. Using these to manipulate session data will cause undesired side effects such as user logouts.

  • Eric Weik's blog

Comments

#1 Thanks for this post.

Visitor's picture

Submitted by Mc David (not verified) on Sun, 11/29/2009 - 11:44.

Thanks for this post.

  • reply

#2 Hey, do you have any idea how

Visitor's picture

Submitted by chext0r (not verified) on Mon, 11/30/2009 - 09:26.

Hey, do you have any idea how Drupal's session data is serialized when it's stored in the database? And how does one unserialize it? PHP's serialize functions do not work

See http://drupal.org/node/646702 for more details. Any help is appreciated. Thanks

  • reply

#3 I'm not 100% sure ...

Eric Weik's picture

Submitted by Eric Weik on Mon, 11/30/2009 - 15:33.

I'm not 100% sure on this one, but I believe that PHP actually handles the serialization, not Drupal.

Drupal registers its own session handlers (sess_read(), sess_write(), etc.) with a call to the PHP internal function session_set_save_handler() in includes/bootstrap.inc (see case DRUPAL_BOOTSTRAP_SESSION in _drupal_bootstrap().

Looking at the source to Drupal's session handler sess_write(), it looks to me like assumes the value passed to it is already serialized. It stores the key and value directly to the {session} table with an UPDATE query.

  • reply

#4 Thank you

Visitor's picture

Submitted by Robin van Emden (not verified) on Mon, 12/07/2009 - 04:15.

Very useful!

  • reply

#5 You are welcome!

Eric Weik's picture

Submitted by Eric Weik on Mon, 12/07/2009 - 21:46.

You are very welcome Robin. When I was first learning Drupal module development, this particular issue caused me some confusion (e.g. using sess_write() resulted in users randomly getting logged out), so I hope to save others some time and headache!

  • reply

#6 Thanks for this info! I lost

Visitor's picture

Submitted by Venkat (not verified) on Sun, 06/20/2010 - 12:32.

Thanks for this info! I lost many hours confused as to why users were getting logged out.

  • reply

Post new comment

Warning
I strongly encourage and welcome links and feedback. However, this site is moderated and comments with inappropriate links are rejected. Please do not post a one-line "Me too" or "Great post!" comment just so you can link to your site. Thank you for your understanding.
The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Use to create page breaks.

More information about formatting options

Blog Posts (RSS)

About

Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.

Contact Details

New Rivers Digital
PO Box 784

Lancaster, VA 22503

Voice+1-804-577-8526
Fax +1-804-462-3229
Contact Form

Content Tags

Abstract B&W Bookmarklet Celestial Clouds CSA Date Drupal Drupal Recipes Drupal Sites Dynamic Range Estuary Government Grasses Long Exposure Macro New Rivers Digital Photoblog Photoset RGB Sketchbook Snow Storm Sumi-e Sunset Surf Theming Trigger.module Views.module Webform.module Zen Zen Theming
more tags

Recent comments

  • 3.x Beta
    7 weeks 6 days ago
  • Some feedback from Webform 3.x
    8 weeks 18 hours ago
  • rainbow art
    8 weeks 20 hours ago
  • Sumi
    10 weeks 2 days ago
  • Thanks for this info! I lost
    11 weeks 1 day ago
  • Look into "Comment Notify"
    18 weeks 2 days ago

Popular content

Today's:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Sending Multiple Customized Confirmation Messages with Webform.Module
  • Session Data in Drupal: DO NOT USE sess_read() and sess_write()!

All time:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Implementing Flickr Slideshow Links By Theming Flickr.module
  • Sending Multiple Customized Confirmation Messages with Webform.Module

Activity Stream

  • Mon, 09/06/2010 - 21:55

  • Flickr Eric posted DSC04355-foxtails 9:55pm #
  • Flickr Eric posted #04361 - Foxtails 9:54pm #
  • Flickr Eric posted DSC04359-foxtails 9:54pm #
  • Thu, 09/02/2010 - 21:05

  • Del.icio.us Eric linked to The Magic Onions 9:05pm#
  • Sat, 08/28/2010 - 23:00

  • Del.icio.us Eric linked to Fanboy.com 11:00pm#
  • Sat, 08/28/2010 - 22:58

  • Del.icio.us Eric linked to Japanese Emoticons: A Stamp of Success ยป Fanboy.com 10:58pm#
  • Del.icio.us Eric linked to http://hypermammut.sourceforge.net/paulstretch/ 10:56pm#
  • Thu, 08/26/2010 - 08:05

  • Del.icio.us Eric linked to Digg - Top in 24 Hours 8:05am#
  • Del.icio.us Eric linked to Kuriositas 11:52pm#
  • Tue, 08/24/2010 - 09:18

  • Del.icio.us Eric linked to Slider | drupal.org 9:18am#
more from my activity-stream


I am a member of the Drupal Association.
Eric At NRD on Drupal.org
Circumjacence (Eric Weik) on Twitter
Circumjacence on Delicious
Eric Weik on Linkedin
Circumjacence (Eric Weik) on Flickr
Circumjacence (Eric Weik) at StumbleUpon

Powered by Drupal & Genesis | Valid XHTML 1.0 Strict | Syndicate content RSS Feed

© 2010 New Rivers Digital | PO Box 784 | Lancaster, Virginia 22503 | +1-804-577-8526 | Contact Form