内存过载而没有警告

时间:2017-04-05 09:43:41

标签: php

我正在创建一个脚本来从数字校验和中生成一个自定义的字母数字校验和,它的工作完全正常,但有时它会在不停止的情况下运行。 为避免这种情况,我对已用内存进行了一些“检查”。但即使有,它有时会继续执行,直到执行限制。所以你有任何想法解决问题吗? (在PHP 5.5.12的Windows上使用WAMP) 脚本:

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));

感谢您的帮助,对我的英语感到抱歉。

0 个答案:

没有答案
相关问题