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.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 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
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • 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:         return true;
 84:     }
 85: 
 86: /**
 87:  * Method used to read from a database session.
 88:  *
 89:  * @param integer|string $id The key of the value to read
 90:  * @return mixed The value of the key or false if it does not exist
 91:  */
 92:     public function read($id) {
 93:         $row = $this->_model->find('first', array(
 94:             'conditions' => array($this->_model->primaryKey => $id)
 95:         ));
 96: 
 97:         if (empty($row[$this->_model->alias]['data'])) {
 98:             return false;
 99:         }
100: 
101:         return $row[$this->_model->alias]['data'];
102:     }
103: 
104: /**
105:  * Helper function called on write for database sessions.
106:  *
107:  * @param integer $id ID that uniquely identifies session in database
108:  * @param mixed $data The value of the data to be saved.
109:  * @return boolean True for successful write, false otherwise.
110:  */
111:     public function write($id, $data) {
112:         if (!$id) {
113:             return false;
114:         }
115:         $expires = time() + $this->_timeout;
116:         $record = compact('id', 'data', 'expires');
117:         $record[$this->_model->primaryKey] = $id;
118:         return $this->_model->save($record);
119:     }
120: 
121: /**
122:  * Method called on the destruction of a database session.
123:  *
124:  * @param integer $id ID that uniquely identifies session in database
125:  * @return boolean True for successful delete, false otherwise.
126:  */
127:     public function destroy($id) {
128:         return $this->_model->delete($id);
129:     }
130: 
131: /**
132:  * Helper function called on gc for database sessions.
133:  *
134:  * @param integer $expires Timestamp (defaults to current time)
135:  * @return boolean Success
136:  */
137:     public function gc($expires = null) {
138:         if (!$expires) {
139:             $expires = time();
140:         } else {
141:             $expires = time() - $expires;
142:         }
143:         return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
144:     }
145: 
146: }
147: 
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