Skip to content

Instantly share code, notes, and snippets.

@Jason-cqtan
Created March 14, 2019 08:07
Show Gist options
  • Save Jason-cqtan/d9a656de317d8dae5fa21ff9b95c6b16 to your computer and use it in GitHub Desktop.
Save Jason-cqtan/d9a656de317d8dae5fa21ff9b95c6b16 to your computer and use it in GitHub Desktop.
生成随机字符串,类似“CuOi3OQNt”
<?php
//方法一
$password_length = 8;
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
// 随机字符总集
$alfa = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$token = "";
for($i = 0; $i < $password_length; $i ++) {
$token .= $alfa[rand(0, strlen($alfa))];
}
echo $token;
echo '<br>';
//方法二
// 创建密码
$totalChar = 9; // 密码中字符串的个数
// salt to select chars from
$salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
srand((double)microtime()*1000000); // 启动随机产生器
$Spass = ""; // 设置初始值
for ($i=0;$i<$totalChar;$i++) // 循环创建密码
$Spass .= substr ($salt, rand() % strlen($salt), 1);
echo $Spass;
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment