何时使用 URL/会话/cookie 变量?

发布于 2024-09-15 19:30:12 字数 350 浏览 4 评论 0原文

我做了很多 php 和 javascript,但我认为这是一个相对与语言无关的问题。是否有关于何时使用以下各项的最佳实践:

  • URL 变量
  • SESSION 变量
  • cookie

我了解其中一些不能的固有限制,但它们的使用有时可能会重叠,也是,这些实例正是我真正要问的。


编辑 只是为了澄清:我非常熟悉哪种方法存储在哪里以及客户端/服务器可以访问哪些方法的技术细节。我正在寻找的是更高级别的东西,比如“临时用户设置应该存在于cookie中,数据状态信息应该存在于服务器上,等等......”

谢谢!

I do a lot of php and javascript, but I think this is relatively language-agnostic question. Are there any best-practices for when to use each of:

  • URL variables
  • SESSION variables
  • cookies

I understand the inherent limitations of what some of them can't do, but it seems like their use can overlap sometimes, too, and those instances are what I'm really asking about.


EDIT
Just to clarify: I'm pretty familiar with the technicalities of which method is stored where, and which the client/server can access. What I am looking for is something a little higher-level, like "temporary user settings should live in cookies, data state info should live on the server, etc..."

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-09-22 19:30:12

一般来说:

  1. 使用 URL (GET) 参数向服务器发送简单的请求参数,例如。搜索查询或产品列表中的页码。

  2. 顾名思义,使用会话变量来存储与特定用户会话相关的临时数据,例如。登录用户的 ID 或非持久购物车。

  3. 尽可能避免使用 cookie。谨慎使用它们来存储与特定计算机/用户配置文件相关的设置,例如。诸如“记住我在此计算机上的用户 ID”之类的设置。

In general:

  1. Use URL (GET) parameters for sending simple request parameters to the server, eg. a search query or the page number in a product listing.

  2. Use session variables, as the name indicates, to store temporary data associated with a specific user session, eg. a logged-in user's ID or a non-persistent shopping cart.

  3. Avoid using cookies when possible. Use them sparingly to store settings that are tied to a particular computer / user profile, eg. a setting such as "remember my user ID on this computer".

走过海棠暮 2024-09-22 19:30:12
  1. 会话存储在服务器上,这意味着客户端无权访问您存储的有关它们的信息。会话数据存储在您的服务器上,不需要随每个页面完整传输;客户端只需发送一个 ID,然后从服务器加载数据。

  2. 另一方面,Cookie 存储在客户端。它们可以经久耐用,并且可以让您在拥有 Web 服务器集群时更加顺利地工作。然而,与会话不同的是,存储在 Cookie 中的数据会随每个页面请求完整传输。如果您需要更长的登录会话,则应该使用 cookie。

  3. URL 变量 (GET) 是开放的,用户可以看到。它们也很有用,因为它允许用户为页面添加书签并共享链接。

  1. Sessions are stored on the server, which means clients do not have access to the information you store about them. Session data, being stored on your server, does not need to be transmitted in full with each page; clients just need to send an ID and the data is loaded from the server.

  2. On the other hand, Cookies are stored on the client. They can be made durable for a long time and would allow you to work more smoothly when you have a cluster of web servers. However unlike Sessions, data stored in Cookies is transmitted in full with each page request. You should use cookie if you need longer logged-in sessions.

  3. URL variables (GET) are open and can be seen by user. They are also useful as it allows the user to bookmark the page and share the link.

ぃ双果 2024-09-22 19:30:12

当 cookie 被禁用时,PHP 会将会话 ID 直接嵌入到 URL 中。然后,会话 ID 成为可通过 HTTP GET 变量访问的值。

PHP embeds the session id directly into URLs when cookies are disabled. Then, the session id becomes a value accessible thru an HTTP GET variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文