Jump to content


Photo

Random Password


  • Please log in to reply
6 replies to this topic

#1 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 03 October 2006 - 03:34 PM

Hi is this the best function for a Random Password or is there a better one...

function makeRandomPassword()
{

$salt = "abchefghjkmnpqrstuvwxyz0123456789";

srand((double)microtime()*1000000); 

$i = 0;

while ($i <= 7)
{
 
$num = rand() % 33;

$tmp = substr($salt, $num, 1);

$pass = $pass . $tmp;

$i++;

}

$pass = md5($pass);
     
return $pass;
     
}

#2 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 03 October 2006 - 03:42 PM

If you want security (md5) you could also add the CAPITAL LETTERS.

#3 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 03 October 2006 - 03:43 PM

wat in the salt bit...?

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 03 October 2006 - 04:24 PM

yes in the $salt bit

 $salt = "abchefghjkmnpqrstuvwxyzABCHEFGHJKMNPQRSTUVWXYZ0123456789";


and change the first $num line to
$num = rand() % 56;


you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 03 October 2006 - 06:24 PM

and change the first $num line to

$num = rand() % 56;


What would that help?

#6 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 03 October 2006 - 06:49 PM

sorry that was me misunderstanding the function, i thought it was the maximum number required whilst working over $salt, which is about 33 characters long, and putting the extra capitals in makes it 54....having checked it doesnt matter.
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 03 October 2006 - 06:52 PM

An easy version could be:
<?php
function random_password($length=8)
{
	return substr(md5(microtime()),0,$length);
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users