user3195417
user3195417

Reputation: 329

PHP hash() function returns same value for different input

I'm taking my baby steps with setting up user registration for my website. I have a form like this

<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>

and a receiver like this

<?php

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

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

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

?>

But the same value is echoed when I enter different passwords! Why is this?

Upvotes: 1

Views: 981

Answers (1)

JohnKiller
JohnKiller

Reputation: 2008

The $_POST array is filled with all the fields in a form that has the name attribute, not the id, and your password input hasn't one. You can confirm this by echoing the unencrypted password.

To solve your issue, just add name="password" like you did on the other fields.

<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>

Also take a look at the new password_hash function in PHP, it's better than MD5.

Upvotes: 4

Related Questions