<?php
/**
* Copyright (C) 2011 Kai Dorschner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Kai Dorschner <the-hide@address.com>
* @copyright Copyright 2011, Kai Dorschner
* @license http://www.gnu.org/licenses/gpl.html GPLv3
* @package mocovi
*/
class Session
{
/**
* Start global session by using PHP session management.
*
* It additionally checks if the header is already sent
* and throws an exception if it is.
*
* @access public
* @static
* @return void
*/
public static function start()
{
if(!session_id())
if(!headers_sent())
{
if(!session_start())
throw new Exception('Session not started successfully.');
}
else
throw new Exception('Session cannot be started. Headers already sent.');
}
/**
* Destroys a PHP session.
*
* It additionally deletes the session cookie (if the header is not already
* sent) and destroys every variable content relating to the session.
*
* @access public
* @static
* return void
*/
public static function destroy()
{
if(!headers_sent())
{
$params = session_get_cookie_params();
setcookie
( session_name()
, ''
, time() - 3600 // Set cookie valide time to the past (1h)
, $params["path"]
, $params["domain"]
, $params["secure"]
, $params["httponly"]
);
}
session_unset();
session_destroy();
}
/**
* Returns the value of a session var.
*
* @access public
* @static
* @return String Session-var value
* @param string $name Name of the session var
*/
public static function get($name)
{
if(isset($_SESSION[$name]))
return $_SESSION[$name];
return false;
}
/**
* Sets/overwrites a session var.
*
* @access public
* @static
* @return void
* @param string $name Name of the session var
* @param string $value New value of the session var
*/
public static function set($name, $value)
{
$_SESSION[$name] = $value;
}
/**
* Deletes a session var.
*
* @access public
* @static
* @return void
* @param string $name Name of the session var
*/
public static function delete($name)
{
unset($_SESSION[$name]);
}
/**
* Returns the current session ID.
*
* @access public
* @static
* @return string Session ID
*/
public static function id()
{
return session_id();
}
}