【发布时间】:2019-09-09 03:17:14
【问题描述】:
我想生成随机数两次说用户名和密码,然后想将它保存在一个文本文件中。我的脚本成功生成数字,但我无法在两个变量之间放置单个空格,也无法在文本文档中写入文件。请在下面找到我的 php 脚本:
/**
////generatePassword
**/
function generatePassword ($length = 7)
{
// start with a blank password
$genpassword = "";
// define possible characters
$possible = "0123456789";
// set up a counter
$i = 0;
// add random characters to $password until $length is reached
while ($i < $length) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
// we don't want this character if it's already in the password
if (!strstr($genpassword, $char)) {
$genpassword .= $char;
$i++;
}
}
// done!
return $genpassword;}
echo generatePassword().generatePassword();
$cfgclines='';
addFlinetoConfig($cfgclines);
writetoLog("cfgclines");
function addFlinetoConfig($cfgclines)
{$handle = fopen("/var/etc/user.cfg",'a');
fwrite($handle, $genpassword);
fclose($handle);
}
?>
当我在浏览器中打开此文件时,我得到如下随机数:83215063912640
第 7 个字符后应该有一个空格,如下所示:8321506 3912640
此外,我无法调用 echo 函数将其写入名为 user.cfg 的文本文件
【问题讨论】:
标签: php echo whitespace fwrite