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

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

In:
  • Drupal 6
  • 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.

Mc David'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

chext0r'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

Robin van Emden'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

Venkat'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

#7 So how do I do...

Naruki's picture

Submitted by Naruki (not verified) on Tue, 12/28/2010 - 13:24.

I am new to Drupal, and I think I want to hook into the sess_read() function. I don't want to add to the session object, I just want to check some custom cookies and load a specific user.

I am using an external authentication source, and it works fine when the user specifically logs in. But I am also required to log him in automatically if the right cookies are set and can be authenticated using my external source.

It seems that the sess_read() function is the best place to check for this, since everything else will flow naturally after that. Plus, for legal reasons I can't store identifying information (username/email/etc) in Drupal -- so loading them temporarily at sess_read() kind of makes sense.

Am I going about this entirely the wrong way?

  • 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 associated with the e-mail address you provide, it will be 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 Celestial Clouds Drupal 6 Drupal Recipes Drupal Sites Estuary Etsy Government Grasses HDR Long Exposure Macro New Rivers Digital Orton Photoblog Photo Expedition Photoset Renderblog RGB Sketchbook Snow Storm Structure Synth Sunflow Sunset Theming Webform.module Wide Angle Zen Zen Theming
more tags

Recent comments

  • This is stunning
    2 weeks 5 days ago
  • #42 worked well for me
    3 weeks 3 days ago
  • Email by country
    12 weeks 3 days ago
  • hid conversion kit
    13 weeks 3 days ago
  • James
    14 weeks 2 days ago
  • Michelle
    14 weeks 3 days ago

Popular content

Today's:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Implementing Flickr Slideshow Links By Theming Flickr.module
  • Non-Unique Conditional Email Addresses with Webform.Module

All time:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Sending Multiple Customized Confirmation Messages with Webform.Module
  • Non-Unique Conditional Email Addresses with Webform.Module

Activity Stream

  • Sun, 01/29/2012 - 22:31

  • Flickr Eric posted #0228 - Sunset Tree 10:31pm #
  • Flickr Eric posted #0225 - Rappahannock 10:30pm #
  • Flickr Eric posted #5617 - Hobie Sailing 11:56am #
  • Mon, 01/16/2012 - 10:43

  • Flickr Eric posted #5687 - Broken Tree (Digital Sketch) 10:43am #
  • Fri, 01/06/2012 - 07:54

  • Flickr Eric posted #8975 - Greenvale Creek 7:54am #
  • Flickr Eric posted #8824 - Grass (blue ch) 7:46am #
  • Flickr Eric posted #5687 - Broken Tree (IR) 7:35am #
  • Flickr Eric posted #3205 - Toadstool 7:21am #
  • Flickr Eric posted #2318 - Tree and Beach 7:13am #
  • Flickr Eric posted #2251 - Low Tide Arrival 7:04am #
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