Home php教程 php手册 PHP中SESSION使用中的一点总结

PHP中SESSION使用中的一点总结

Jun 13, 2016 am 10:48 AM
php session comminicate use Discover right Summarize most of

最近在网上的交流中发现大家对SESSION的使用上有很多误区,本质上就是不了解SESSION的工作原理。

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()
  来使其失效。
在浏览器端:
可以直接 www.2cto.com

setcookie('PHPSESSID','',123);
  让cookie过期,或者另外一种方式,但不能立即失效

session_set_cookie_params($time);//当前时间戳上的秒,例如60,即让其60秒后过期,不要用时间戳+自己设定的时间。

  上面所讲的都是让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测试,看看是否还保存着。

 

摘自  技术熊猫
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles