Drupal sessions:
How Sessions works on drupal:
The page request send by the browser includes a 32-character ID known as PHPSESSID under set- cookie tag, which is used as the key to the session information that drupal stores about session.
-
The settings for session management are done in three files i.e. .htaccess, settings.php nad bootstarp.inc
-
By default drupal uses the cookie based session management instead of URL base session management. If the browser doesn’t accept cookies , a session cannot be established
-
-
The PHP directive ’sessions.use_only_cookies’ is set to 1 in settings.php to accept cookie based sessions.
-
The PHP directive ’sessions.use_trans_sid’ is set to 0 in settings.php to reject URL based sessions.
-
-
-
At the installation time Drupal turns off the PHP’s ’session.auto_start’ functionality in .htaccess file to take full control over sessions.
Why Drupal need sessions?
-
To handle the viewing preferences of the site content according to viewer status.
-
To remember the viewing preferences: Drupal stores sessions in ’sessions’ table, the session table is used to associate the sessions Ids with the user ids to retrieve the viewing preferences of a user. It applies a join query on session and user tables using session ids and user ids to know the user role. Session data of a user is viewable by $user->session of $user object.
-
-
To represent a session for anonymous user, the ’session’ table ‘uid’ column is set to 0.
-
To represent a session for login user, the ’session’ table ‘uid’ column is set to user Id with a new session Id such that the session does’nt get hijacked. If a user logs out, the row for that session is removed from the database immediately.
-
-
-
To remember the comments preferences: In comment.module session is used to store viewing preferences for users, i.e.
-
-
$_SESSIONS['comment_mode']=$mode
-
$_SESSIONS['comment_sort']=$order
-
$_SESSIONS['comment_comments_per_page']=$comments_per_page
-
-
-
To handle file uploads
Session Life time
-
Sessions lifetime related settings are located in settings.php, By default
-
-
ini_set(’session.cache_expire’, 200000); // 138.9 days
-
ini_set(’session.cookie_lifetime’, 2000000); // 23.1 days
-
ini_set(’session.gc_maxlifetime’, 200000); // 55 hours
-
-
Other Information
-
Writing session in the session table is the last task of drupal while serving pages.
-
Drupal does not store session information , the first time a user visits a site. This is to reduce the load on the sessions table generated by crawlers.
-
Drupal $user object is first built during the DRUPAL_BOOTSTRAP_SESSION phase of bootstrapping by sess_read().
-
While deploying drupal on multiple subdomains, you can create a unique session name for each site by adding ini_set(’session.name’, ‘my-site-unique-name_PHPSESSID’); in settings.php
-
If you need to store short term data for anonymous users, use the $_SESSION superglobal e.g.
-
-
$_SESSION['anonymous_data']=$anonymous_data;
-
-
-
If you want to store a permanent data to a user, save it in the $user object e.g.
-
-
$user->login_name=$login_name; user_save();
-
-
csmpqgbe7r
