我在电子邮件中生成参考号,有时它会在某些邮件中重复参考号。
我在我的代码中使用了 microtime()、getTimestamp() 和随机生成函数。我使用 WAF 缓存作为我的服务器。注意:我不能使用数据库。有谁知道如何解决这个问题?
function generate_inquiry_reference_no(){
$date=current_time("Y-m-d");
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 8; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $date."-".$randomString;
}
function generate_email_reference_no(){
$date = new DateTime();
$email_ref_no=$date->getTimestamp();
$ref_no = ((microtime() * 100000)*10);
return $email_ref_no."-".$ref_no;
}```
考虑使用类似的方法random_bytes
来获得实际的随机结果,而不是基于时间的结果。
function str_rand(int $length = 64){ // 64 = 32
$length = ($length < 4) ? 4 : $length;
return bin2hex(random_bytes(($length-($length%2))/2));
}
echo (new DateTime())->format('Y-m-d') . '_' . str_rand(10)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句