Hướng dẫn php password match

You get the weird output, because of two errors you did in your code.

1. You used password_hash() and pasword_verify() not in the correct way

To verify if you have the same password, you take the password from the $_POST and hash it via $passW = password_hash($_POST['pass'], PASSWORD_DEFAULT);. You then check if this password is in your file with the command password_verify($passW, $parts[1]). The correct way anyway is, that you use password_hash() when you save the password and only then. If you want to check if the password was saved you use password_verify() with the unhashed password:

$passW = $_POST['pass'];
....
if ($userN==$parts[0] && password_verify($passW, $parts[1])) {
...

Like this the check will work.

2. You echo the output of your login for each iteration in your loop

You get the text "Wrong username/password!" three times on each login try. This is because the line echo "Wrong username/password!"; is inside of your foreach loop and your file contains three lines. Instead, you should not use the else block of your condition to handle the output of a wrong login attempt, because you leave the page in case of a successful login anway. So you can safely add a die() after the redirection to stop further code execution and print out the error message in any other way. Because the check $userN === $parts[0] is also part of that check it was executed for each row anyway, which resulted in a wrong output.

foreach($userList as $row) { 
    $parts = explode(";",$row);
    if($userN === $parts[0] && password_verify($passW, $parts[1])) {
        $_SESSION['username'] = $_POST['namn'];
        header("Location:index.php");
        die();
    }
}
echo "Wrong username/password!";

Because of the die() command the error message will only be printed if the login was not successful. Because it is outside of the foreach loop it will be printed only once.

Cập nhật ngày 27/12/2021

Trong PHP, muốn sử dụng mã hoá Bcrypt ta sẽ dùng hàm password_hash(). Hàm này thường dùng để mã hoá mật khẩu.

Ngoài mã hoá Bcrypt hàm này còn hỗ trợ mã hoá Argon2i và Argon2id.

password_hash ( string $password , int $algo [, array $options ] ) : string

Trong đó:

  • $password: chuỗi cần mã hoá
  • $algo: Phương thức mã hoá (mặc định là Bcrypt). (xem thêm)
  • $options: mảng tùy chọn (xem thêm)

Kết quả:

Trả về chuỗi mã hoá hoặc FALSE nếu thất bại.

Ví dụ:

12, ]; echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);

So sánh 2 chuỗi đã mã hoá?

Đồi khi ta buồn ta chả biết làm gì rồi ngồi vu vơ nghĩ về Bcrypt, khi mà cùng 1 chuỗi nó mã hoá ra nhiều chuỗi mới khác nhau thì làm sao so sánh?

Nhưng không sao, PHP đã cung cấp cho ta 1 hàm giúp làm việc này đó là password_verify().

password_verify ( string $password , string $hash ) : bool

Trong đó:

  • $password: chuỗi gốc cần so sánh
  • $hash: chuỗi đã má hoá

Kết quả:

TRUE nếu khớp, FALSE nếu không khớp.

Ví dụ:

Hướng dẫn php password match

Hướng dẫn php bcrypt online

Cập nhật ngày 27/12/2021Nội dung chínhSo sánh 2 chuỗi đã mã hoá?Face It, Cryptography is hard.PHP 5.5 API - (Available for 5.3.7+)ZendCryptPasswordBcrypt (5.3.2+)PasswordLibAbout ...

Hướng dẫn strtotime php

Cú phápCú pháp của hàm strtotime() trong PHP như sau:Nội dung chínhĐịnh nghĩa và cách sử dụngTrả về giá trị1. Thiết lập time_zone tại Việt Nam2. Định dạng ...

Hướng dẫn python dunder function

Trang chủ Lập trình Python Phương thức Magic hoặc Dunder trong PythonTrong hướng dẫn này chúng ta sẽ đi tìm hiểu về phương thức Magic và Nạp chồng toán tử ...

What is string and its types in php?

PHP StringPHP string is a sequence of characters i.e., used to store and manipulate text. PHP supports only 256-character set and so that it does not offer native Unicode support. There are 4 ways to ...

Hướng dẫn dùng comparing string trong PHP

Sử dụng toán tử so sánhCách đơn giản và thô sơ nhất là dùng toán tử so sánh của PHP (comparison operators). Tuy nhiên không nên dùng toán tử Equal (==) mà nên dùng ...

Hướng dẫn dùng no comparison trong PHP

Cách đây 4 tháng, mình đã từng có một bài viết mang tên Variables Comparison in Javascript, bài viết phân tích về tính falsy và truthy trong Javascript, cũng như những ...

Hướng dẫn php argon2

2017-08-17 | By: Enrico ZimuelPHP 7.2 will be released later this year (2017). This version contains some interesting additions, including two new security features: support of the Argon2 password ...

Hướng dẫn python compare dunder

Conservative Python 3 Porting GuidePython 3 is strict when comparing objects of disparate types. It also drops cmp-based comparison and sorting in favor of rich comparisons and key-based sorting, ...

Hướng dẫn dùng python comparable python

For a full set of comparison functions I have used the following mixin, which you could put in say for example a mixin.py in your module.class ComparableMixin(object): def _compare(self, other, ...

Hướng dẫn bcryptjs php

Cập nhật ngày 27/12/2021Nội dung chínhSo sánh 2 chuỗi đã mã hoá?Face It, Cryptography is hard.PHP 5.5 API - (Available for 5.3.7+)ZendCryptPasswordBcrypt (5.3.2+)PasswordLibAbout ...

Hướng dẫn dùng bycrypt trong PHP

Cập nhật ngày 27/12/2021Trong PHP, muốn sử dụng mã hoá Bcrypt ta sẽ dùng hàm password_hash(). Hàm này thường dùng để mã hoá mật khẩu.Ngoài mã hoá Bcrypt hàm này ...

Phá khóa sheet trong excel bằng marcro

Phá pass Excel hay gỡ bỏ mật khẩu của Sheet không phải là chuyện đơn giản hay dễ làm bởi nó là minh chứng cho độ bảo mật Excel của chính Microsoft với Excel ...

Hướng dẫn bcrypt in php

Cập nhật ngày 27/12/2021Trong PHP, muốn sử dụng mã hoá Bcrypt ta sẽ dùng hàm password_hash(). Hàm này thường dùng để mã hoá mật khẩu.Ngoài mã hoá Bcrypt hàm này ...

Hướng dẫn dateinterval to timestamp php

What is the best way to calculate the total number of seconds between two dates? So far, Ive tried something along the lines of: $delta = $date->diff(new DateTime(now)); $seconds = ...

Hướng dẫn compare string php

Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related ...

Hướng dẫn one-way hash php

Hướng dẫn sắp xếp trong phpVietnamese (Tiếng Việt) translation by Thai An (you can also view the original English article) Việc lấy thông tin từ một mảng thông tin đã ...

Hướng dẫn dùng ressource trong PHP

So, you want to use bcrypt? Awesome! However, like other areas of cryptography, you shouldnt be doing it yourself. If you need to worry about anything like managing keys, or storing salts or ...

Hướng dẫn dùng hashing means trong PHP

Tìm hiểu khi sử dụng các hàm băm tạo dữ liệu lưu trữ passwordKhi lưu trữ password vào CSDL thường sẽ sử dụng các hàm băm khác nhau được hỗ trợ bởi hệ ...

Hướng dẫn password_bcrypt in php

(PHP 5 >= 5.5.0, PHP 7, PHP 8)password_hash — Creates a password hashDescriptionpassword_hash(string $password, string|int|null $algo, array $options = []): string The following algorithms are ...

Hướng dẫn dùng resource-pack-sha1 trong PHP

So, you want to use bcrypt? Awesome! However, like other areas of cryptography, you shouldnt be doing it yourself. If you need to worry about anything like managing keys, or storing salts or ...

Hướng dẫn dùng bcrypt compare trong PHP

Cập nhật ngày 27/12/2021Trong PHP, muốn sử dụng mã hoá Bcrypt ta sẽ dùng hàm password_hash(). Hàm này thường dùng để mã hoá mật khẩu.Ngoài mã hoá Bcrypt hàm này ...