PHP
·
发表于 5年以前
·
阅读量:8301
ʹÓÃPHP¿ª¢Ó¦ÓóÌÐo£¬ÓÈÆaÊÇÍøÕ¾³ÌÐo£¬³£³£ÐeÒªÉu³ÉËae»uÃÜÂe£¬ÈçÓû§×¢²aÉu³ÉËae»uÃÜÂe£¬Óû§ÖØÖÃÃÜÂeÒ²ÐeÒªÉu³ÉÒ»¸oËae»uµÄÃÜÂe¡£Ëae»uÃÜÂeÒ²¾ÍÊÇÒ»´®¹Ì¶¨³¤¶ÈµÄ×Öu´®£¬ÕaÀiÎÒÊÕ¼¯ÕuÀiÁ˼¸ÖÖÉu³ÉËae»u×Öu´®µÄ½*¨£¬ÒÔ¹(C)´o¼Ò²Î¿¼¡£
½¨Ò»£º
1¡¢ÔÚ 33 ¨C 126 ÖÐÉu³ÉÒ»¸oËae»uÕuÊý£¬Èç 35£¬
2¡¢½« 35 ת»»³É¶ÔÓ¦µÄASCIIÂe×Ö*u£¬Èç 35 ¶ÔÓ¦ #
3¡¢Öظ´ÒÔÉÏ 1¡¢2 ²½Öe n ´Î£¬Á¬½Ó³É n λµÄÃÜÂe
¸ÃËa*¨Ö÷ÒªÓõ½ÁËÁ½¸oº¯Êý£¬mt_rand ( int $min , int $max )º¯ÊýÓÃÓÚÉu³ÉËae»uÕuÊý£¬ÆaÖÐ $min ¨C $max Ϊ ASCII ÂeµÄ*¶Î§£¬ÕaÀiÈ¡ 33 -126 £¬¿ÉÒÔ¸u¾ÝÐeÒªµ÷Õu*¶Î§£¬ÈçASCIIÂe±iÖÐ 97 ¨C 122 λ¶ÔÓ¦ a ¨C z µÄÓ¢ÎÄ×Öĸ£¬¾ßÌa¿É²Î¿¼ ASCIIÂe±i£» chr ( int $ascii )º¯ÊýÓÃÓÚ½«¶ÔÓ¦ÕuÊý $ascii ת»»³É¶ÔÓ¦µÄ×Ö*u¡£
function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
// µ÷Óøú¯Êý£¬´«µÝ³¤¶È²ÎÊý$pw_length = 6
echo create_password(6);
½¨¶þ£º
1¡¢Ô¤ÖÃÒ»¸oµÄ×Ö*u´® $chars £¬°uÀ¨ a ¨C z£¬A ¨C Z£¬0 ¨C 9£¬ÒÔ¼°Ò»Ð(C)ÌØÊa×Ö*u
2¡¢ÔÚ $chars ×Ö*u´®ÖÐËae»uÈ¡Ò»¸o×Ö*u
3¡¢Öظ´µÚ¶þ²½ n ´Î£¬¿ÉµÃ³¤¶ÈΪ n µÄÃÜÂe
function generate_password( $length = 8 ) {
// ÃÜÂe×Ö*u¼¯£¬¿ÉÈÎÒaÌi¼ÓÄaÐeÒªµÄ×Ö*u
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
// ÕaÀiÌa¹(C)Á½ÖÖ×Ö*u»ñÈ¡*½Ê½
// µÚÒ»ÖÖÊÇʹÓà substr ½ØÈ¡$charsÖеÄÈÎÒaһλ×Ö*u£»
// µÚ¶þÖÖÊÇÈ¡×Ö*uÊý×e $chars µÄÈÎÒaÔªËØ
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
½¨Èý£º
1¡¢Ô¤ÖÃÒ»¸oµÄ×Ö*uÊý×e $chars £¬°uÀ¨ a ¨C z£¬A ¨C Z£¬0 ¨C 9£¬ÒÔ¼°Ò»Ð(C)ÌØÊa×Ö*u
2¡¢Í¨¹ýarray_rand()´ÓÊý×e $chars ÖÐËae»uÑ¡³o $length ¸oÔªËØ
3¡¢¸u¾ÝÒÑ»ñÈ¡µÄ¼uÃuÊý×e $keys£¬´ÓÊý×e $chars È¡³o×Ö*uÆ´½Ó×Ö*u´®¡£¸Ã*½*¨µÄȱµaÊÇÏaͬµÄ×Ö*u²»»aÖظ´È¡¡£
function make_password( $length = 8 )
{
// ÃÜÂe×Ö*u¼¯£¬¿ÉÈÎÒaÌi¼ÓÄaÐeÒªµÄ×Ö*u
$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!',
'@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_',
'[', ']', '{', '}', '<', '>', '~', '`', '+', '=', ',',
'.', ';', ':', '/', '?', '|');
// ÔÚ $chars ÖÐËae»uÈ¡ $length ¸oÊý×eÔªËؼuÃu
$keys = array_rand($chars, $length);
$password = '';
for($i = 0; $i < $length; $i++)
{
// ½« $length ¸oÊý×eÔªËØÁ¬½Ó³É×Ö*u´®
$password .= $chars[$keys[$i]];
}
return $password;
}
½¨ËÄ£º
±¾½¨ÊDZ¾Îı»À¶É«ÀiÏeתÔغo£¬Ò»ÃuÍøÓÑÌa¹(C)µÄÒ»¸oн¨£¬Ëa¨¼oµ¥£¬´uÂe¼o¶Ì£¬Ö»ÊÇÒoΪmd5()º¯ÊýµÄµ»ØÖµµÄÔµ¹Ê£¬Éu³ÉµÄÃÜÂeÖ»°uÀ¨×ÖĸºÍÊý×Ö£¬²»¹ýÒ²ËaÊÇÒ»¸o²»´iµÄ½¨¡£Ëa*¨Ë¼Ïe£º
1¡¢time() »ñÈ¡µ±Ç°µÄ Unix ʱ¼a´Á
2¡¢½«µÚÒ»²½»ñÈ¡µÄʱ¼a´Á½øÐÐ md5() ¼ÓÃÜ
3¡¢½«µÚ¶þ²½¼ÓÃܵĽa¹u£¬½ØÈ¡ n λ¼´µÃÏeÒªµÄÃÜÂe
function get_password( $length = 8 )
{
$str = substr(md5(time()), 0, 6);
return $str;
}
ʱ¼aЧÂʶԱÈ
ÎÒÃÇʹÓÃÒÔÏÂPHP´uÂe£¬¼ÆËaÉÏÃaeµÄ 4 ¸oËae»uÃÜÂeÉu³Éº¯ÊýÉu³É 6 λÃÜÂeµÄÔËÐÐʱ¼a£¬½ø¶ø¶ÔËuÃǵÄʱ¼aЧÂʽøÐÐÒ»¸o¼oµ¥µÄ¶Ô±È¡£
<?php
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
// ¼Ç¼¿ªÊ¼Ê±¼a
$time_start = getmicrotime();
// ÕaÀi*ÅÒªÖ´ÐеÄPHP´uÂe£¬Èç:
// echo create_password(6);
// ¼Ç¼½aÊøʱ¼a
$time_end = getmicrotime();
$time = $time_end - $time_start;
// Êa³oÔËÐÐ×Üʱ¼a
echo "Ö´ÐÐʱ¼a $time seconds";
?>
½¨Îa£º
function rand_string($len = 16, $keyword = '') {
if (strlen($keyword) > $len) {//¹Ø¼u×Ö²»ÄܱÈ×ܳ¤¶È³¤
return false;
}
$str = '';
$chars = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHIJKMNPQRSTUVWXYZ'; //È¥µo1¸u×Öĸl*À»iÏý
if ($len > strlen($chars)) {//λÊý¹ý³¤Öظ´×Ö*u´®Ò»¶¨´ÎÊý
$chars = str_repeat($chars, ceil($len / strlen($chars)));
}
$chars = str_shuffle($chars); //´oÂÒ×Ö*u´®
$str = substr($chars, 0, $len);
if (!empty($keyword)) {
$start = $len - strlen($keyword);
$str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //´ÓËae»uλÖòaÈe¹Ø¼u×Ö
}
return $str;
}
echo rand_string(16,"ab"); //output example:V8bNY6SmkeywordB
?>
×iÖյóoµÄ½a¹uÊÇ£º
*½*¨Ò»£º9.8943710327148E-5 Ãe
*½*¨¶þ£º9.6797943115234E-5 Ãe
*½*¨Èý£º0.00017499923706055 Ãe
*½*¨ËÄ£º3.4093856811523E-5 Ãe
¿ÉÒÔ¿´³o*½*¨Ò»ºÍ*½*¨¶þµÄÖ´ÐÐʱ¼a¶¼²i²»¶a£¬*½*¨ËÄÔËÐÐʱ¼a×i¶Ì£¬¶ø*½*¨ÈýµÄÔËÐÐʱ¼aÉÔ΢³¤µa¡£