<?php
class Session
{
public static $seesionFlashName = '__FlashBack';
public function __construct() {
}
public static function start() {
ini_set('session.use_only_cookies', 'Off');
ini_set('session.use_cookies', 'On');
ini_set('session.use_trans_sid', 'Off');
ini_set('session.cookie_httponly', 'On');
if (isset($_COOKIE[session_name()]) && !preg_match('/^[a-zA-Z0-9,\-]{22,52}$/', $_COOKIE[session_name()])) {
exit('Error: Invalid session ID!');
}
session_set_cookie_params(0, '/');
session_start();
}
public static function id() {
return sha1(session_id());
}
public static function regenerate() {
session_regenerate_id(true);
}
public static function exists($name) {
if(isset($name) && $name != '') {
if(isset($_SESSION[$name])) {
return true;
}
}
return false;
}
public static function set($name='', $value='') {
if($name != '' && $value != '') {
$_SESSION[$name] = $value;
}
}
public static function get($name) {
if(self::exists($name)) {
return $_SESSION[$name];
}
return false;
}
public static function delete($name) {
if(self::exists($name)) {
unset($_SESSION[$name]);
}
return false;
}
public static function setFlash($value='') {
if($value != '') {
self::set(self::$seesionFlashName, $value);
}
}
public static function getFlash() {
if(self::exists(self::$seesionFlashName)) {
ob_start();
echo self::get(self::$seesionFlashName);
$content = ob_get_contents();
ob_end_clean();
self::delete(self::$seesionFlashName);
return $content;
}
return false;
}
public static function flashExists() {
return self::exists(self::$seesionFlashName);
}
public static function destroy() {
foreach($_SESSION as $sessionName) {
self::delete($sessionName);
}
session_destroy();
}
}
?>
伊朗 PHP 程序员
法尔哈德·赞德·莫加达姆