easy way to generate random password
$length = 12; //password length
$password = substr(preg_replace('/\W/', '', md5(rand())), 0, $length);
Advertise your mobile site for FREE with AdTwirl
Started on the old forum in July 2010 · view the original in the archive · old links redirect here
easy way to generate random password
$length = 12; //password length
$password = substr(preg_replace('/\W/', '', md5(rand())), 0, $length);
Advertise your mobile site for FREE with AdTwirl
/* If using PHP < 4.3.2, create our own session_regenerate_id() function */
if (!function_exists('session_regenerate_id')) {
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float)$sec + ((float)$usec * 100000);
}
function php_combined_lcg() {
mt_srand(make_seed());
$tv = gettimeofday();
$lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
$lcg['s2'] = mt_rand();
$q = (int) ($lcg['s1'] / 53668);
$lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
if ($lcg['s1'] < 0) {
$lcg['s1'] += 2147483563;
}
$q = (int) ($lcg['s2'] / 52774);
$lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
if ($lcg['s2'] < 0) {
$lcg['s2'] += 2147483399;
}
$z = (int) ($lcg['s1'] - $lcg['s2']);
if ($z < 1) {
$z += 2147483562;
}
return $z * 4.656613e-10;
}
function session_regenerate_id() {
$tv = gettimeofday();
$buf = sprintf("%.15s%ld%ld%0.8f", $_SERVER['REMOTE_ADDR'], $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
session_id(md5($buf));
if (ini_get('session.use_cookies')) {
setcookie(session_name(), session_id(), NULL, '/');
}
return TRUE;
}
}
Added after 7 minutes:
here are some others for generting random string
function generate_password($len = 10) {
$result = '';
$alpha = 'abcdefghijklmnopqrstuvwxyz' .
'0123456789' .
'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$size = strlen($alpha) - 1;
$used = array();
while ($len--) {
$random = mt_rand(0, $size);
$char = $alpha[$random];
// No duplicate characters.
if (in_array($char, $used, true)) {
$len++;
continue;
}
$used[] = $char;
$result .= $char;
}
return $result;
}
function genGUID() {
return md5(uniqid(mt_rand(), true));
}