CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CacheSession
  • DatabaseSession

Interfaces

  • CakeSessionHandlerInterface
  1: <?php
  2: /**
  3:  * Database Session save handler.  Allows saving session information into a model.
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       Cake.Model.Datasource.Session
 16:  * @since         CakePHP(tm) v 2.0
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
 21: App::uses('ClassRegistry', 'Utility');
 22: 
 23: /**
 24:  * DatabaseSession provides methods to be used with CakeSession.
 25:  *
 26:  * @package       Cake.Model.Datasource.Session
 27:  */
 28: class DatabaseSession implements CakeSessionHandlerInterface {
 29: 
 30: /**
 31:  * Reference to the model handling the session data
 32:  *
 33:  * @var Model
 34:  */
 35:     protected $_model;
 36: 
 37: /**
 38:  * Number of seconds to mark the session as expired
 39:  *
 40:  * @var int
 41:  */
 42:     protected $_timeout;
 43: 
 44: /**
 45:  * Constructor.  Looks at Session configuration information and
 46:  * sets up the session model.
 47:  *
 48:  */
 49:     public function __construct() {
 50:         $modelName = Configure::read('Session.handler.model');
 51: 
 52:         if (empty($modelName)) {
 53:             $settings = array(
 54:                 'class' => 'Session',
 55:                 'alias' => 'Session',
 56:                 'table' => 'cake_sessions',
 57:             );
 58:         } else {
 59:             $settings = array(
 60:                 'class' => $modelName,
 61:                 'alias' => 'Session',
 62:             );
 63:         }
 64:         $this->_model = ClassRegistry::init($settings);
 65:         $this->_timeout = Configure::read('Session.timeout') * 60;
 66:     }
 67: 
 68: /**
 69:  * Method called on open of a database session.
 70:  *
 71:  * @return boolean Success
 72:  */
 73:     public function open() {
 74:         return true;
 75:     }
 76: 
 77: /**
 78:  * Method called on close of a database session.
 79:  *
 80:  * @return boolean Success
 81:  */
 82:     public function close() {
 83:         $probability = mt_rand(1, 150);
 84:         if ($probability <= 3) {
 85:             $this->gc();
 86:         }
 87:         return true;
 88:     }
 89: 
 90: /**
 91:  * Method used to read from a database session.
 92:  *
 93:  * @param mixed $id The key of the value to read
 94:  * @return mixed The value of the key or false if it does not exist
 95:  */
 96:     public function read($id) {
 97:         $row = $this->_model->find('first', array(
 98:             'conditions' => array($this->_model->primaryKey => $id)
 99:         ));
100: 
101:         if (empty($row[$this->_model->alias]['data'])) {
102:             return false;
103:         }
104: 
105:         return $row[$this->_model->alias]['data'];
106:     }
107: 
108: /**
109:  * Helper function called on write for database sessions.
110:  *
111:  * @param integer $id ID that uniquely identifies session in database
112:  * @param mixed $data The value of the data to be saved.
113:  * @return boolean True for successful write, false otherwise.
114:  */
115:     public function write($id, $data) {
116:         if (!$id) {
117:             return false;
118:         }
119:         $expires = time() + $this->_timeout;
120:         $record = compact('id', 'data', 'expires');
121:         $record[$this->_model->primaryKey] = $id;
122:         return $this->_model->save($record);
123:     }
124: 
125: /**
126:  * Method called on the destruction of a database session.
127:  *
128:  * @param integer $id ID that uniquely identifies session in database
129:  * @return boolean True for successful delete, false otherwise.
130:  */
131:     public function destroy($id) {
132:         return $this->_model->delete($id);
133:     }
134: 
135: /**
136:  * Helper function called on gc for database sessions.
137:  *
138:  * @param integer $expires Timestamp (defaults to current time)
139:  * @return boolean Success
140:  */
141:     public function gc($expires = null) {
142:         if (!$expires) {
143:             $expires = time();
144:         }
145:         return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
146:     }
147: 
148: /**
149:  * Closes the session before the objects handling it become unavailable
150:  *
151:  * @return void
152:  */
153:     public function __destruct() {
154:         try {
155:             session_write_close();
156:         } catch (Exception $e) {
157:         }
158:     }
159: 
160: }
161: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs