Random Password generator with PHP

password

Sometimes we need to create a random password generator in our web application. This random password will be given to the user. This is a nice idea to force users to use a secure password. This is a working random password generation function for PHP. Just include this function anywhere in your code and then use it.

In this code, I have removed the l and 1 because it can create confusion in passwords. It means, generated password will not contain 1 and l both. If you want to use, you can add it in the code. This code will generate a random password with mixture of characters, numbers and special characters.

You can also see the demo of this password generator function from the link below.

Demo

Random Password generator with PHP

<?php

function createRandomPassword()
{
$chars = "[email protected]#$%&*()?~abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ023456789";
$len=strlen($chars);
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 10)
{
$num = rand(0,$len);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

$newpassword = createRandomPassword();
echo "Your random password is: $newpassword";
?>

You can also improve this function to make it better. If you have better code, you can share it with us and I will be happy to share that code with our readers along with giving you full credits for that code.

If you have any kind of problem in implementing this code, comment below to ask your queries.

Advertisement

Tags:

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker