下面我们来看一下Session是如何工作的。不知你是否知道通过Cookie来实现身份认证的吧。首先生成一个独一无二的Cookie作为用户身份的标志,并在数据库中进行注册。然后通过用户传递来的Cookie和数据库中注册的Cookie进行对照以确定用户的身份。
Session的工作原理也是这样。
首先,PHP为建立Session的用户产生一个独一无二的字符串,用来标志这个用户的session。一般将这个字符串称作Session Id。然后“sess”+Session Id为文件名(例如一个Session ID为111,那么文件名为sess_111)在服务器的文件系统中建立一个文件,在文件中保存用户在Session所定义的全局变量的变量名和值。然后再将Session Id作为一个名为PHPSession的Cookie保存在用户端的文件系统中。
然后,当用户再次连接服务器访问一个PHP脚本时,PHP从用户发来的PEESession这个Cookie中得到用户所在Session的Session Id,并根据Session Id从服务器的文件系统中保存Session信息的文件。最后从这个文件中读出用户在上次连接时所设置的全局变量的值。
因此,我们可以看到Session的工作原理和我们上一节所介绍的身份认证的工作原理是一样的。所不同的只是Session将信息保存在了服务器的文件系统中,而我们将信息保存在了数据库中。当然使用Session好处就是数据的保存和获取是由PHP自动完成的,而直接使用Cookie的话就需要自己动手进行数据的保存和获取。
Session利用Cookie的身份标志功能,将用户在浏览网站时需要保存的信息保存在服务器上。这样Session既克服了HTTP协议的缺陷,又防止了信息的泄漏,而且方便了编程者的使用,是一个非常好的解决方案。不过,Session的功能只有PHP4支持,PHP3是不支持Session的。因此使用PHP3进行网站构建的读者只能采用直接使用Cookie的方式。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

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