什么是 cookie 和会话,它们之间有什么关系? [英] What are cookies and sessions, and how do they relate to each other?

查看:26
本文介绍了什么是 cookie 和会话,它们之间有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试专业地理解 cookie 和会话.我知道当浏览器连接到服务器时,服务器要求"浏览器在客户端浏览器的 cookie 文件夹中粘贴"一个带有phpsessid"的 cookie.

I am trying to understand cookies and sessions professionally. I know that when a browser connects to a server, the server "asks" the browser to "paste" a cookie with "phpsessid" in the client browser cookies folder.

现在我们有了phpsessid",如果客户端进入服务器,浏览器将phpsessid"发送到服务器,服务器查看 tmp 文件夹,如果我们有匹配项,它会加载回所有数据用户有这个客户,但我对这个过程有点困惑.

Now that we have the "phpsessid", if the client enters the server the browser sends to the server the "phpsessid" and the server takes a look at the tmp folder and if we have a match it loads back every data the user has for this client, but I am kinda confused with the process.

如果有人能帮助我理解创建会话和 cookie 的过程 - 幕后发生的事情,我将不胜感激.

I will be thankful if some one can help me understand those processes of creating a session and cookies - what is happening behind the scenes.

推荐答案

让我们来看看这个:

Cookiessessions 都是在不同的浏览器发出的请求.多亏了他们,例如,您不必每次在 StackOverflow 上请求页面时都登录.

Cookies and sessions are both ways to preserve the application's state between different requests the browser makes. It's thanks to them that, for instance, you don't need to log in every time you request a page on StackOverflow.

Cookie 是一小部分数据(最大长度为 4KB),以键=值对的形式保存数据:

Cookies are small bits of data, (maximum of 4KB long), which hold data in a key=value pairs:

name=value; name2=value2

这些由 JavaScript 设置,或通过服务器使用 HTTP 标头.

These are set either by JavaScript, or via the server using an HTTP header.

Cookie 设置了到期日期,例如使用 HTTP 标头:

Cookies have an expiry datetime set, example using HTTP headers:

Set-Cookie: name2=value2; Expires=Wed, 19 Jun 2021 10:18:14 GMT

这会导致浏览器设置一个名为 name2 的 cookie,其值为 value2,该 cookie 将在大约 9 年后过期.

Which would cause the browser to set a cookie named name2 with a value of value2, which would expire in about 9 years.

Cookie 被认为高度不安全,因为用户可以轻松操纵其内容.这就是为什么您应该始终验证 cookie 数据.不要假设您从 cookie 中得到的东西一定是您所期望的.

Cookies are considered highly insecure because the user can easily manipulate their content. That's why you should always validate cookie data. Don't assume what you get from a cookie is necessarily what you expect.

Cookies 通常用于保存登录状态,其中从浏览器发送用户名和特殊哈希值,服务器根据数据库检查它们以批准访问.

Cookies are usually used to preserve login state, where a username and a special hash are sent from the browser, and the server checks them against the database to approve access.

Cookie 也经常用于会话创建.

Cookies are also often used in sessions creation.

会话略有不同.每个用户都会获得一个会话 ID,该 ID 通过 cookieGET 变量 发送回服务器进行验证.

Sessions are slightly different. Each user gets a session ID, which is sent back to the server for validation either by cookie or by GET variable.

会话通常是短暂的,这使得它们非常适合在应用程序之间保存临时状态.一旦用户关闭浏览器,会话也会过期.

Sessions are usually short-lived, which makes them ideal in saving temporary state between applications. Sessions also expire once the user closes the browser.

会话被认为比 cookie 更安全,因为变量本身保存在服务器上.这是它的工作原理:

Sessions are considered more secure than cookies because the variables themselves are kept on the server. Here's how it works:

  1. 服务器打开一个会话(通过 HTTP 标头设置 cookie)
  2. 服务器设置会话变量.
  3. 客户变更页面
  4. 客户端发送所有 cookie 以及步骤 1 中的会话 ID.
  5. 服务器从 cookie 中读取会话 ID.
  6. 服务器从数据库(或内存等)的列表中匹配会话 ID.
  7. 服务器找到匹配项,读取现在在 $_SESSION 超全局变量上可用的变量.
  1. Server opens a session (sets a cookie via HTTP header)
  2. Server sets a session variable.
  3. Client changes page
  4. Client sends all cookies, along with the session ID from step 1.
  5. Server reads session ID from cookie.
  6. Server matches session ID from a list in a database (or memory etc).
  7. Server finds a match, reads variables which are now available on $_SESSION superglobal.

如果 PHP 没有找到匹配项,它将开始一个新的会话,并重复 1-7 的步骤.

If PHP does not find a match, it will start a new session, and repeat the steps from 1-7.

您可以在会话中存储敏感信息,因为它保存在服务器上,但请注意,如果用户通过不安全的 WiFi 登录,会话 ID 仍然可能被盗.(攻击者可以嗅探cookie,并将其设置为自己的,他不会看到变量本身,但服务器会将攻击者识别为用户).

You can store sensitive information on a session because it is kept on the server, but be aware that the session ID can still be stolen if the user, let's say, logged in over an insecure WiFi. (An attacker can sniff the cookies, and set it as its own, he won't see the variables themselves, but the server will identify the attacker as the user).

这就是它的要点.您可以在 PHP 手册中了解有关这两个主题的更多信息.

That's the gist of it. You can learn more on the PHP manual on both subjects.

这篇关于什么是 cookie 和会话,它们之间有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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