PHP中SESSION使用中的一点经验总结
发布时间:2023-11-19 11:48:54
SESSION会话开启时,会首先发送一个对浏览器的唯一标识session_id的cookie(名字为PHPSESSID可以通过session_name()获取),同session.save_handler = files的情况下,在服务器的指定目录(如temp)下生成一个没有后缀的文件,名字是
'sess_" + 'session_id';
这样就完成了基本的设置。那么在下一次发起http请求时,首先浏览器会发送这个当前域名下的所有cookie名字和值过去,这样服务器就能根据cookie中的session_id来去读取session文件,而不会混淆这个session属于谁。
这一步具体如下:
SESSION发送一个对浏览器的唯一cookie变量session_id,这个session_id变量有名字、有值。变量名(name)默认为PHPSESSID,变量值(value)为apach随机生成的字符串,类似rvag9m368vim7k8g4v7k2ank70 。通常说的session_id其实是指这个唯一的字符串rvag9m368vim7k8g4v7k2ank70。
具体的在FF的HTTP响应头下如此:
session_start();
在程序中上面一句就完成了上面的功能,假如浏览器没有发送PHPSESSID的cookie过来就发送一个过去,有就读取这个cookie,这样就能维持同一个会话。
好了既然知道了session的工作原理,那么我们可以推测到假如服务器端sess_rvag9m368vim7k8g4v7k2ank70 这个文件我们手动删除了,那么session失效,假如浏览器cookie失效,那么session照样失效。
在手动的情况下:
在服务器端,可以用
session_ destroy() 或者session_ unset()
来使其失效。
在浏览器端:
可以直接
?setcookie('PHPSESSID','',123);
让cookie过期,或者另外一种方式,但不能立即失效
?session_set_cookie_params($time);//当前时间戳上的秒,例如60,即让其60秒后过期,<SPAN style="COLOR: #ff0000">不要用时间戳+自己设定的时间。 </SPAN>
上面所讲的都是让session提前过期,但是想直接让session延迟行不行呢?除了修改配置(session.gc_maxlifetime)是不行的,在php.ini里面session.gc_maxlifetime 设置过期时间,到了这个时间,就有session.gc_probability /session.gc_divisor的概率被回收。假如到了这个时间,并且启动了GC进程,GC会去读取session文件的修改时间(mtime),发现大于和当前时间相减后大于session.gc_maxlifetime ,立刻删除。到此,我们也就明白了如何保持这个会话呢,只能在session.gc_maxlifetime 内,必须有用户在访问,每次访问都去修改下session,这样就这个session又多出session.gc_maxlifetime的存活时间。
另外说一下session.cookie_lifetime ,设置PHPSESSID在浏览器的存活时间,默认为0,IE下我发现是正常的,浏览器重启即cookie失效;FF下还继续存在。设置session.cookie_lifetime可以用session_set_cookie_params,
?session_set_cookie_params(60);//60 s session_start();
session.gc_maxlifetime和session.cookie_lifetime 共同决定了session的生存时间。
-------------------------------------------------------------
刚刚找了一下firefox cookie会话过期的资料,发现如下
This is apparently by design. Check out this Bugzilla bug:https://bugzilla.mozilla.org/show_bug.cgi?id=443354
Firefox has a feature where you close Firefox and it offers to save all your tabs, and then you restore the browser and those tabs come back. That's called session restore. What I didn't realize is that it'll also restore all the session cookies for those pages too! It treats it like you had never closed the browser.
This makes sense in the sense that if your browser crashed you get right back to where you were, but is a little disconcerting for web devs used to session cookies getting cleared. I've got some old session cookies from months ago that were set by sites I always have open in tabs.
To test this out, close all the tabs in your browser, then close the browser and restart it. I think the session cookies for your site should clear in that case. Otherwise you'd have to turn off session restore.
这是火狐的会话保存功能,FF设计就是如此。可以做这个close all the tabs in your browser, then close the browser and restart it测试,看看是否还保存着。
猜你喜欢
- 1.分包背景这里首先介绍下MultiDex的产生背景。当Android系统安装一个应用的时候,有一步是对Dex进行优化,这个过程有一个专门的
- 微软在今天早上发布新的安全通告证实,一处远程执行代码漏洞影响到了整个SQL Server产品线。该漏洞的入侵代码在两周前已经被公布在互联网上
- pymysql写入中文直接写入中文会出现错误。根据网上的方法,设置my.ini不成功sql = "INSERT INTO comp
- Git 创建仓库本章节我们将为大家介绍如何创建一个 Git 仓库。你可以使用一个已经存在的目录作为Git仓库。git initGit 使用
- 1.简介keplergl是由Uber开源的一款地理数据可视化工具,通过keplergl我们可以在Jupyter notebook中使用,可视
- 前言大家好!这个系列文章是W3CN 阿捷编写的。是一些制作过程中的心得和经验,希望对大家有点帮助。第一天开始制作符合标准的站点,第一件事情就
- 项目场景:在使用selenium模块进行数据爬取时,通常会遇到爬取iframe中的内容。会因为定位的作用域问题爬取不到数据。问题描述:我们以
- Python not equal operator returns True if two variables are of same ty
- 代码如下:function FSOlastline(filename) dim fso,f,temparray
- 微软昨天在其2009年专业开发者大会上展示了下一个版本的Internet Explorer浏览器IE9。尽管只是一个早期版本,IE开发团队还
- 之前公司有个绘制实时盈利率折线图的需求,实现的还不错,今天来分享下vue+echarts实现动态折线图的方法。实现代码<templat
- 前言本文主要给大家介绍了关于Golang map生成有序json数据的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍:先来看一段
- 本文实例讲述了Sanic框架安装与简单用法。分享给大家供大家参考,具体如下:Sanic是一个类似Flask的Python 3.5+ Web服
- 本文实例讲述了Python多线程应用于自动化测试操作。分享给大家供大家参考,具体如下:多线程执行测试用例实例:import threadin
- 这篇文章主要介绍了python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例,文中通过示例代码介绍的非常详细,对大家的
- url标记为变量通过把 URL 的一部分标记为 <variable_name> 就可以在 URL 中添加变量。标记的 部分会作为
- 1.现在有一个需求:我想在monitor应用中导入data文件夹下的apolos.py中的tes()函数并执行。打开monitor应用的视图
- 原问题:https://stackoverflow.com/questions/48122744/how-to-download-all-f
- 1. 字符编码简介1.1. ASCIIASCII(American Standard Code for Information Interc
- 本文内容是在《Python核心编程2》上看到的,感觉很有用便写出来,给大家参考参考!浅拷贝首先我们使用两种方式来拷贝对象,一种是切片,另外一