Php 无警告的内存过载
我正在创建一个脚本,从数字校验和生成一个自定义字母数字校验和,它工作得很好,但有时运行时不会停止。 为了避免这种情况,我对已使用的内存进行了一些“检查”。但即使这样,有时它也会继续执行,直到执行极限。那么你有什么办法解决这个问题吗? (在带有PHP 5.5.12的windows上使用WAMP) 剧本:Php 无警告的内存过载,php,Php,我正在创建一个脚本,从数字校验和生成一个自定义字母数字校验和,它工作得很好,但有时运行时不会停止。 为了避免这种情况,我对已使用的内存进行了一些“检查”。但即使这样,有时它也会继续执行,直到执行极限。那么你有什么办法解决这个问题吗? (在带有PHP 5.5.12的windows上使用WAMP) 剧本: function generateChecksum($checksum) { // The string where the characters of the checksum will
function generateChecksum($checksum) {
// The string where the characters of the checksum will be taken
$securityStr = "frKRHndwmB2CyvWMVJek0GEOQUiaFjAL7zxZT8cNuI1s6pS3t4h9PXDoY5lbqg";
// We declare the variables
$cs = "";
$sum = 1;
// We reduce the size of the numeric checksum
$checksum = $checksum % 30;
// We generate the alphanumeric checksum
while ($sum!=$checksum) {
if ($sum<$checksum) {
$rand=rand(0,24);
$c=substr($securityStr, $rand, 1);
$sum+=$rand;
$cs.=$c;
while ($sum>$checksum) {
$sum-=$rand;
$cs = substr($cs, 0, -1);
$rand = rand(0, $rand);
$sum+=$rand;
$cs.=$c;
// A first check of memory use
if (memory_get_usage()>200000) {
throw new Exception("Memory overload");
exit;
}
}
}
// A second one
if (memory_get_usage()>200000) {
throw new Exception("Memory overload");
exit;
}
}
// If the checksum is too big or too short we generate another one
if (strlen($cs)>15||strlen($cs)<5) {
// Another one
if (memory_get_usage()>200000) {
throw new Exception("Memory overload");
exit;
}
return generateChecksum($checksum);
}
return $cs;
}
// We set a time limit
set_time_limit(10);
// And we call the function
var_dump(generateChecksum(37854));
函数generateChecksum($checksum){
//将获取校验和字符的字符串
$securityStr=“frKRHndwmB2CyvWMVJek0GEOQUiaFjAL7zxZT8cNuI1s6pS3t4h9PXDoY5lbqg”;
//我们声明变量
$cs=”“;
$sum=1;
//我们减小了数字校验和的大小
$checksum=$checksum%30;
//我们生成字母数字校验和
而($sum!=$checksum){
如果($sum$校验和){
$sum-=$rand;
$cs=substr($cs,0,-1);
$rand=rand(0$rand);
$sum+=$rand;
$cs.=$c;
//内存使用的第一次检查
如果(内存使用率()>200000){
抛出新异常(“内存过载”);
出口
}
}
}
//第二个
如果(内存使用率()>200000){
抛出新异常(“内存过载”);
出口
}
}
//如果校验和太大或太短,我们将生成另一个校验和
如果(斯特伦($cs)>15 |斯特伦($cs)200000){
抛出新异常(“内存过载”);
出口
}
返回generateChecksum($checksum);
}
返回$cs;
}
//我们设定了一个时限
设置时间限制(10);
//我们称之为函数
var_dump(generateChecksum(37854));
感谢您的帮助,我的英语很抱歉。使用简单的内置校验和生成器(如crc32或md5)不是更容易吗?是的,但md5返回32个字符(太长),crc32已经用于生成数字校验和,但我想要字母数字的,问题仍然是为什么超时?