dsyct08008 2016-01-22 15:59
浏览 40
已采纳

允许的内存大小用递归函数耗尽

Part of trying to catch multi-accounts by same user, we place a cookie when users login. During login, first we try to read a previous cookie and replace it by a new one. Read and written cookies are stored in a database. When cookies aren't deleted, this system can be part of recognizing multi-accounts.

When searching for multi-accounts with a recursive function, we ran out of memory. When not used as recursive, it works, but sometimes a user has so many accounts, we only find them by repeating this function for all found accounts. It works when repeating this function max 3 times, but to be sure, this function must run, untill no multi-accounts are found.

function xtest_getOtherNicks($dbh,$nick){
    /**
        Find all duplicate accounts, based on cookie
    */
    $aOtherNicks = array();
    $aCookies = db\ct_getCookies($dbh,$nick);

    foreach($aCookies as $k => $aCookie){

        $aFoundNicks = db\ct_getNick($dbh,$aCookies[$k]['cookiewaarde']);

        if(count($aFoundNicks) > 0){

            foreach($aFoundNicks as $key => $aFoundNick){

                if(!in_array($aFoundNick['nick'],$aOtherNicks)){
                    $aOtherNicks[] = $aFoundNick['nick'];

                    //Recursive part
                    //$aMoreFoundNicks = xtest_getOtherNicks($dbh,$aFoundNick['nick']);
                    /**
                    if(count($aMoreFoundNicks)>0){
                        foreach($aMoreFoundNicks as $key => $aMoreFoundNick){
                            if(!in_array($aMoreFoundNick['nick'],$aOtherNicks)){
                                $aOtherNicks[] = $aMoreFoundNick['nick'];
                            }    
                        }
                    } */

                }
            }
        }
    }

    return $aOtherNicks; 
}

I have searched for other recursive problems, but they were different then mine. Perhaps it is possible to change the SQL and let the database do the job, but i am clueless for that part.

table:

CREATE TABLE IF NOT EXISTS `cookietest` (
  `cookiewaarde` varchar(100) NOT NULL,
  `nick` varchar(100) NOT NULL,
  `datum` datetime NOT NULL,
  `rw` enum('r','w') NOT NULL,
  KEY `cookiewaarde` (`cookiewaarde`),
  KEY `nick` (`nick`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

query for cookies:

$q = "SELECT 
            cookiewaarde
        FROM
            cookietest
        WHERE
            nick = '".sanitize($nick)."'";// will be converted to PDO
  • 写回答

1条回答 默认 最新

  • doufang8282 2016-01-23 02:22
    关注

    You need to pass $aOtherNicks into recursive scope. Otherwise you wont break recursive loop because it will compare against empty array (found values always unique). This shuold work:

    function xtest_getOtherNicks($dbh, $nick, $aOtherNicks = array())
    {
        $aCookies = db\ct_getCookies($dbh, $nick);
        foreach ($aCookies as $aCookie) {
    
            $aFoundNicks = db\ct_getNick($dbh, $aCookie['cookiewaarde']);
            foreach ($aFoundNicks as $aFoundNick) {
                if (in_array($aFoundNick['nick'], $aOtherNicks)) { continue; }
                $aOtherNicks[] = $aFoundNick['nick'];
                $aOtherNicks = xtest_getOtherNicks($dbh, $aFoundNick['nick'], $aOtherNicks);
            }
        }
    
        return $aOtherNicks;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能