STaRDoGG Posted March 14, 2010 Share Posted March 14, 2010 I'm writing a simple logging routine that checks if a name is already in the array, and decide how to proceed from there. So my array needs to retain it's items (names) each time the function is called. Right now, everything works fine with the exception of the array retaining it's items. The script (page) gets reloaded once every couple of minutes. Here's the entire function: function Logging($aimid, $Online) { // *Logs any changes for a user* // This array will hold only the AimID's of anyone who is currently Online static $UserLogArray = array(); //echo '<pre>' . print_r($UserLogArray, true) . $aimid . ' ' . $Online . '</pre>'; switch ($Online) { Case '1': // User is Online ... // search for the current AimID (User Nickname) in the static $UserLogArray if (($i = array_search($aimid, $UserLogArray)) !== FALSE) { // $i = contains the array key of the name found in the array. // ... AND the user was found in the array, meaning we already added them to it, so do nothing, just exit. //exit; return; } // ... user wasn't found in the array, so add them and log it ... // Add aimid to array here, and the string to add to the log file $UserLogArray[] = $aimid; $write = "Online: " . $aimid . " " . Fetch_TimeStamp() . "\n"; break; Case '0': // User is Offline ... // search for the current AimID (User Nickname) in the static $UserLogArray if (($i = array_search($aimid, $)) !== FALSE) { // $i = contains the array key of the name found in the array. // ... AND the user was found in the array, so remove them from the array, and write that they went offline to the log. array_splice($UserLogArray, $i, 1); $write = "Offline: " . $aimid . " " . Fetch_TimeStamp() . "\n"; break; } // ... user wasn't found in the array, so just exit. return; } // Finally, open the Log and append to the file $fp = fopen('aim-log.txt', 'a'); $write = fputs($fp, $write); fclose($fp); } I've tried commenting out static $UserLogArray = array(); in the function, and placing this session code at the top of the file instead: session_start(); if (!is_array($_SESSION['UserLogArray'])) $_SESSION['UserLogArray'] = array(); But when I do that, I get errors saying there's a problem with the second parameter in array_search. I've also tried changing the array var declaration at the beginning of the function to this instead: static $UserLogArray; if (is_null($UserLogArray)){ $UserLogArray = array(); } But I get the same error again about the array_search parameter. Anyone able to shed some light on this? Thanks. Link to comment https://forums.phpfreaks.com/topic/195170-problem-with-static-array-retaining-its-items/ Share on other sites More sharing options...
trq Posted March 14, 2010 Share Posted March 14, 2010 The script (page) gets reloaded once every couple of minutes. Variables (static or not) are not persisted across multiple requests. You will either need to use the $_SESSION array (which does persist) or a data storage mechanism (eg; database / text file). But when I do that, I get errors saying there's a problem with the second parameter in array_search. Can we see this piece of code? Link to comment https://forums.phpfreaks.com/topic/195170-problem-with-static-array-retaining-its-items/#findComment-1025824 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.