PHP:会话的替代方案

发布于 2024-10-09 09:16:26 字数 433 浏览 3 评论 0原文

我有一个 PHP 应用程序,它非常依赖会话变量。登录后,用户将被重定向到一个页面,该页面执行代码以根据用户是谁来设置会话变量的负载。该应用程序使用来自不同来源的数据,并且会话用于存储 ID 号以查询数据库。

因此,当用户转到查询其资产管理系统的页面时,将通过会话调用该特定数据库的 ID。

我最近在会话变量方面遇到了很多问题。有时,在应用程序的生命周期内仅创建一个会话文件,有时每个会话请求都会产生一个新的会话 ID(仍然没有设法找出原因!)。

我的问题是这样的。有没有使用会话变量的替代方法?像全局变量还是其他方式?

我在这里有一个关于我的会话问题的问题 - PHP 会话变量没有被维护

I have a PHP application that relies on session variables quite a lot. After login the user get redirected to a page that executes code to set up a load of session variables depending on who the user is. The application is using data from different sources and the sessions are used to store ID numbers to query the databases.

So when the user goes to a page that will query their asset management system their ID for that particular database is called via the session.

I've had a LOT of problems with session variables recently. Sometimes only one session file is created during the lifetime of the app, and sometimes each session request results in a new session id (still haven't managed to find out why!).

My question is this. Is there an alternative to using session variables for this? Like globals or some other way?

I have a question open about my session problem here - PHP session variables not being maintaned

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

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

发布评论

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

评论(7

软的没边 2024-10-16 09:16:26

另一种选择是将会话数据存储在数据库中,为用户提供带有您的 session_id 的 cookie。这将使您对会话有更多的控制。

Another alternative is to store session data in database in give user a cookie with your session_id. That will give you more control around sessions.

太阳公公是暖光 2024-10-16 09:16:26

不,由于每个用户会话都应该有其一个值,因此您需要某种会话机制。 PHP 的会话非常适合这一点。您可能只需要正确配置它即可满足您的需求。

No, since each user session should have its one values, you need some kind of session mechanism. And PHP’s session is fine for that. You probably just need to configure it properly to meet your needs.

听你说爱我 2024-10-16 09:16:26

您可以使用其他一些“会话”解决方案,但这需要您做更多的管道工作。

例如,您可以使用基于 url 的会话,其中站点生成的每个 url 都包含一个 sessionid。这通常是对站点的重大重新设计,唯一需要的情况是您需要在多个域上进行相同的会话,或者您希望同一台计算机在浏览器的不同窗口中运行不同的会话。

但在你的情况下,我会检查会话被破坏的原因,可能是配置问题,或者你有其他东西搞砸了会话文件。

我从来没有遇到过 PHP 会话的任何问题,它们只是工作;)

You could use some other "session" solution but that requires you to do more of the plumbing.

For example, you could use url based sessions where every url the site generates includes a sessionid. This is usually a major redesign of the site and the only time it is required is if you need to have the same session over multiple domains OR if you like to have the same computer running different sessions in different windows in the browser.

But in your case I would check why the sessions are broken, its probably a configuration issue or you have someting else that screws up the session files.

I have never had any trouble with PHP sessions, they just work ;)

风和你 2024-10-16 09:16:26

会话的替代方案是cookie(事实上,会话通常是使用cookie来实现的)。但只有当您想要存储少量数据时才应使用cookie。

The alternative to sessions is cookies (in fact, sessions are usually implemented using cookies). But cookies should only be used if you want to store small amounts of data.

烧了回忆取暖 2024-10-16 09:16:26

会议似乎是解决此问题的正确解决方案。
一般来说,会话​​的工作方式如下:
在服务器上创建一个会话对象并分配一个 id。 id通过cookie发送到浏览器,并随着浏览器的请求发送到服务器,从而使服务器能够从内存中获取正确的会话对象。

您可能想查看您的会话是如何配置的。如果您不断“丢失”会话,一种可能的原因可能是您在子域之间“跳转”并且仅为一个子域设置了会话 cookie。另一个原因可能是您的会话过期时间太短。

Sessions seem like the right solution for this.
Generally sessions work like this:
A session object is created on the server and assigned an id. The id is sent to the browser in a cookie and sent to the server with request of the browser, thus enabling the server to fetch the correct session object from memory.

You may want to look at how your session is configured. If you keep "loosing" sessions, one possible cause could be that you are "jumping" between subdomains and have the session cookie set for only one subdomain. Another cause could be that you have a too low expiry on your session.

你怎么敢 2024-10-16 09:16:26

Cookie 并不是真正的替代方案,特别是在用于登录时,因为它们可以由用户更改,而对于会话来说,只是在计算机上存储包含标识哈希的 Cookie,并且修改它不会让它们走得太远。

不管怎样,会议有效。毫无疑问,你的问题是实施问题。我建议您花一些时间进行调试和测试,因为实施替代方法将导致代码变得极其复杂 - 而且您不想这样做,因为会话已经给您带来了麻烦。

Cookies aren't really an alternative, in particular when used for logins, since they can be changed by the user, while for sessions just store a cookie containing an identifying hash on their computer, and modifying it won't get them very far.

Anyway, sessions work. Your problem is, with no doubt, an implementation problem. I suggest you spend some time debugging and testing, since implementing an alternative method will lead to an incredibly more complex code - and you don't want to do it, since sessions are causing you trouble already.

离去的眼神 2024-10-16 09:16:26

对于在每个请求时重新创建的会话,您应该尝试调试服务器发送给您的 cookie。
在这些 cookie 中,检查 cookie 域是否正确(并检查 cookie 路径)。

要调试 cookie,您可以使用 Web 开发人员工具栏来显示页面加载后哪些 cookie 处于活动状态。但最好的工具是 Live Http headers,检查服务器发送的真实 headers,在那里你会找到真正的 cookie 内容,如果 cookie 域不匹配(例如 - 但大多数情况下就是这样),你的浏览器不会为您的网站存储此 cookie。

如果您的浏览器忽略 cookie,则您请求的下一页 PHP 会构建另一个会话,等等。

如果您无法让 cookie 系统正常工作,您可以跟踪 PHP 中跟踪会话 ID 的旧方法,该方法使用永久 get 和/或 post参数与 PHPSESSID。
您需要在 PHP 编译中使用 --enable-rans-id (在 phpinfo 页面上检查) http://www.php.net/manual/en/session.idpassing.php

For your sessions which are re-created at each request you should try to debug the cookies send to you by the server.
In theses cookies check that the cookie domain is the right one (and check cookie path as well).

To debug cookies you could use the web developer toolbar to show which cookies are active after the page load. But the best tool is Live Http headers, check the real headers send by the server, there you'll find the real cookie content, and if there's a mismatch in the cookie domain (for example -- but most time it's that) your browser will not store this cookie for your website.

If your browser ignore a cookie, next page you request PHP builds another session, etc.

If you cannot get you cooki system to work you can track the old way of tracking sessions Id in PHP, which was using a permanent get and/or post parameter with the PHPSESSID.
You need --enable-rans-id on your PHP compilation (check it on a phpinfo page) http://www.php.net/manual/en/session.idpassing.php

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