PHP Store user password new Play program code

Source: Internet
Author: User
Tags hash md5

Believe that many people are either MD5 () directly into the library or store the MD5 () and salt two fields. But the first kind is unsafe. The second kind of trouble. Fortunately, PHP offers a simpler solution.

Note: The following prerequisites are in the php5.5 version of the

After the php5.5, you can change one way of saving. Simple and convenient.

<?php
$pwd = "123456";
$hash = Password_hash ($pwd, Password_bcrypt);
Echo $hash;

MySQL only needs to store this hash value on the line. It is recommended that you set the database value of this field to 255

if (Password_verify ($pwd, ' hash value in database ')} {
echo "Correct password";
} else {
echo "Password Error";
}
See at this time, there may be some people say TM before I save the password is only a separate MD5 password, also did not use salt, now I want to change to you this, how to do well.

Refer to this article in StackOverflow, which may be helpful to you.

Http://stackoverflow.com/questions/18906660/converting-md5-password-hashes-to-php-5-5-password-hash

$password = $_post["password"];

TODO here to determine if the password is the correct password
And then decide to update

if (substr ($pwInDatabase, 0, 1) = = "$")
{
Password already converted, verify using password_verify
}
Else
{
User still using the old MD5, update it!

if (MD5 ($password) = = $pwInDatabase)
{
$db->STOREPW (Password_hash ($password));
}
}


This practice is not recommended because double hashes do not increase security.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.