PHP会话如何工作? (不是“如何使用?") [英] How do PHP sessions work? (not "how are they used?")

查看:71
本文介绍了PHP会话如何工作? (不是“如何使用?")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会话文件通常存储在服务器上的/tmp/中,并命名为sess_{session_id}.我一直在查看内容,无法弄清楚它们的实际工作原理.

Session files are usually stored in, say, /tmp/ on the server, and named sess_{session_id}. I have been looking at the contents and cannot figure out how they really work.

从文件中获取变量名称和内容很容易.但是PHP如何知道哪个会话属于谁?

Fetching the variable name and and content from the file is easy. But how does PHP know what session belongs to whom?

session_id似乎是完全随机的,一个IP地址可以有多个用户,并且如果每个用户打开了多个浏览器窗口,则每个用户可以有多个会话.

The session_id seems totally random and one IP address can have several users, and each user can have several sessions if they have more than one browser window open.

那它如何工作?

推荐答案

一般情况下:

  • 会话ID是在创建用户会话后发送给用户的.
  • 它存储在cookie中(默认情况下称为PHPSESSID)
  • 该Cookie由浏览器随每个请求发送到服务器
  • 服务器(PHP)使用包含session_id的cookie来知道哪个文件对应于该用户.
  • the session id is sent to the user when his session is created.
  • it is stored in a cookie (called, by default, PHPSESSID)
  • that cookie is sent by the browser to the server with each request
  • the server (PHP) uses that cookie, containing the session_id, to know which file corresponds to that user.

会话文件中的数据是$_SESSION的内容,已序列化(即,表示为字符串-具有诸如序列化);并且在PHP加载文件时未序列化以填充$_SESSION数组.

The data in the sessions files is the content of $_SESSION, serialized (ie, represented as a string -- with a function such as serialize) ; and is un-serialized when the file is loaded by PHP, to populate the $_SESSION array.


有时,会话ID不会存储在Cookie中,而是也会通过URL发送-但这在当今非常罕见.


Sometimes, the session id is not stored in a cookie, but sent in URLs, too -- but that's quite rare, nowadays.


有关更多信息,请查看手册的会话处理部分,提供了一些有用的信息.


For more informations, you can take a look at the Session Handling section of the manual, that gives some useful informations.

例如,有一个有关传递会话ID 的页面,解释了如何使用Cookie或URL在页面之间传递会话ID,以及哪些配置选项会对此产生影响.

For instance, there is a page about Passing the Session ID, which explains how the session id is passed from page to page, using a cookie, or in URLs -- and which configuration options affect this.

这篇关于PHP会话如何工作? (不是“如何使用?")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆