note 55897 added to ref.array

From: Date: Tue, 16 Aug 2005 21:29:37 +0000
Subject: note 55897 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-93766@lists.php.net to get a copy of this message
With little help of serialize/unserialize Arrays can be used just like database (of course less functional, but still) <?php /*********************************************************** * Easy Counter * * Author: Vladson <vladson@pc-labs.info> * Web-site: http://vladson.pc-labs.info/ ***********************************************************/ // Settings $db_file = 'counter.txt'; // Get date and ip $date = date('Ymd'); $ip = $_SERVER['REMOTE_ADDR']; // Read counter's database file if ( file_exists($db_file) ) { $fp = fopen($db_file, "r"); $content = fread($fp, filesize($db_file)); fclose($fp); $array = unserialize($content); } else { $array = array(); } // Clean old database records if nesesery if ( !$array['records'][$date] ) { unset($array['records']); } // Update statistics $array['records'][$date][$ip]++; $array['total']++; // Write counter's database file $content = serialize($array); $fp = fopen($db_file, "w"); fputs($fp, $content); fclose($fp); // Count statistics $hits = array_sum($array['records'][$date]); $hosts = count($array['records'][$date]); $total = $array['total']; echo "Hits - $hits // Hosts - $hosts // Total - $total"; ?> ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55897 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55897&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55897&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55897&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55897&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55897&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55897&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55897&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93766) next »