Statistics

From: Date: Thu, 02 Sep 2004 15:07:45 +0000
Subject: Statistics
Groups: php.general 
Request: Send a blank email to php-general+get-195580@lists.php.net to get a copy of this message
I'm trying to develop a set of functions that can handle the statistics of a website. I want them to be as flexible as possible, and i want to be able to have several different "views" (day, week, month, year). For example if i wanted to know how many visitors, visits and page hits there was each month last year, i could call the function like this: $months = Statistics::getStats(STAT_VIEW_YEAR, 2004); foreach ($months as $month) { echo $month['visitors']; // ... } I'm thinking of doing it like this: <?php class Statistics { public static function logPageView () { global $db; // $db is just a MySQL object interface $ip = $_SERVER['REMOTE_ADDR']; $ex = STATS_SESSION_EXPIRE; $result = $db->query("UPDATE statistics SET visit_end = NOW() AND hits = hits+1 WHERE ip = '$ip' AND NOW()-visit_end < $ex ORDER BY visit_start DESC LIMIT 1"); if ($db->affectedRows() !== 1) { $db->query("INSERT INTO statistics (ip, visit_start, visit_end) VALUES ('$ip', NOW(), NOW())"); } } public static function getStats ($view = STATS_VIEW_DAY, $date) { global $db; switch ($view) { case STATS_VIEW_YEAR: $sql = "YEAR(visit_start) = $date GROUP BY MONTH(visit_start) ORDER BY MONTH(visit_start)"; break; // More cases default: return FALSE; } return $db->query("SELECT COUNT(DISTINCT ip) AS visitors, COUNT(*) AS visits, SUM(hits) AS hits FROM statistics WHERE " . $sql)->fetchArray(); } } ?> Is there a simpler way of approaching this? -- Daniel Schierbeck

« previous php.general (#195580) next »