PHP Velho Oeste 2024

password_verify

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

password_verifyParola ile aş eşleşmesini doğrular

Açıklama

password_verify(string $parola, string $aş): bool

Belirtilen aş ile belirtilen parolanın eşleşmesini doğrular. password_verify() işlevi crypt() işlevi ile uyumludur. Bu bakımdan crypt() ile oluşturulan parola aşları password_verify() ile kullanılabilir.

password_hash() işlevinin döndürdüğü aşın içinde algoritma, maliyet ve tuz bulunduğuna dikkat edilmelidir. Bu bakımdan, bir aşı doğrulamak için gereken tüm bilgi içinde olur. Bu, tuz veya algoritma bilgisinin ayrı bir depoda saklanmasını gerektirmeden doğrulama işlevinin aşı doğrulamasını sağlar.

Bu işlev zamanlama saldırılarına karşı güvenlidir.

Bağımsız Değişkenler

parola

Kullanıcı parolası.

password_hash() işleviyle oluşturulan aş.

Dönen Değerler

Parola ve aş eşleşirse true, aksi takdirde false döner.

Örnekler

Örnek 1 - password_verify() örneği

Bu basit bir örnek olup gerekirse doğu parolanın yeniden aşlanması önerilir; password_needs_rehash() işlevinde bunun yapılış örneği vardır.

<?php
// Bunun nereden geldiğini öğrenmek için password_hash() örneğine bak
$aş = '$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a';

if (
password_verify('rasmuslerdorf', $aş)) {
echo
'Parola geçerli!';
} else {
echo
'Geçersiz parola.';
}
?>

Yukarıdaki örneğin çıktısı:

Parola geçerli!

Ayrıca Bakınız

add a note add a note

User Contributed Notes 5 notes

up
265
Anonymous
9 years ago
If you get incorrect false responses from password_verify when manually including the hash variable (eg. for testing) and you know it should be correct, make sure you are enclosing the hash variable in single quotes (') and not double quotes (").

PHP parses anything that starts with a $ inside double quotes as a variable:

<?php
// this will result in 'Invalid Password' as the hash is parsed into 3 variables of
// $2y, $07 and $BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq
// due to it being enclosed inside double quotes
$hash = "$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq";

// this will result in 'Password is valid' as variables are not parsed inside single quotes
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

if (
password_verify('rasmuslerdorf', $hash)) {
    echo
'Password is valid!';
} else {
    echo
'Invalid password.';
}
?>
up
50
Vasil Toshkov
10 years ago
This function can be used to verify hashes created with other functions like crypt(). For example:

<?php

$hash
= '$1$toHVx1uW$KIvW9yGZZSU/1YOidHeqJ/';

if (
password_verify('rasmuslerdorf', $hash)) {
    echo
'Password is valid!';
} else {
    echo
'Invalid password.';
}

// Output: Password is valid!

?>
up
25
chris at weeone dot de
8 years ago
The function password_verify() uses constant time. This makes it safe against timing attacks. Don't use crypt($password_database) === crypt($password_given_by_login), since there is no protection against timing attacks!

If you don't want to use password_verify(), then have a look at hash_equals(), which also runs a timing attack safe string comparison.
up
18
omidbahrami1990 at gmail dot com
6 years ago
This Is The Most Secure Way To Keep Your Password Safe With PHP 7 ,
Even When Your DataBase Has Been Hacked ,
It Will Be Almost Impossible To Retrieve Your Password .
--------------------------------------------------------
--- When A User Wants To Sign Up ---
1 ---> Get Input From User Which Is The User`s Password
1 ---> Hash The Password
2 ---> Store The Hashed Password In Your DataBase
--------------------------------------------------------
<?php
$hashed_password
= password_hash($_POST["password"],PASSWORD_DEFAULT);

// $_POST["password"] ---> Is The User`s Input
// $hashed_password ---> Is The Hashed Password You Can Store In Your DataBase
?>
--------------------------------------------------------
--- When A User Wants To Sign In ---
1 ---> Get Input From User Which Is The User`s Password
2 ---> Fetch The Hashed Password From Your Database
3 ---> Compare The User`s Input And The Hashed Password
--------------------------------------------------------
<?php
   
if(password_verify($_POST["password"],$hashed_password))
    echo
"Welcome";

    else
    echo
"Wrong Password";

// $_POST["password"] ---> Is The User`s Input
// $hashed_password ---> Is The Hashed Password You Have Fetched From DataBase
?>
up
-56
suit at rebell dot at
10 years ago
As Vasil Toshkov stated, password_verify() can be used to verify a password created by crypt() or password_hash()

That is because passwords created by password_hash() also use the C crypt sheme

If you want to verify older plain MD5-Hashes you just need to prefix them with $1$

See https://en.wikipedia.org/wiki/Crypt_(C) for more information.
To Top