Hey everyone,
Still working on my site with the login system, and yesterday I’ve built a custom piece of code that generates a resetcode. This code is encrypted in the database, and different for each user. When a user asks to recover their password, this code is sent to the user through e-mail.
The code that I’ve written is a bit long though, because I couldn’t find a better/faster way to do it. I’ve written 26 possibilities for the letters a-z, while 0-9 only takes 1 line…
$resetcode = "";
for($i=1; $i<=rand(7,11); $i++)
{
if(rand(0,1))
{
$char = rand(0,9);
}
else
{
$r = rand(1,26);
switch($r)
{
case 1:
$char = "a";
break;
case 2:
$char = "b";
break;
case 3:
$char = "c";
break;
case 4:
$char = "d";
break;
case 5:
$char = "e";
break;
case 6:
$char = "f";
break;
case 7:
$char = "g";
break;
case 8:
$char = "h";
break;
case 9:
$char = "i";
break;
case 10:
$char = "j";
break;
case 11:
$char = "k";
break;
case 12:
$char = "l";
break;
case 13:
$char = "m";
break;
case 14:
$char = "n";
break;
case 15:
$char = "o";
break;
case 16:
$char = "p";
break;
case 17:
$char = "q";
break;
case 18:
$char = "r";
break;
case 19:
$char = "s";
break;
case 20:
$char = "t";
break;
case 21:
$char = "u";
break;
case 22:
$char = "v";
break;
case 23:
$char = "w";
break;
case 24:
$char = "x";
break;
case 25:
$char = "y";
break;
case 26:
$char = "z";
break;
}
if(rand(0,1))
{
$char = strtoupper($char);
}
}
$resetcode .= $char;
}
I think this is a pretty safe way to come up with such security codes, right? If anyone knows a better or faster way, please help me out Or if you find any possible security flaws…