Skip to content
Snippets Groups Projects
sessions.class.php 2.74 KiB
Newer Older
    public static $userSession = null;
    public static $user = null;
    public static function start()
    {
        session_start();
    }
    public static function unset()
    {
        session_unset();
    }
    public static function destroy()
    {
        session_destroy();
    }
    public static function set($key, $value)
    {
        $_SESSION[$key] = $value;
    }
    public static function delete($key)
    {
        session_unset($_SESSION[$key]);
    }
    public static function isset($key)
    {
        return isset($_SESSION[$key]);//this only returns true or false for the paticular i.e available or not
    }
    public static function get($key, $default = false)
    {
        if(isset($_SESSION[$key])) {
            sessions::isset($key);
            return $_SESSION[$key];//this returns the exact values for the key
        } else {
            return $default;
        }
    }
    public static function loadTemplate($name)
    {
        //print(" including $name.php");
        $script = $_SERVER["DOCUMENT_ROOT"] . get_config("base_path") . "__templates/$name.php";
        if(is_file($script)) {
            include $script;
        } else {
            sessions::loadTemplate('__error');
        }
        /* we are generally using this function and making multiple calls over our index.php file there all we need to do
           is call the function with the right name for eg load_template('__header') and here we gave the location of the
           file along with the extension .php and also this works because of our include function above and location is all that
           matters even a single/ makes a big issue....there are 4 functions like include,include_once
           (require,require_once  or it could be given as __Dir__."/../__templates/$name.php") */
        //this load_template has been moved from load.php to sessions.class.php for architecturing our framework
    }
    public static function renderPage()
    {
        sessions::loadTemplate('_master');
    }
    public static function currentScript()
    {
        return basename($_SERVER['SCRIPT_NAME'], '.php');
    }
    public static function getUser()
    {
        return sessions::$user;
    }
    public static function getUsersession()
    {
        return sessions::$userSession;
    }
        TODO://incase of error replace isValid() with isActive
    if(is_object(sessions::getUsersession())) {
        return sessions::getUsersession()->isValid();
    } else {
    }
    public static function ensureLogin()
    {
        if(!sessions::isAuthenticated()) {
            header("Location: /login.php");
        }
        die();
    }