使用php创建一个汉字密码系统

发布于 2024-08-29 01:08:13 字数 467 浏览 6 评论 0原文

我在验证中文字符与其他中文字符时遇到问题,例如,我正在创建一个简单的密码脚本,该脚本从数据库获取数据,并通过 get 获取用户输入。

我遇到的问题是由于某种原因,即使当您回显字符时它们看起来完全相同,我的 if 语句仍然认为它们是不同的。

我尝试使用 htmlentities() 函数对字符进行编码,数据库中的密码编码得很好,给了我一个有效的 '& #35441;' (我在其中添加了一个空格以阻止其转换为汉字!)。

其他用户输入值给了我一堆有趣​​的字符。我认为唯一破坏它的是它以不同的方式编码,因此 php 认为它是两个完全不同的字符串。

有人有什么想法吗?
预先感谢,

编辑:
感谢您的快速回复,我将考虑将数据库编码设置为 UTF-8,但是目前,数据库的结果不是问题,它们正在使用 htmlentities 正确编码,这是我得到的结果$_GET 导致了问题。

干杯,
将要

I'm having an issue with validating chinese characters against other chinese characters, for example I'm creating a simple password script which gets data from a database, and gets the user input through get.

The issue I'm having is for some reason, even though the characters look exactly the same when you echo them out, my if statement still thinks they are different.

I have tried using the htmlentities() function to encode the characters, the password from the database encodes nicely, giving me a working '& #35441;' (I've put a space in it to stop it from converting to a chinese character!).

The other user input value gives me a load of funny characters. The only thing which I believe must be breaking it, is it encodes in a different way and therefore the php thinks it's 2 completely different strings.

Does anybody have any ideas?
Thanks in advance,
Will

Edit:
Thanks for the quick responses guys, I'm gonna look around setting the database encoding to UTF-8, however at the moment, the results from the database are not the problem, they are encoding correctly using htmlentities, it's the results I get from $_GET which is causing the problems.

Cheers,
Will

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

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

发布评论

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

评论(4

摘星┃星的人 2024-09-05 01:08:14

由于无论如何您都应该存储密码的哈希值而不是密码本身,因此这可能是解决方案的一部分。您存储的是哈希值而不是密码,因此数据库不会出现问题。

也就是说,不同的浏览器对它们提交的字符串进行编码的方式可能会有所不同。这不是我很感兴趣的事情,但你最好确保找到一个在所有浏览器上生成完全相同的字符串的解决方案。将accept-charset 设置为utf-8 很简单,您可能还想弄乱enctype。

Since you anyway ought to store hashes of passwords rather than the passwords themselves, this might be a part of the solution. You store the hash rather than the password and thus have no problems with the database.

That said, there might be differences to how different browsers encode the strings they submit. It's not something I'm very much into, but you better make sure that you find a solution that makes the exact same string on all browsers. Setting the accept-charset to utf-8 is a nobrainer, you might also want to mess with the enctype.

岁吢 2024-09-05 01:08:13

对于密码,我的建议是不要进行直接比较,因为这意味着您以明文形式存储密码。在存储它们之前,至少要通过 MD5 或 SHA 之类的哈希(最好也带有盐值)运行它们。然后您只需比较哈希值(通常是十六进制值),因此不会导致任何编码问题。

对于非密码值,听起来您的数据库和 PHP 不采用相同的编码,因此它们不正确匹配。如果 MySQL 按照您想要的方式存储它们,请让它进行比较(而不是让它首先返回值),这应该避免 1 次通过编码更改,这似乎可能是问题所在。

For passwords my advice is don't do a direct comparison, because that means you're storing passwords in the clear. At least run them through a hash like MD5 or SHA (preferably with a salt value as well) before storing them. Then you just have to compare the hash values, which are typically Hex values, so shouldn't cause any encoding problems.

For non-password values it sounds like your database and PHP are not on the same encoding, so they are not matching properly. If MySQL is storing them the way you want, have it do the comparison (instead of having it return the values first), that should avoid 1 of the passes through an encoding change which seems likely to be the problem.

树深时见影 2024-09-05 01:08:13

如果您想存储密码,请阅读以下内容:您需要了解的有关安全密码方案的信息

读完它后,您的根本问题似乎是您从用户收到的内容与从数据库获得的内容之间存在某种字符编码不匹配。
如果您使用Mysql和utf-8编码,您是否首先使用SET名称“utf-8”查询?

If you want to store passwords, read this : what you need to know about secure password schemes.

After reading it, your root problem seem to be some character encoding missmatch between what you receive from the user and what you get from your database.
If you are using Mysql and utf-8 encoding, do you first use the SET names "utf-8" query ?

如若梦似彩虹 2024-09-05 01:08:13

使用 SHA1 和 MD5 保存值可能会解决您的问题,正如其他人所说。这也是一个安全的过程。这是一个可以提供帮助的代码片段。

public function getHashedPassword()
{
    $salt = 'mysalt';
    return  sprintf( "%d%s",$salt,sha1( sprintf( "%d%s", $salt,$this->_rawPassword) ));
}

比较后,重新哈希输入的密码并将其与数据库中保存的哈希密码进行比较。这样做可能会消除编码问题。

Saving the values using SHA1 and MD5 may solve your problem as the other stated it. It is also a secure process. Here's a code snippet to help out.

public function getHashedPassword()
{
    $salt = 'mysalt';
    return  sprintf( "%d%s",$salt,sha1( sprintf( "%d%s", $salt,$this->_rawPassword) ));
}

Upon comparison, rehash the password input and compare it to the save hashed password in your database. Doing so may remove the encoding issue.

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