php中生成随机密码几种方法

mt_rand ( int $min , int $max )函数用于生成随机整数,其中$min–$max为ascii码的范围,这里取33-126,可以根据需要调整范围,如ascii码表中97–122位对应a–z的英文字母,具体可参考ascii码表;chr ( int $ascii )函数用于将对应整数$ascii转换成对应的字符,代码如下:

  1. function create_password($pw_length = 8) 
  2.     $randpwd = ''
  3.     for ($i = 0; $i < $pw_length$i++)  
  4.     { 
  5.         $randpwd .= chr(mt_rand(33, 126)); 
  6.     } 
  7.     return $randpwd
  8. // 调用该函数,传递长度参数$pw_length = 6 
  9. echo create_password(6); 

方法二:

1、预置一个的字符串$chars,包括a–z、a–z、0–9以及一些特殊字符;

2、在$chars字符串中随机取一个字符;

3、重复第二步n次,可得长度为n的密码,代码如下:

  1. function generate_password( $length = 8 ) { 
  2.     // 密码字符集,可任意添加你需要的字符 
  3.     $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|'
  4.     $password = ''
  5.     for ( $i = 0; $i < $length$i++ )  
  6.     { 
  7.         // 这里提供两种字符获取方式 
  8.         // 第一种是使用 substr 截取$chars中的任意一位字符; 
  9.         // 第二种是取字符数组 $chars 的任意元素 
  10.         // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); 
  11.         $password .= $chars[ mt_rand(0, strlen($chars) – 1) ]; 
  12.     } 
  13.     return $password

方法三:

1、预置一个的字符数组$chars,包括a–z、a–z、0–9以及一些特殊字符;

2、通过array_rand()从数组$chars中随机选出$length个元素;

3、根据已获取的键名数组$keys,从数组$chars取出字符拼接字符串。

该方法的缺点是相同的字符不会重复取,代码如下:

  1. function make_password( $length = 8 ) 
  2.     // 密码字符集,可任意添加你需要的字符 
  3.     $chars = array('a''b''c''d''e''f''g''h',  
  4.     'i''j''k''l','m''n''o''p''q''r''s',  
  5.     't''u''v''w''x''y','z''a''b''c''d',  
  6.     'e''f''g''h''i''j''k''l','m''n''o',  
  7.     'p''q''r''s''t''u''v''w''x''y','z',  
  8.     '0''1''2''3''4''5''6''7''8''9''!',  
  9.     '@','#''$''%''^''&''*''('')''-''_',  
  10.     '['']''{''}''<''>''~''`''+''='',',  
  11.     '.'';'':''/''?''|'); 
  12.     // 在 $chars 中随机取 $length 个数组元素键名 
  13.     $keys = ($chars$length);  
  14.     $password = ''
  15.     for($i = 0; $i < $length$i++) 
  16.     { 
  17.         // 将 $length 个数组元素连接成字符串 
  18.         $password .= $chars[$keys[$i]]; 
  19.     } 
  20.     return $password

时间效率对比:我们使用以下php代码,计算上面的3个随机密码生成函数生成6位密码的运行时间,进而对他们的时间效率进行一个简单的对比,代码如下:

  1. function getmicrotime() 
  2.     list($usec$sec) = explode(" ",microtime()); 
  3.     return ((float)$usec + (float)$sec); 
  4. // 记录开始时间 
  5. $time_start = getmicrotime(); 
  6. // 这里放要执行的php代码,如: 
  7. // echo create_password(6); 
  8. // 记录结束时间 
  9. $time_end = getmicrotime(); 
  10. $time = $time_end – $time_start
  11. // 输出运行总时间  
  12. echo "执行时间 $time seconds";  
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php中生成随机密码几种方法

103 评论

  1. brand indomethacin 75mg suprax usa generic amoxicillin 500mg

  2. orlistat 120mg brand acyclovir ca zovirax 800mg ca

  3. buy tiotropium bromide 9 mcg pills order spiriva 9 mcg hytrin 1mg without prescription

  4. order pioglitazone 15mg without prescription purchase sildenafil pills order viagra 100mg without prescription

  5. buy altace for sale amaryl 1mg sale etoricoxib 60mg generic

  6. buy glycomet 500mg for sale glycomet tablet buy tamoxifen 20mg sale

  7. ivermectin 12 mg without a doctor prescription stromectol 3mg pill order prednisone sale

  8. viagra order online price viagra order cialis 20mg pills

  9. order combivent 100mcg pill buy decadron sale linezolid 600 mg without prescription

  10. buy diflucan pills oral cipro cipro 1000mg pill

  11. order avanafil 100mg online avana uk where can i buy diclofenac

  12. order tamoxifen 10mg pill ceftin us order ceftin 250mg generic

  13. buy trimox cheap trimox cheap buy clarithromycin without prescription

  14. vardenafil without prescription buy tizanidine sale order generic plaquenil 200mg

  15. order carvedilol without prescription order generic cenforce buy chloroquine 250mg without prescription

  16. order lopressor 100mg generic atenolol 50mg drug buy cheap generic methylprednisolone

  17. acillin pills cost cipro metronidazole 200mg uk

  18. buy retin gel for sale avana buy online buy avanafil 100mg generic

  19. order terbinafine pill lamisil 250mg cheap order amoxicillin 250mg without prescription

  20. lansoprazole 15mg brand albuterol uk buy generic pantoprazole 40mg

  21. can i buy ed pills over the counter price of cialis tadalafil brand

  22. macrodantin buy online generic motrin pamelor over the counter

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡