session_api.module in Session API 5
Same filename and directory in other branches
The Session API module provides a quick interface for storing information in the session.
File
session_api.moduleView source
<?php
/**
* @file
* The Session API module provides a quick interface for storing
* information in the session.
*/
/**
* Determine if cookies are enabled.
*/
function session_api_available() {
return !empty($_COOKIE);
}
/**
* Return a Session API ID corresponding to the current session. The Session
* API ID is the mapping of the private php session_id [1] to the ID used by
* modules leveraging the Session API.
*
* 1. http://php.net/manual/en/function.session-id.php
*/
function session_api_get_sid() {
if (!session_api_available()) {
return FALSE;
}
if (!is_numeric($_SESSION['session_api_id'])) {
// first, check if id exists in db
$sid = db_result(db_query("SELECT sid FROM {session_api} WHERE session_id = '%s'", session_id()));
if (!$sid) {
// no sid exists, create new one
$sid = db_next_id('{session_api}_sid');
db_query("INSERT INTO {session_api} (sid, session_id) VALUES (%d, '%s')", $sid, session_id());
}
$_SESSION['session_api_id'] = $sid;
}
return $_SESSION['session_api_id'];
}
/**
* Implementation of hook_menu().
*/
function session_api_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/session-api',
'title' => t('Session API Configuration'),
'description' => t('Configure Session API behavior'),
'access' => user_access('administer site configuration'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'session_api_settings_form',
),
);
}
return $items;
}
/**
* Implementation of hook_cron().
*/
function session_api_cron() {
// should any cleanup be run
if (!variable_get('session_api_run_cron_session_api', FALSE)) {
return;
}
// find modules that have this hook
$modules = module_implements('session_api_cleanup');
// run Session API hook first
array_unshift($modules, 'session_api');
$modules = array_unique($modules);
foreach ($modules as $module) {
if (variable_get('session_api_run_cron_' . $module, FALSE)) {
module_invoke($module, 'session_api_cleanup');
}
}
}
/**
* Implementation of hook_session_api_cleanup().
*
* A self implementation of the Session API cleanup hook. Generally called
* from session_api_cron().
*
* @param string $op
* The operation the function perform.
* * run - perform the cleanup
* * info - provide information(in the form of a keyed array) of the title
* and description of the cleanup.
* @return
* Varies with $op.
* * run - NULL
* * info - a keyed array containing title and description
*/
function session_api_session_api_cleanup($op = 'run') {
switch ($op) {
case 'info':
return array(
'title' => t('Clear expired <strong>Session API</strong> sessions'),
'description' => t('Clear out old <strong>Session API</strong> IDs for which the corresponding <a href="!php">PHP session</a> no longer exists. <br /><strong>Important</strong>: This option must be checked for any of the subsequent cleanup processes to work. Unchecking this will disable any removal of expired session data.', array(
'!php' => url('http://php.net/manual/en/book.session.php'),
)),
);
case 'run':
default:
// fetch list of outdated sids
$result = db_query("SELECT sap.sid FROM {session_api} sap LEFT JOIN {sessions} s ON (sap.session_id = s.sid) WHERE s.sid IS NULL");
while ($session = db_fetch_object($result)) {
// remove the session api id
db_query("DELETE FROM {session_api} WHERE sid = %d", $session->sid);
}
}
}
/**
* Implementation of hook_simpletest().
*/
function session_api_simpletest() {
$module_name = 'session_api';
$dir = drupal_get_path('module', $module_name) . '/tests';
$tests = file_scan_directory($dir, '\\.test$');
return array_keys($tests);
}
Functions
Name | Description |
---|---|
session_api_available | Determine if cookies are enabled. |
session_api_cron | Implementation of hook_cron(). |
session_api_get_sid | Return a Session API ID corresponding to the current session. The Session API ID is the mapping of the private php session_id [1] to the ID used by modules leveraging the Session API. |
session_api_menu | Implementation of hook_menu(). |
session_api_session_api_cleanup | Implementation of hook_session_api_cleanup(). |
session_api_simpletest | Implementation of hook_simpletest(). |