网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站设计 > PHP
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 180.125.50.74
Old 2015-01-16, 09:20 AM
New Brighton New Brighton is offline
初级会员
 
Join Date: 2007-05-15
Posts: 1
New Brighton 现在声名狼藉
Default php生成随机密码的方法

php使用十分广泛,其中很多人都会对生成随机密码的方法感兴趣。当然方法不是唯一的,今天就为大家介绍关于php生成随机密码的方法两则。

方法一:

1、在 33 – 126 中生成一个随机整数,如 35,

2、将 35 转换成对应的ASCII码字符,如 35 对应 #

3、重复以上 1、2 步骤 n 次,连接成 n 位的密码

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

Code:
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、预置一个的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符

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

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

Code:
function generate_password( $length = 8 ) {
 // 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';

$password = '';
 for ( $i = 0; $i < $length; $i++ )
 {
 // 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
 $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
 }

return $password;
}
注意事项



php生成随机密码的方式虽然不是唯一的,但是切勿混淆使用。
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 10:52 PM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.