Blame view

yaf/Hood/Session.php 1.56 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 14/12/26
 * Time: 上午12:39
 */

namespace Hood;

use Hood\Core\Session\SessionNamespace;
use Hood\Core\Session\CacheSession;
use Hood\Core\Session\FileSession;

class Session
{

    private static $_session = null;

    /**
     * 开启Session并设置NameSpace
     * @param $namespace
     * @return SessionNamespace
     */
    static public function start($namespace = 'session_default', $sessionName = null, $domain = null)
    {
        if (self::isSessionStarted() == false || self::$_session == null) {
            self::$_session = new CacheSession($sessionName, $domain);
            session_start();
        }
        return new SessionNamespace ($namespace);
    }

    /**
     * 开启Session并设置NameSpace,使用File
     * @param $namespace
     * @return SessionNamespace
     */
    static public function fileStart($namespace = 'session_default')
    {
        if (self::isSessionStarted() == false || self::$_session == null) {
            self::$_session = new FileSession();
            session_start();
        }
        return new SessionNamespace ($namespace);
    }


    /**
     * 检查Session是否开启
     * @return bool
     */
    static private function isSessionStarted()
    {
        if (php_sapi_name() !== 'cli') {
            if (version_compare(phpversion(), '5.4.0', '>=')) {
                return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
            } else {
                return session_id() === '' ? FALSE : TRUE;
            }
        }
        return FALSE;
    }
}