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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.10
      • 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
  • None

Classes

  • CakeSession
  • DataSource
  • DboSource
  1: <?php
  2: /**
  3:  * Session class for CakePHP.
  4:  *
  5:  * CakePHP abstracts the handling of sessions.
  6:  * There are several convenient methods to access session information.
  7:  * This class is the implementation of those methods.
  8:  * They are mostly used by the Session Component.
  9:  *
 10:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 11:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 12:  *
 13:  * Licensed under The MIT License
 14:  * For full copyright and license information, please see the LICENSE.txt
 15:  * Redistributions of files must retain the above copyright notice.
 16:  *
 17:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 18:  * @link          https://cakephp.org CakePHP(tm) Project
 19:  * @package       Cake.Model.Datasource
 20:  * @since         CakePHP(tm) v .0.10.0.1222
 21:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 22:  */
 23: 
 24: App::uses('Hash', 'Utility');
 25: App::uses('Security', 'Utility');
 26: 
 27: /**
 28:  * Session class for CakePHP.
 29:  *
 30:  * CakePHP abstracts the handling of sessions. There are several convenient methods to access session information.
 31:  * This class is the implementation of those methods. They are mostly used by the Session Component.
 32:  *
 33:  * @package       Cake.Model.Datasource
 34:  */
 35: class CakeSession {
 36: 
 37: /**
 38:  * True if the Session is still valid
 39:  *
 40:  * @var bool
 41:  */
 42:     public static $valid = false;
 43: 
 44: /**
 45:  * Error messages for this session
 46:  *
 47:  * @var array
 48:  */
 49:     public static $error = false;
 50: 
 51: /**
 52:  * User agent string
 53:  *
 54:  * @var string
 55:  */
 56:     protected static $_userAgent = '';
 57: 
 58: /**
 59:  * Path to where the session is active.
 60:  *
 61:  * @var string
 62:  */
 63:     public static $path = '/';
 64: 
 65: /**
 66:  * Error number of last occurred error
 67:  *
 68:  * @var int
 69:  */
 70:     public static $lastError = null;
 71: 
 72: /**
 73:  * Start time for this session.
 74:  *
 75:  * @var int
 76:  */
 77:     public static $time = false;
 78: 
 79: /**
 80:  * Cookie lifetime
 81:  *
 82:  * @var int
 83:  */
 84:     public static $cookieLifeTime;
 85: 
 86: /**
 87:  * Time when this session becomes invalid.
 88:  *
 89:  * @var int
 90:  */
 91:     public static $sessionTime = false;
 92: 
 93: /**
 94:  * Current Session id
 95:  *
 96:  * @var string
 97:  */
 98:     public static $id = null;
 99: 
100: /**
101:  * Hostname
102:  *
103:  * @var string
104:  */
105:     public static $host = null;
106: 
107: /**
108:  * Session timeout multiplier factor
109:  *
110:  * @var int
111:  */
112:     public static $timeout = null;
113: 
114: /**
115:  * Number of requests that can occur during a session time without the session being renewed.
116:  * This feature is only used when config value `Session.autoRegenerate` is set to true.
117:  *
118:  * @var int
119:  * @see CakeSession::_checkValid()
120:  */
121:     public static $requestCountdown = 10;
122: 
123: /**
124:  * Whether or not the init function in this class was already called
125:  *
126:  * @var bool
127:  */
128:     protected static $_initialized = false;
129: 
130: /**
131:  * Session cookie name
132:  *
133:  * @var string
134:  */
135:     protected static $_cookieName = null;
136: 
137: /**
138:  * Whether or not to make `_validAgentAndTime` 3.x compatible.
139:  *
140:  * @var bool
141:  */
142:     protected static $_useForwardsCompatibleTimeout = false;
143: 
144: /**
145:  * Whether this session is running under a CLI environment
146:  *
147:  * @var bool
148:  */
149:     protected static $_isCLI = false;
150: 
151: /**
152:  * Pseudo constructor.
153:  *
154:  * @param string|null $base The base path for the Session
155:  * @return void
156:  */
157:     public static function init($base = null) {
158:         static::$time = time();
159: 
160:         if (env('HTTP_USER_AGENT') && !static::$_userAgent) {
161:             static::$_userAgent = md5(env('HTTP_USER_AGENT') . Configure::read('Security.salt'));
162:         }
163: 
164:         static::_setPath($base);
165:         static::_setHost(env('HTTP_HOST'));
166: 
167:         if (!static::$_initialized) {
168:             register_shutdown_function('session_write_close');
169:         }
170: 
171:         static::$_initialized = true;
172:         static::$_isCLI = (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg');
173:     }
174: 
175: /**
176:  * Setup the Path variable
177:  *
178:  * @param string|null $base base path
179:  * @return void
180:  */
181:     protected static function _setPath($base = null) {
182:         if (empty($base)) {
183:             static::$path = '/';
184:             return;
185:         }
186:         if (strpos($base, 'index.php') !== false) {
187:             $base = str_replace('index.php', '', $base);
188:         }
189:         if (strpos($base, '?') !== false) {
190:             $base = str_replace('?', '', $base);
191:         }
192:         static::$path = $base;
193:     }
194: 
195: /**
196:  * Set the host name
197:  *
198:  * @param string $host Hostname
199:  * @return void
200:  */
201:     protected static function _setHost($host) {
202:         static::$host = $host;
203:         if (strpos(static::$host, ':') !== false) {
204:             static::$host = substr(static::$host, 0, strpos(static::$host, ':'));
205:         }
206:     }
207: 
208: /**
209:  * Starts the Session.
210:  *
211:  * @return bool True if session was started
212:  */
213:     public static function start() {
214:         if (static::started()) {
215:             return true;
216:         }
217: 
218:         $id = static::id();
219:         static::_startSession();
220:         if (!$id && static::started()) {
221:             static::_checkValid();
222:         }
223: 
224:         static::$error = false;
225:         static::$valid = true;
226:         return static::started();
227:     }
228: 
229: /**
230:  * Determine if Session has been started.
231:  *
232:  * @return bool True if session has been started.
233:  */
234:     public static function started() {
235:         if (function_exists('session_status')) {
236:             return isset($_SESSION) && (session_status() === PHP_SESSION_ACTIVE);
237:         }
238:         return isset($_SESSION) && session_id();
239:     }
240: 
241: /**
242:  * Returns true if given variable is set in session.
243:  *
244:  * @param string $name Variable name to check for
245:  * @return bool True if variable is there
246:  */
247:     public static function check($name) {
248:         if (!static::_hasSession() || !static::start()) {
249:             return false;
250:         }
251:         if (isset($_SESSION[$name])) {
252:             return true;
253:         }
254: 
255:         return Hash::get($_SESSION, $name) !== null;
256:     }
257: 
258: /**
259:  * Returns the session id.
260:  * Calling this method will not auto start the session. You might have to manually
261:  * assert a started session.
262:  *
263:  * Passing an id into it, you can also replace the session id if the session
264:  * has not already been started.
265:  * Note that depending on the session handler, not all characters are allowed
266:  * within the session id. For example, the file session handler only allows
267:  * characters in the range a-z A-Z 0-9 , (comma) and - (minus).
268:  *
269:  * @param string|null $id Id to replace the current session id
270:  * @return string Session id
271:  */
272:     public static function id($id = null) {
273:         if ($id) {
274:             static::$id = $id;
275:             session_id(static::$id);
276:         }
277:         if (static::started()) {
278:             return session_id();
279:         }
280:         return static::$id;
281:     }
282: 
283: /**
284:  * Removes a variable from session.
285:  *
286:  * @param string $name Session variable to remove
287:  * @return bool Success
288:  */
289:     public static function delete($name) {
290:         if (static::check($name)) {
291:             static::_overwrite($_SESSION, Hash::remove($_SESSION, $name));
292:             return !static::check($name);
293:         }
294:         return false;
295:     }
296: 
297: /**
298:  * Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself.
299:  *
300:  * @param array &$old Set of old variables => values
301:  * @param array $new New set of variable => value
302:  * @return void
303:  */
304:     protected static function _overwrite(&$old, $new) {
305:         if (!empty($old)) {
306:             foreach ($old as $key => $var) {
307:                 if (!isset($new[$key])) {
308:                     unset($old[$key]);
309:                 }
310:             }
311:         }
312:         foreach ($new as $key => $var) {
313:             $old[$key] = $var;
314:         }
315:     }
316: 
317: /**
318:  * Return error description for given error number.
319:  *
320:  * @param int $errorNumber Error to set
321:  * @return string Error as string
322:  */
323:     protected static function _error($errorNumber) {
324:         if (!is_array(static::$error) || !array_key_exists($errorNumber, static::$error)) {
325:             return false;
326:         }
327:         return static::$error[$errorNumber];
328:     }
329: 
330: /**
331:  * Returns last occurred error as a string, if any.
332:  *
333:  * @return mixed Error description as a string, or false.
334:  */
335:     public static function error() {
336:         if (static::$lastError) {
337:             return static::_error(static::$lastError);
338:         }
339:         return false;
340:     }
341: 
342: /**
343:  * Returns true if session is valid.
344:  *
345:  * @return bool Success
346:  */
347:     public static function valid() {
348:         if (static::start() && static::read('Config')) {
349:             if (static::_validAgentAndTime() && static::$error === false) {
350:                 static::$valid = true;
351:             } else {
352:                 static::$valid = false;
353:                 static::_setError(1, 'Session Highjacking Attempted !!!');
354:             }
355:         }
356:         return static::$valid;
357:     }
358: 
359: /**
360:  * Tests that the user agent is valid and that the session hasn't 'timed out'.
361:  * Since timeouts are implemented in CakeSession it checks the current static::$time
362:  * against the time the session is set to expire. The User agent is only checked
363:  * if Session.checkAgent == true.
364:  *
365:  * @return bool
366:  */
367:     protected static function _validAgentAndTime() {
368:         $userAgent = static::read('Config.userAgent');
369:         $time = static::read('Config.time');
370:         if (static::$_useForwardsCompatibleTimeout) {
371:             $time += (Configure::read('Session.timeout') * 60);
372:         }
373:         $validAgent = (
374:             Configure::read('Session.checkAgent') === false ||
375:             isset($userAgent) && static::$_userAgent === $userAgent
376:         );
377:         return ($validAgent && static::$time <= $time);
378:     }
379: 
380: /**
381:  * Get / Set the user agent
382:  *
383:  * @param string|null $userAgent Set the user agent
384:  * @return string Current user agent.
385:  */
386:     public static function userAgent($userAgent = null) {
387:         if ($userAgent) {
388:             static::$_userAgent = $userAgent;
389:         }
390:         if (empty(static::$_userAgent)) {
391:             CakeSession::init(static::$path);
392:         }
393:         return static::$_userAgent;
394:     }
395: 
396: /**
397:  * Returns given session variable, or all of them, if no parameters given.
398:  *
399:  * @param string|null $name The name of the session variable (or a path as sent to Set.extract)
400:  * @return mixed The value of the session variable, null if session not available,
401:  *   session not started, or provided name not found in the session, false on failure.
402:  */
403:     public static function read($name = null) {
404:         if (!static::_hasSession() || !static::start()) {
405:             return null;
406:         }
407:         if ($name === null) {
408:             return static::_returnSessionVars();
409:         }
410:         $result = Hash::get($_SESSION, $name);
411: 
412:         if (isset($result)) {
413:             return $result;
414:         }
415:         return null;
416:     }
417: 
418: /**
419:  * Returns all session variables.
420:  *
421:  * @return mixed Full $_SESSION array, or false on error.
422:  */
423:     protected static function _returnSessionVars() {
424:         if (!empty($_SESSION)) {
425:             return $_SESSION;
426:         }
427:         static::_setError(2, 'No Session vars set');
428:         return false;
429:     }
430: 
431: /**
432:  * Writes value to given session variable name.
433:  *
434:  * @param string|array $name Name of variable
435:  * @param mixed $value Value to write
436:  * @return bool True if the write was successful, false if the write failed
437:  */
438:     public static function write($name, $value = null) {
439:         if (!static::start()) {
440:             return false;
441:         }
442: 
443:         $write = $name;
444:         if (!is_array($name)) {
445:             $write = array($name => $value);
446:         }
447:         foreach ($write as $key => $val) {
448:             static::_overwrite($_SESSION, Hash::insert($_SESSION, $key, $val));
449:             if (Hash::get($_SESSION, $key) !== $val) {
450:                 return false;
451:             }
452:         }
453:         return true;
454:     }
455: 
456: /**
457:  * Reads and deletes a variable from session.
458:  *
459:  * @param string $name The key to read and remove (or a path as sent to Hash.extract).
460:  * @return mixed The value of the session variable, null if session not available,
461:  *   session not started, or provided name not found in the session.
462:  */
463:     public static function consume($name) {
464:         if (empty($name)) {
465:             return null;
466:         }
467:         $value = static::read($name);
468:         if ($value !== null) {
469:             static::_overwrite($_SESSION, Hash::remove($_SESSION, $name));
470:         }
471:         return $value;
472:     }
473: 
474: /**
475:  * Helper method to destroy invalid sessions.
476:  *
477:  * @return void
478:  */
479:     public static function destroy() {
480:         if (!static::started()) {
481:             static::_startSession();
482:         }
483: 
484:         if (static::started()) {
485:             if (session_id() && static::_hasSession()) {
486:                 session_write_close();
487:                 session_start();
488:             }
489:             session_destroy();
490:             unset($_COOKIE[static::_cookieName()]);
491:         }
492: 
493:         $_SESSION = null;
494:         static::$id = null;
495:         static::$_cookieName = null;
496:     }
497: 
498: /**
499:  * Clears the session.
500:  *
501:  * Optionally also clears the session id and renews the session.
502:  *
503:  * @param bool $renew If the session should also be renewed. Defaults to true.
504:  * @return void
505:  */
506:     public static function clear($renew = true) {
507:         if (!$renew) {
508:             $_SESSION = array();
509:             return;
510:         }
511: 
512:         $_SESSION = null;
513:         static::$id = null;
514:         static::renew();
515:     }
516: 
517: /**
518:  * Helper method to initialize a session, based on CakePHP core settings.
519:  *
520:  * Sessions can be configured with a few shortcut names as well as have any number of ini settings declared.
521:  *
522:  * @return void
523:  * @throws CakeSessionException Throws exceptions when ini_set() fails.
524:  */
525:     protected static function _configureSession() {
526:         $sessionConfig = Configure::read('Session');
527: 
528:         if (isset($sessionConfig['defaults'])) {
529:             $defaults = static::_defaultConfig($sessionConfig['defaults']);
530:             if ($defaults) {
531:                 $sessionConfig = Hash::merge($defaults, $sessionConfig);
532:             }
533:         }
534:         if (!isset($sessionConfig['ini']['session.cookie_secure']) && env('HTTPS')) {
535:             $sessionConfig['ini']['session.cookie_secure'] = 1;
536:         }
537:         if (isset($sessionConfig['timeout']) && !isset($sessionConfig['cookieTimeout'])) {
538:             $sessionConfig['cookieTimeout'] = $sessionConfig['timeout'];
539:         }
540:         if (isset($sessionConfig['useForwardsCompatibleTimeout']) && $sessionConfig['useForwardsCompatibleTimeout']) {
541:             static::$_useForwardsCompatibleTimeout = true;
542:         }
543: 
544:         if (!isset($sessionConfig['ini']['session.cookie_lifetime'])) {
545:             $sessionConfig['ini']['session.cookie_lifetime'] = $sessionConfig['cookieTimeout'] * 60;
546:         }
547: 
548:         if (!isset($sessionConfig['ini']['session.name'])) {
549:             $sessionConfig['ini']['session.name'] = $sessionConfig['cookie'];
550:         }
551:         static::$_cookieName = $sessionConfig['ini']['session.name'];
552: 
553:         if (!empty($sessionConfig['handler'])) {
554:             $sessionConfig['ini']['session.save_handler'] = 'user';
555: 
556:             // In PHP7.2.0+ session.save_handler can't be set to 'user' by the user.
557:             // https://github.com/php/php-src/commit/a93a51c3bf4ea1638ce0adc4a899cb93531b9f0d
558:             if (version_compare(PHP_VERSION, '7.2.0', '>=')) {
559:                 unset($sessionConfig['ini']['session.save_handler']);
560:             }
561:         } elseif (!empty($sessionConfig['session.save_path']) && Configure::read('debug')) {
562:             if (!is_dir($sessionConfig['session.save_path'])) {
563:                 mkdir($sessionConfig['session.save_path'], 0775, true);
564:             }
565:         }
566: 
567:         if (!isset($sessionConfig['ini']['session.gc_maxlifetime'])) {
568:             $sessionConfig['ini']['session.gc_maxlifetime'] = $sessionConfig['timeout'] * 60;
569:         }
570:         if (!isset($sessionConfig['ini']['session.cookie_httponly'])) {
571:             $sessionConfig['ini']['session.cookie_httponly'] = 1;
572:         }
573:         // For IE<=8
574:         if (!isset($sessionConfig['cacheLimiter'])) {
575:             $sessionConfig['cacheLimiter'] = 'must-revalidate';
576:         }
577: 
578:         if (empty($_SESSION) && !headers_sent() && (!function_exists('session_status') || session_status() !== PHP_SESSION_ACTIVE)) {
579:             if (!empty($sessionConfig['ini']) && is_array($sessionConfig['ini'])) {
580:                 foreach ($sessionConfig['ini'] as $setting => $value) {
581:                     if (ini_set($setting, $value) === false) {
582:                         throw new CakeSessionException(__d('cake_dev', 'Unable to configure the session, setting %s failed.', $setting));
583:                     }
584:                 }
585:             }
586:         }
587:         if (!empty($sessionConfig['handler']) && !isset($sessionConfig['handler']['engine'])) {
588:             call_user_func_array('session_set_save_handler', $sessionConfig['handler']);
589:         }
590:         if (!empty($sessionConfig['handler']['engine']) && !headers_sent()) {
591:             $handler = static::_getHandler($sessionConfig['handler']['engine']);
592:             if (!function_exists('session_status') || session_status() !== PHP_SESSION_ACTIVE) {
593:                 session_set_save_handler(
594:                     array($handler, 'open'),
595:                     array($handler, 'close'),
596:                     array($handler, 'read'),
597:                     array($handler, 'write'),
598:                     array($handler, 'destroy'),
599:                     array($handler, 'gc')
600:                 );
601:             }
602:         }
603:         Configure::write('Session', $sessionConfig);
604:         static::$sessionTime = static::$time;
605:         if (!static::$_useForwardsCompatibleTimeout) {
606:             static::$sessionTime += ($sessionConfig['timeout'] * 60);
607:         }
608:     }
609: 
610: /**
611:  * Get session cookie name.
612:  *
613:  * @return string
614:  */
615:     protected static function _cookieName() {
616:         if (static::$_cookieName !== null) {
617:             return static::$_cookieName;
618:         }
619: 
620:         static::init();
621:         static::_configureSession();
622: 
623:         return static::$_cookieName = session_name();
624:     }
625: 
626: /**
627:  * Returns whether a session exists
628:  *
629:  * @return bool
630:  */
631:     protected static function _hasSession() {
632:         return static::started()
633:             || !ini_get('session.use_cookies')
634:             || isset($_COOKIE[static::_cookieName()])
635:             || static::$_isCLI
636:             || (ini_get('session.use_trans_sid') && isset($_GET[session_name()]));
637:     }
638: 
639: /**
640:  * Find the handler class and make sure it implements the correct interface.
641:  *
642:  * @param string $handler Handler name.
643:  * @return CakeSessionHandlerInterface
644:  * @throws CakeSessionException
645:  */
646:     protected static function _getHandler($handler) {
647:         list($plugin, $class) = pluginSplit($handler, true);
648:         App::uses($class, $plugin . 'Model/Datasource/Session');
649:         if (!class_exists($class)) {
650:             throw new CakeSessionException(__d('cake_dev', 'Could not load %s to handle the session.', $class));
651:         }
652:         $handler = new $class();
653:         if ($handler instanceof CakeSessionHandlerInterface) {
654:             return $handler;
655:         }
656:         throw new CakeSessionException(__d('cake_dev', 'Chosen SessionHandler does not implement CakeSessionHandlerInterface it cannot be used with an engine key.'));
657:     }
658: 
659: /**
660:  * Get one of the prebaked default session configurations.
661:  *
662:  * @param string $name Config name.
663:  * @return bool|array
664:  */
665:     protected static function _defaultConfig($name) {
666:         $defaults = array(
667:             'php' => array(
668:                 'cookie' => 'CAKEPHP',
669:                 'timeout' => 240,
670:                 'ini' => array(
671:                     'session.use_trans_sid' => 0,
672:                     'session.cookie_path' => static::$path
673:                 )
674:             ),
675:             'cake' => array(
676:                 'cookie' => 'CAKEPHP',
677:                 'timeout' => 240,
678:                 'ini' => array(
679:                     'session.use_trans_sid' => 0,
680:                     'url_rewriter.tags' => '',
681:                     'session.serialize_handler' => 'php',
682:                     'session.use_cookies' => 1,
683:                     'session.cookie_path' => static::$path,
684:                     'session.save_path' => TMP . 'sessions',
685:                     'session.save_handler' => 'files'
686:                 )
687:             ),
688:             'cache' => array(
689:                 'cookie' => 'CAKEPHP',
690:                 'timeout' => 240,
691:                 'ini' => array(
692:                     'session.use_trans_sid' => 0,
693:                     'url_rewriter.tags' => '',
694:                     'session.use_cookies' => 1,
695:                     'session.cookie_path' => static::$path,
696:                     'session.save_handler' => 'user',
697:                 ),
698:                 'handler' => array(
699:                     'engine' => 'CacheSession',
700:                     'config' => 'default'
701:                 )
702:             ),
703:             'database' => array(
704:                 'cookie' => 'CAKEPHP',
705:                 'timeout' => 240,
706:                 'ini' => array(
707:                     'session.use_trans_sid' => 0,
708:                     'url_rewriter.tags' => '',
709:                     'session.use_cookies' => 1,
710:                     'session.cookie_path' => static::$path,
711:                     'session.save_handler' => 'user',
712:                     'session.serialize_handler' => 'php',
713:                 ),
714:                 'handler' => array(
715:                     'engine' => 'DatabaseSession',
716:                     'model' => 'Session'
717:                 )
718:             )
719:         );
720:         if (isset($defaults[$name])) {
721:             return $defaults[$name];
722:         }
723:         return false;
724:     }
725: 
726: /**
727:  * Helper method to start a session
728:  *
729:  * @return bool Success
730:  */
731:     protected static function _startSession() {
732:         static::init();
733:         session_write_close();
734:         static::_configureSession();
735: 
736:         if (headers_sent()) {
737:             if (empty($_SESSION)) {
738:                 $_SESSION = array();
739:             }
740:         } else {
741:             $limit = Configure::read('Session.cacheLimiter');
742:             if (!empty($limit)) {
743:                 session_cache_limiter($limit);
744:             }
745:             session_start();
746:         }
747:         return true;
748:     }
749: 
750: /**
751:  * Helper method to create a new session.
752:  *
753:  * @return void
754:  */
755:     protected static function _checkValid() {
756:         $config = static::read('Config');
757:         if ($config) {
758:             $sessionConfig = Configure::read('Session');
759: 
760:             if (static::valid()) {
761:                 static::write('Config.time', static::$sessionTime);
762:                 if (isset($sessionConfig['autoRegenerate']) && $sessionConfig['autoRegenerate'] === true) {
763:                     $check = $config['countdown'];
764:                     $check -= 1;
765:                     static::write('Config.countdown', $check);
766: 
767:                     if ($check < 1) {
768:                         static::renew();
769:                         static::write('Config.countdown', static::$requestCountdown);
770:                     }
771:                 }
772:             } else {
773:                 $_SESSION = array();
774:                 static::destroy();
775:                 static::_setError(1, 'Session Highjacking Attempted !!!');
776:                 static::_startSession();
777:                 static::_writeConfig();
778:             }
779:         } else {
780:             static::_writeConfig();
781:         }
782:     }
783: 
784: /**
785:  * Writes configuration variables to the session
786:  *
787:  * @return void
788:  */
789:     protected static function _writeConfig() {
790:         static::write('Config.userAgent', static::$_userAgent);
791:         static::write('Config.time', static::$sessionTime);
792:         static::write('Config.countdown', static::$requestCountdown);
793:     }
794: 
795: /**
796:  * Restarts this session.
797:  *
798:  * @return void
799:  */
800:     public static function renew() {
801:         if (session_id() === '') {
802:             return;
803:         }
804:         if (isset($_COOKIE[static::_cookieName()])) {
805:             setcookie(Configure::read('Session.cookie'), '', time() - 42000, static::$path);
806:         }
807:         if (!headers_sent()) {
808:             session_write_close();
809:             session_start();
810:             session_regenerate_id(true);
811:         }
812:     }
813: 
814: /**
815:  * Helper method to set an internal error message.
816:  *
817:  * @param int $errorNumber Number of the error
818:  * @param string $errorMessage Description of the error
819:  * @return void
820:  */
821:     protected static function _setError($errorNumber, $errorMessage) {
822:         if (static::$error === false) {
823:             static::$error = array();
824:         }
825:         static::$error[$errorNumber] = $errorMessage;
826:         static::$lastError = $errorNumber;
827:     }
828: 
829: }
830: 
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