Search code examples
phpcodeigniter-3

How to Generate Random Password in Codeigniter for forgot password


Hello I am working with Codeigniter and I want to generate the random password.

I developed signup functionality using

crypt($password) 

login functionality check password following.

if (crypt($password, $hashed_password) === $hashed_password)
{
    return $query->result();
}

I am developing Forgot functionality.

How to send random generated password to user mail When I am using crypt() login time.


Solution

  • You can try below function to generate random password.

    function random_password() 
    {
        $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
        $password = array(); 
        $alpha_length = strlen($alphabet) - 1; 
        for ($i = 0; $i < 8; $i++) 
        {
            $n = rand(0, $alpha_length);
            $password[] = $alphabet[$n];
        }
        return implode($password); 
    }
    echo random_password();