php中生成随机密码几种方法(1/2)
mt_rand ( int $min , int $max )函数用于生成随机整数,其中$min–$max为ascii码的范围,这里取33-126,可以根据需要调整范围,如ascii码表中97–122位对应a–z的英文字母,具体可参考ascii码表;chr ( int $ascii )函数用于将对应整数$ascii转换成对应的字符。
代码:
function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $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的密码。
代码:
function generate_password( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_ []{}~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i {
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
1 2

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools
