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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历