示例

JS

1
2
3
4
5
6
7
8
9
function generate_password(length = 16) {
let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
let password = ''
for (let i = 0; i < length; i++) {
password += chars[ Number.parseInt(chars.length * Math.random()) ]
}
return password
}
console.log(generate_password(16))

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php 
function generate_password( $length = 16 ) {
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
// 使用
echo generate_password(16);
?>