不太理解PHP的session概念
主要是不太明白session的生存周期是如何计算的,它是如何携带状态信息的,望大家指教。
回复内容:
主要是不太明白session的生存周期是如何计算的,它是如何携带状态信息的,望大家指教。
其实session不是php独有的概念,所有的web应用都要涉及到会话(session)。它其实就是保存状态信息的一种机制。
很多人没有想过什么是状态信息,或者web里为什么会有这个机制。其实这跟web的特点是紧密相连的,web是没有长连接的,每个请求过去后,就与服务器断开连接了,这样对服务器来说每个请求都是新请求,所以它没有延续性,没有延续性就没有状态。
但是对我们来说无状态显然不可接受,我不可能每个页面都要求用户输入用户名密码吧,我们需要的是状态?什么是状态,就是可以延续的态,不管发送多少个请求,我都可以保持应有的态。因此我们要解决两个问题
- 怎样保存状态
- 怎样获取状态
于是session机制就应运而生了,其浏览器和服务器的交互机制可以理解为
- 浏览器对服务器说:"嗨,亲爱的,我来了(发起请求),但每天都有无数跟我长的一样家伙来找你,我可不希望跟别人分享那份只属于我们的记忆(状态)"
- 服务器对浏览器说:"不要紧,我把对你记忆都锁在这个盒子(session存储,file,memcached或者其它什么玩意)里了,把钥匙(session key)给你(Set-Cookie)"
- 浏览器说:"好的,我放在荷包(Cookie)里了。我走了,88(请求完成)"
- 第二天(第二次请求),浏览器又来找服务器了:"嗨,我又来了,用我荷包里的钥匙,打开你的记忆之盒吧"
- 服务器说:"打开了!真的是你,想死我了..."
影响php session生命周期的有两个因素,一个是Server端的session信息是否还存活,另一个是Browser端存在Cookie里的PHPSESSID是否还在。
Server端的session信息,当启动session的时候创建,一般放在一个文件里,php启动gc的时候会被回收掉
Client端的Cookie里记录的PHPSESSID记录了一个哈希值,指向一个Server端的session,当这个Cookie失效的时候,这个哈希值没了,session也就失效了。
通过:
phpinfo();
可以看到Session的一系列配置,这些配置也可影响Session的生命周期。
Php 在各个请求之间传递的是session id, 然后将session数据存放在服务器端的文件或者数据库中,也有为了性能的提升保存在内存中的。无非就是三种方式:
1. 通过http请求中的Cookies头部来传递session id
2. 通过http get请求来传递session id, 也就是将session id 放在url参数中
3. 通过http post请求来传递session id,也就是将session id 放在post数据中进行传递
我在自己的博客中结合http写了一篇阐述php session本质的文章 - http://www.360weboy.com/php/session-c...。 有兴趣的话,你可以细细读下,如果发现有什么不妥之处,你可以在评论中指出来,我会核对加以纠正。 希望更志同道合的朋友一起分享技术思想。
70 回答的好幽默,好形象。(然后一不小心写错地方了。)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
