I'm storing stuff in a session that is sometimes handled by drupal. The session id is passed forward to another service, which checks back to the original server to get the session data.

When the session id is generated by drupal, i want to recognize it so i can include the drupal bootstrap instead of creating a normal php session. What would be a proper way to recognize drupal session ids ?

Is there a way to make it more specific, like change the sessionid myself ?

  • 1
    "which is invalid in php" - false. Drupal is written in PHP so if it was invalid, Drupal would not be able to do it.
    – Mołot
    Jan 7, 2014 at 8:03
  • Yeah, the session ID is a string. - is not an invalid character for a string
    – Clive
    Jan 7, 2014 at 8:50
  • I'd dig into includes/session.inc and see exactly how Drupal internally recognizes the id. I'd do it myself and post an answer, but don't have time ATM.
    – Letharion
    Jan 7, 2014 at 9:58
  • sorry guys - the _ is an invalid character in a php sessionid, as used by the default php session handling. Its not just a string. drupal overwrites the default handling. use the drupal id to create a default session and php will barf `Warning: session_start() [function.session-start]: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' ... anyway, thats not really answering anything, is it.
    – commonpike
    Jan 7, 2014 at 10:36
  • The session ID is generated using drupal_random_key(), in drupal_session_initialize() and other functions. Drupal does no special processing of the session ID, and definitely nothing to do with manipulating underscores in the string. As @Letharion said you can see all of that in includes/session.inc. The file's only a few hundred lines long, very easy to understand
    – Clive
    Jan 7, 2014 at 10:55

2 Answers 2


Drupal will let you override the default session handler using the session_inc variable. This is what Drupal actually uses to bootstrap the session:

require_once DRUPAL_ROOT . '/' . variable_get('session_inc', 'includes/session.inc');

So to replace that with your own, set the variable to your own path:

variable_set('session_inc', 'sites/all/modules/MYMODULE/session.inc');

And re-implement the functions from the core session.inc in your custom file, making changes where necessary.


You can get seesion id by calling session_id() function. For session name you can use session_name().

  • Yes - these are php functions (the question was how I can recognize if they were generated by drupal .. it has slightly different ids)
    – commonpike
    Mar 12, 2018 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.