在 PHP 中安全地保存会话数据
我试图了解会话在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以覆盖脚本的会话保存处理程序以使用文件系统以外的其他内容,例如数据库或内存缓存。这是详细的实现: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
取决于您对 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.
编写您自己的 SESSION 包装器。
例如,CodeIgniter 的 会话库 不依赖于 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:
您可以使用 session_save_path() 更改会话数据目录至未共享的一个。
You can use session_save_path() to change the session data directory to one that isn't shared.
使用 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.