Terradon
Terradon

Reputation: 858

Allowed memory size exhausted with recursive function

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

Upvotes: 2

Views: 696

Answers (1)

shudder
shudder

Reputation: 2101

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

Upvotes: 1

Related Questions