多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
YurunPHP中提供了丰富的Session操作和配置,无需手动session_start。 ## 相关配置项 #### SESSION_NAME 用在 cookie 或者 URL 中的会话名称, 例如:PHPSESSID。 只能使用字母和数字作为会话名称,建议尽可能的短一些, 并且是望文知意的名字(对于启用了 cookie 警告的用户来说,方便其判断是否要允许此 cookie)。 如果指定了 name 参数, 那么当前会话也会使用指定值作为名称。 > 会话名称至少需要一个字母,不能全部都使用数字, 否则,每次都会生成一个新的会话 ID。 #### SESSION_SAVEPATH 指定Session文件存储目录,如果不指定按照PHP默认配置存储。 #### SESSION_USE_COOKIES sessionid在客户端采用的存储方式,置1代表使用cookie记录客户端的sessionid,同时,$_COOKIE变量里才会有$_COOKIE['PHPSESSIONID']这个元素存在 #### SESSION_CACHE_EXPIRE Session的缓存有效期,单位为分钟 #### SESSION_CACHE_LIMITER 指定缓存机制名字 <table class="doctable table"> <thead> <tr class="firstRow"> <th> 值 </th> <th> 发送的Header </th> </tr> </thead> <tbody class="tbody"> <tr> <td> public </td> <td> <pre>Expires: (sometime in the future, according session.cache_expire) Cache-Control: public, max-age=(sometime in the future, according to session.cache_expire) Last-Modified: (the timestamp of when the session was last saved)</pre> </td> </tr> <tr> <td> private_no_expire </td> <td> <pre>Cache-Control: private, max-age=(session.cache_expire in the future), pre-check=(session.cache_expire in the future) Last-Modified: (the timestamp of when the session was last saved)</pre> </td> </tr> <tr> <td> private </td> <td> <pre>Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: private, max-age=(session.cache_expire in the future), pre-check=(session.cache_expire in the future) Last-Modified: (the timestamp of when the session was last saved)</pre> </td> </tr> <tr> <td> nocache </td> <td> <pre>Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache</pre> </td> </tr> </tbody> </table> #### SESSION_GC_PROBABILITY 每次请求时触发Session失效缓存清理的概率。取值范围:0.0-1.0 #### SESSION_MAX_LIFETIME Session数据在服务器端储存的时间,如果超过这个时间,那么Session数据就自动删除! #### SESSION_PREFIX SESSION前缀 #### SESSION_USER_SAVE_HANDLER Session用户处理类名 ## 方法 #### 设置Session ~~~ Session::set($name, $value) ~~~ | 参数名 | 描述 | | -- | -- | | $name | session名称 | | $value | session值 | ~~~ Session::set('pwd','123456'); // $_SESSION['pwd'] = '123456'; Session::set('@.pwd','123456'); // $_SESSION['prefix']['pwd'] = '123456'; ~~~ #### 获取Session ~~~ Session::get($name = null, $default = false) ~~~ | 参数名 | 描述 | | -- | -- | | $name | session名称 | | $default | 默认值 | ~~~ Session::get('pwd','123456'); // 取$_SESSION['pwd'],如果不存在返回123456 Session::get('@.pwd'); // 取$_SESSION['prefix']['pwd']的值 ~~~ #### 删除Session ~~~ Session::delete($name = null) ~~~ | 参数名 | 描述 | | -- | -- | | $name | session名称 | ~~~ Session::delete('pwd'); // 删除$_SESSION['pwd'] Session::delete('@.pwd'); // 删除$_SESSION['prefix']['pwd']的值 ~~~ #### 清空Session ~~~ Session::clear() ~~~ #### Session值是否存在 ~~~ Session::exists($name) ~~~ | 参数名 | 描述 | | -- | -- | | $name | session名称 | ~~~ Session::exists('pwd'); // $_SESSION['pwd']是否存在 Session::exists('@.pwd'); // $_SESSION['prefix']['pwd']是否存在 ~~~