php generate secure password code example

Example 1: auto generate password in php

function rand_string( $length ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return substr(str_shuffle($chars),0,$length);
}

echo rand_string(8);

Example 2: php secure password hash

$password = 'mypassword1';
$passwordHash = password_hash($password, PASSWORD_DEFAULT);//hash the password

echo password_verify('mypassword1', $passwordHash);//true, Yeah we match
echo password_verify('wrongpassword', $passwordHash); //false ,Ooops wrong password

Tags:

Php Example