在 PHP 中安全地保存会话数据

发布于 2024-09-09 22:13:45 字数 88 浏览 3 评论 0原文

我试图了解会话在 PHP 中的工作原理,发现会话数据默认存储在文件系统中。在共享托管环境中,任何用户编写的 PHP 脚本都可以读取会话数据。如何防止这种情况发生?

I was trying to understand how sessions work in PHP and found that session data is by default stored in the file system. In a shared hosting environment, session data can be read by PHP scripts written by any user. How can this be prevented ?

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

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

发布评论

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

评论(5

洒一地阳光 2024-09-16 22:13:45

您可以覆盖脚本的会话保存处理程序以使用文件系统以外的其他内容,例如数据库或内存缓存。这是详细的实现:http://phpsec.org/projects/guide/5.html

You can override the session save handler for your script to use something other than the filesystem, such as a database or memcache. Here is a detailed implementation: http://phpsec.org/projects/guide/5.html

药祭#氼 2024-09-16 22:13:45

取决于您对 php.ini 文件的访问级别 - 如果您位于运行 suPHP 的共享主机环境中并允许您拥有自己的 php.ini 文件(例如),那么您只需设置会话即可。 save_path 到类似 ~/tmp 的路径,而不是通常共享的 /tmp 。

首先,我不认为您实际上可以从其他应用程序读取 php 会话数据。我相信这对于观看它的人来说是相当独特的。

最后php的Session数据不只是文件系统保存而已。还可以将其设置为保存在用户计算机上的 cookie 中,或者您可以将 php 会话数据设置为存储在数据库中。

Depends on the level of access you have to the php.ini file - if you're on a Shared Hosting environment which runs suPHP and allows you to have your own php.ini file (for instance) then you can simply set the session.save_path to a path like ~/tmp instead of /tmp which is usually shared.

To begin with though, I don't think that you actually CAN read php session data from other applications. I believe it's something rather unique to the person viewing it.

Finally php Session data is not solely file system saved only. It can also be setup to save in a cookie on the user's machine or you can setup php session data to be stored in a database.

忱杏 2024-09-16 22:13:45

编写您自己的 SESSION 包装器。

例如,CodeIgniter 的 会话库 不依赖于 PHP 的本机库,而且更安全:

注意:Session 类不使用本机 PHP 会话。它生成自己的会话数据,为开发人员提供更大的灵活性。

Write your own SESSION wrapper.

For example CodeIgniter's session library doe's not depend on PHP's native one and it's more secure:

Note: The Session class does not utilize native PHP sessions. It generates its own session data, offering more flexibility for developers.

誰ツ都不明白 2024-09-16 22:13:45

您可以使用 session_save_path() 更改会话数据目录至未共享的一个。

You can use session_save_path() to change the session data directory to one that isn't shared.

如果没结果 2024-09-16 22:13:45

使用 session_save_path() 并更改会话文件夹,例如“/htdocs/storage/sessions”。现在会话仅保存到您给定的路径。

Use session_save_path() and change your session folder like "/htdocs/storage/sessions". Now sessions only saved to your given path.

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