【问题标题】:PHP hash() function returns same value for different inputPHP hash() 函数为不同的输入返回相同的值
【发布时间】:2014-01-21 21:52:14
【问题描述】:

我正在为我的网站设置用户注册。我有这样的表格

<form action="registrate-user.php" method="POST"> <br/>
    Username: <input type="text" id="username" name="username" value=""> <br/>
    E-mail: <input type="text" id="email" name="email" value="Secret"> <br/>
    Password: <input type="password" id="password" value=""><br/>
    <input id="registratebutton" type="submit" value="Register">
</form>

还有这样的接收器

<?php

    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    $hashedpass = hash('md5', $password);

    echo $username . " " . $email . " " . $hashedpass;

?>

但是当我输入不同的密码时会回显相同的值!这是为什么呢?

【问题讨论】:

  • 表单元素必须有一个name 属性才能从浏览器传递到网络服务器......所以你不会得到任何作为 $_POST['password']... 传递的东西。所以你总是散列相同的空值
  • 学习时的小步骤很好,但是应该单独使用md5。密码存储被认为是不安全的。只是一个快速的“仅供参考”;-)
  • 哦,好的,我明白了。将其发布为答案,我将接受它作为解决方案。 @Fred-ii- 你建议我改用哪种算法?
  • 在这里阅读 >>> php.net/password 并且始终 hash 不加密。

标签: php html forms hash registration


【解决方案1】:

$_POST 数组由具有 name 属性而不是 id 属性的表单中的所有字段填充,并且您的密码输入没有。您可以通过回显未加密的密码来确认这一点。

要解决您的问题,只需像在其他字段中一样添加 name="password"

<form action="registrate-user.php" method="POST"> <br/>
    Username: <input type="text" id="username" name="username" value=""> <br/>
    E-mail: <input type="text" id="email" name="email" value="Secret"> <br/>
    Password: <input type="password" id="password" name="password" value=""><br/>
    <input id="registratebutton" type="submit" value="Register">
</form>

另外看看PHP中新的password_hash函数,它比MD5好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2021-07-09
    相关资源
    最近更新 更多