1、PHP自己的框架session()
2、session类(SessionBase.php)
<?php
class SessionBase
{/*** 设置session*/public static function set($name, $data, $expire=600){$session_data = array();$session_data['data'] = $data;$session_data['expire'] = time()+$expire;$_SESSION[$name] = $session_data;}/*** 读取session*/public static function get($name){if(isset($_SESSION[$name])){if($_SESSION[$name]['expire']>time()){return $_SESSION[$name]['data'];}else{self::clear($name);}}return false;}/*** 清除session* @param String $name session name*/private static function clear($name){unset($_SESSION[$name]);}}
3、封装session类function.php
function session(){$num=func_num_args();$session=new SessionBase();if($num>=2){if($num==3){//三个参数$expire=func_get_arg(2);}else{//二个参数$expire=3600;}$session::set(func_get_arg(0),func_get_arg(1),$expire);return true;}else{//一个参数return $session::get(func_get_arg(0));}
}
4、调用session(indexCrl.php)
<?php
class indexCrl extends CrlBase {public function index(){//设置sessionsession('name','haha',60);//获取sessionvar_dump(session('name'));//清空sessionsession('name',null);}}