Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Created October 8, 2012 11:26
Show Gist options
  • Save samarpanda/3852025 to your computer and use it in GitHub Desktop.
Save samarpanda/3852025 to your computer and use it in GitHub Desktop.
Generating random password like wordpress using php
<?php
class Utility{
static $random = "";
static function generate_password($length = 12, $special_chars=true)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
if($special_chars) $chars .= '!@#$%^&*_-()';
$password = "";
for($i=0;$i<$length;$i++)
$password .= substr($chars, self::generate_random_number(0, strlen($chars)-1), 1);
return $password;
}
static function generate_random_number(){
$seed = mt_random();
if(strlen(self::$random) < 8 ) {
self::$random = md5(uniqid(microtime().mt_rand(), true).$seed);
self::$random .= sha1(self::$random);
self::$random .= sha1(self::$random.$seed);
}
$value = substr(self::$random, 0, 8);
self::$random = substr(self::$random, 8);
$value = abs(hexdec($value));
if($max != 0)
$value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
return abs(intval($value));
}
}
echo Utility::generate_password();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment