コード例 #1
0
ファイル: Session.class.php プロジェクト: Superbeest/Core
 /**
  * The constructor initializes the session and sends the session headers
  * The session will be based on the given session handler. The session handler can be overridden by setting the SESSION_HANDLER definition.
  * Do note: if a handler fails to initialize, it will default to the DEFAULT_HANDLER.
  */
 public final function __construct()
 {
     if (!self::$headersSend) {
         //apply the session handler directive to our system
         if (!defined('SESSION_HANDLER')) {
             throw new \System\Error\Exception\SystemException('Invalid session handler given. Set to HANDLER_FILES; HANDLER_MEMCACHE; or a SessionHandler class');
         }
         self::$currentHandler = SESSION_HANDLER;
         switch (self::$currentHandler) {
             case SessionHandler::HANDLER_MEMCACHE:
                 //initialize memcache and establish a connection. This makes sure the MEMCACHE_* settings are defined and correct.
                 $memcache = new \System\Cache\Memcache\Memcache();
                 //if we are not connected, we do a fallthrough to regular files.
                 if ($memcache->isConnected()) {
                     ini_set('session.save_handler', 'memcache');
                     $hosts = $ports = array();
                     \System\Cache\Memcache\Memcache::getServers($hosts, $ports);
                     $connectArray = array();
                     foreach ($hosts as $index => $host) {
                         $connectArray[] = 'tcp://' . $host . ':' . $ports[$index];
                     }
                     //the save path expects something like 'tcp://IP:PORT'
                     ini_set('session.save_path', implode(',', $connectArray));
                     break;
                 }
                 //we do a fallthrough if the memcache is not connected and revert to files
                 self::$currentHandler = SessionHandler::HANDLER_FILES;
             case SessionHandler::HANDLER_FILES:
                 if (session_save_path() == '') {
                     session_save_path(PATH_TEMP);
                     //we use the temporary folder for this.
                 }
                 break;
             default:
                 if (class_exists(self::$currentHandler) && in_array('SessionHandlerInterface', class_implements(self::$currentHandler, true))) {
                     self::$customHandler = new self::$currentHandler();
                     session_set_save_handler(self::$customHandler, true);
                 } else {
                     throw new \System\Error\Exception\SystemException('Invalid session handler given: ' . self::$currentHandler);
                 }
         }
         //this implicitly sends a few headers
         session_cache_limiter('nocache');
         session_cache_expire(180);
         //this is the default, but we set it explicitly
         session_name(SITE_IDENTIFIER . '_session');
         session_start();
         //we should only set our headers once
         self::$headersSend = true;
     }
 }