在 PHP 中保护登录 cookie 数据的最佳方法是什么?

发布于 2024-09-06 18:39:48 字数 104 浏览 1 评论 0 原文

我正在用 PHP 创建一个登录系统,我想知道如何最好地保护 cookie 中的用户信息字符串。我正在考虑用密钥以某种方式加密字符串?这是最好的方法吗?我对此有点陌生。

提前致谢。

I'm creating a login system in PHP, and I want to know how to best protect the user information string in my cookie. I was thinking of encrypting the string with a key somehow? Is this the best way? I'm kinda new to this.

Thanks in advance.

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

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

发布评论

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

评论(5

昨迟人 2024-09-13 18:39:48

不要在 cookie 中存储敏感信息。存储会话 ID 哈希以将登录用户与其帐户连接起来。

Don't store sensitive information in cookies. Store a session ID hash to connect the logged in user with their account.

葬﹪忆之殇 2024-09-13 18:39:48

亚伦·哈伦为您提供了正确的答案。只要将此类数据存储在会话中,基本上就不需要对其进行加密,因为该数据永远不会到达客户端/浏览器/用户,因为它都是服务器端的。当您在 PHP 上创建会话时,它会为您处理 cookie 内容,因此您不必担心这一点。大多数情况下,不需要处理cookie。在安全方面,处理 cookie 是有害的。

我见过一些草率的网站,它们实际上将用户名存储在表单的隐藏字段中,这允许任何人简单地编辑该表单的本地副本并以他们喜欢的用户身份执行操作。这似乎是一个显而易见的问题,但 cookie 也好不到哪儿去。

如果您确实认为设计自制身份验证系统是个好主意,那么您需要首先设计数据库。不要存储明文密码,而是存储散列(如 md5、sha-1 等),此时为每个密码生成盐(在散列之前附加到用户密码的随机字符串,并将该盐与密码哈希一起存储,因为稍后您将需要它 - 这可以防止字典哈希攻击,即彩虹表)。

Aaron Harun has the right answer for you. There's basically no need to encrypt such data as long as you store it in a session, because that data never reaches the client/browser/user, as it is all server-side. When you create a session on PHP, it handles the cookie stuff for you, so you don't have to worry about that. In most cases, there is no need to deal with cookies. In security, dealing with cookies is detrimental.

I've seen some sloppy sites that actually store the username in a hidden field on a form, which allows anybody to simply edit their local copy of that form and take actions as whichever user they like. This seems like an obvious problem, but cookies are no better.

If you truly think it's a good idea to design a homebrew authentication system, you need to design the database first. Don't store plaintext passwords, store a hash instead (like md5, sha-1, etc) and at that point there's no harm in generating a salt for each password (a random string that you append to the user's password before hashing it, and store that salt with the password hash because you'll need it later--this prevents dictionary hash attacks, ie rainbow tables).

愁杀 2024-09-13 18:39:48

您绝对不应该将安全信息存储在 cookie 中。 Cookie 以文本格式保存在用户计算机上,有很多原因说明您永远不应该在其中存储敏感信息:

  1. Cookie 基本上是文本文件,计算机上的任何人都可以使用任何文本编辑器打开它们。
  2. cookie存储在用户计算机上,这意味着他没有时间限制,没有连接限制,没有处理限制,因此他可以尝试暴力破解任何数据,而不必担心IP被禁止/踢掉。 。

您应该只存储需要记住的用户名或会话 ID 之类的内容

You should never store secure information in a cookie. Cookies are saved in textformat on the user computer, and there are many reason why you should never stock sensitive informations in them :

  1. Cookies are basically text files, which can be opened by anyone on the computer, with any text editor.
  2. The cookies are stored on the user computer, this mean he have no time limit, no connection limit, no processing limit, so he can try to brute force any data as much as he want without being worried of getting ip banned/kicked...

You should only stock things like a username to remember or a session id.

柠檬心 2024-09-13 18:39:48

如果您绝对必须将信息存储在 cookie 而不是用户会话中,请考虑使用 hash_hmac 函数是现代 PHP 版本中的内置函数。

如果您要存储“记住我”功能的用户登录信息,请存储用户 ID 和仅在数据库中可用的信息哈希。例如,将登录名和密码散列在一起并将其与用户 ID 一起存储在 cookie 中是相当安全的。如果用户更改了密码,他使用该方法登录的所有计算机都将失效,并且无法简单地更改 cookie 中的 ID 并仍然登录,因为用户名/密码哈希不匹配。

If you absolutely MUST store information in a cookie instead of the user's session, consider signing it with HMAC. The hash_hmac function is a builtin in modern PHP versions.

If you're storing a user login for a "remember me" feature, store both the user's ID and a hash of information that is only available in your database. For example, hashing together the login name and password and storing that with the user's ID in the cookie is reasonably secure. If the user ever changes their password, all the machines he's logged in to with that method would be invalidated, and there's no way to simply change the ID in the cookie and still get logged in, because the username/password hash won't match.

大海や 2024-09-13 18:39:48

您可以免费获得课程!这是存储在服务器端的数据,由 PHP/您选择的框架自动处理。您只需将数据放入会话中,该会话与存储在客户端会话中的随机 UID 相关联。在客户端,这是会话 cookie。此 ID 是自动生成的,您可以手动细粒度

客户端存储的数据永远不安全,没有真正的加密可用。无论如何,您都需要会话来跟踪登录的用户。如果您有大量数据,您可以使用 ID 来识别来自其​​他数据存储(DB、XML 等)的关联数据。

You get sessions for free! That is data stored server side, automatically handled by PHP/framework-of-your-choice. You just put data into the session, which is associated with a random UID stored in clients' sessions. On the clients' side, this is the session cookie. This ID is automatically generated, you can fine grain the behavior manually.

Data stored client side is never safe, no real encryption available. Sessions you will need anyhow for keep track of logged in users. If you have lots of data, you can use the ID to identify associated data from other datastores (DB, XML etc.)

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