简体   繁体   English

PHP-如何在不使用Cookie的情况下存储用户的首选项(移动网站或完整网站)

[英]PHP - How to store user's preference (Mobile or full site) without using Cookies

I need to be able to load the user's previously set preference (within the same session, doesn't have to persist between sessions) of if they want to view the mobile version or full version of the site. 我需要能够加载用户先前设置的偏好设置(在同一会话中,不必在会话之间持久),如果他们想要查看网站的移动版本或完整版本。

I would like to achieve this via a cookieless approach, however I am pretty sure that storing stuff in the $_SESSION variable will write to a cookie. 我想通过无cookie的方法来实现这一点,但是我很确定将内容存储在$ _SESSION变量中会写入cookie。

Is there a better way of doing this? 有更好的方法吗?

Thank you! 谢谢!

Xavier. 泽维尔。

You can disable cookies for the session: 您可以为该会话禁用Cookie:

ini_set('session.use_cookies', 0);
ini_set('session.use_only_cookies', 0);
ini_set('session.use_trans_sid', 1);

this will force any client to append his session-id to every request he makes in order to authenticate. 这将迫使任何客户端将其会话ID附加到他提出的每个请求中以进行身份​​验证。 (Well actualy your server will append it to any generated link, so the client includes it automatically) (实际上,您的服务器会将其附加到任何生成的链接,因此客户端会自动包括它)

However, this has several drawbacks: 但是,这有几个缺点:

  • The session could be "copied" to other clients, when the url containing the session id is transfered. 当传输包含会话ID的URL时,可以将该会话“复制”到其他客户端。
  • Closing the browser and reopening it does reset the session. 关闭浏览器并重新打开它会重置会话。
  • Navigating away from your website may list a valid session id in the "referer" of another server - which might show it somewhere, so any unknown person might get access to the (maybe still valid) session. 离开您的网站可能会在另一台服务器的“引荐来源网址”中列出有效的会话ID,这可能会在某处显示该会话ID,因此任何未知的人都可以访问(可能仍然有效)会话。

First of there is 2 kinds of cookies. 首先是2种Cookie。

  1. The dreaded client cookies. 可怕的客户端cookie。
  2. The sessions cookies. 会话cookie。 A session cookie is only a unique hash set by the server to identifiy the users session (not the user!), you could achive the same thing by adding a query parameter to every link on the site with a number for the session. 会话cookie只是服务器设置的唯一哈希,用于标识用户会话(不是用户!),您可以通过向站点上的每个链接添加查询参数并为其添加编号来实现相同目的。

The $_SESSION variable contains the variables you set on the server linked to the users session. $ _SESSION变量包含您在服务器上设置的链接到用户会话的变量。

You can disable the session cookie and only use query parameters in php.ini 您可以禁用会话cookie,仅在php.ini中使用查询参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM