简体   繁体   English

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

[英]Saving session data securely in PHP

I was trying to understand how sessions work in PHP and found that session data is by default stored in the file system. 我试图理解会话如何在PHP中工作,并发现会话数据默认存储在文件系统中。 In a shared hosting environment, session data can be read by PHP scripts written by any user. 在共享托管环境中,会话数据可以由任何用户编写的PHP脚本读取。 How can this be prevented ? 如何防止这种情况?

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 以下是详细的实施: http//phpsec.org/projects/guide/5.html

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

To begin with though, I don't think that you actually CAN read php session data from other applications. 首先,我不认为你实际上可以从其他应用程序读取php会话数据。 I believe it's something rather unique to the person viewing it. 我相信这对于观看它的人来说是相当独特的。

Finally php Session data is not solely file system saved only. 最后,php Session数据不仅仅是文件系统保存。 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. 它也可以设置为保存在用户机器上的cookie中,或者您可以设置php会话数据以存储在数据库中。

Write your own SESSION wrapper. 编写自己的SESSION包装器。

For example CodeIgniter's session library doe's not depend on PHP's native one and it's more secure: 例如CodeIgniter的会话库 doe不依赖于PHP的本地会话库 ,它更安全:

Note: The Session class does not utilize native PHP sessions. 注意:Session类不使用本机PHP会话。 It generates its own session data, offering more flexibility for developers. 它生成自己的会话数据,为开发人员提供更多灵活性。

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

Use session_save_path() and change your session folder like "/htdocs/storage/sessions". 使用session_save_path()并将会话文件夹更改为“/ htdocs / storage / sessions”。 Now sessions only saved to your given path. 现在会话只保存到您指定的路径。

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

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