ohdang888 Posted December 24, 2007 Share Posted December 24, 2007 hey. heres my counter code... <?php $file = "counter.txt"; $open = fopen($file, 'r'); $readdata = @fread($open, 10); fclose($open); $open = fopen($file, 'w'); //opens the file using only writing permissions $readdata++; //Adds one to the count. fwrite($open, $readdata); //Overwrites the file with the new page view count. fclose($open); //Closes the file. echo "Total Plays: $readdata"; //Displays the total views. ?> and counter.txt is locaed in the same folder... but i also want to display how many views in the last week... how would i do that? Quote Link to comment https://forums.phpfreaks.com/topic/83071-site-counters-time-sensitive/ Share on other sites More sharing options...
corbin Posted December 24, 2007 Share Posted December 24, 2007 You would have to either keep two files, and truncate one every week, or you could log times as well (which could potentially make your files huge). The first way has a disadvantage too. The first way wouldn't tell you how many views in a past week as in X time, it would tell you how many views THIS week as in like since Sunday or Monday or some preset delete time. And yes, this would mean it would suddenly jump from X to 0, and no one wants a site counter that has like 10 views. The second way, you would have to log every visit time, and then, you would have to go through them, counting the ones where the time is > a week ago. Quote Link to comment https://forums.phpfreaks.com/topic/83071-site-counters-time-sensitive/#findComment-422577 Share on other sites More sharing options...
ohdang888 Posted December 25, 2007 Author Share Posted December 25, 2007 wow. doesn't sound worth the work Quote Link to comment https://forums.phpfreaks.com/topic/83071-site-counters-time-sensitive/#findComment-422717 Share on other sites More sharing options...
corbin Posted December 25, 2007 Share Posted December 25, 2007 Hrmmmm you could maybe do it with SQL and store it by day.... For example, you could store it kinda like: 12/17/07 - 200 hits 12/18/07 - 220 hits 12/19/07 - 250 hits 12/20/07 - 220 hits so on.... And then, you could just pull the total for today minus 7 days. This way, you would only update the count for today. In other words, you would take the hit count for today and update it to old_value + 1. File wise, you could do the same thing, but it would be a little more... ghetto. Quote Link to comment https://forums.phpfreaks.com/topic/83071-site-counters-time-sensitive/#findComment-422725 Share on other sites More sharing options...
corbin Posted December 25, 2007 Share Posted December 25, 2007 I got kinda bored and wanted to see if I could do it, so I made a little 'library' of log functions. (Flat file, not database) <?php function MakeName($days = 0, $ext = '.log') { if($ext == null) $ext = '.log'; $offset = $days*86400; return date('Y-m-d', time() + $offset) . $ext; } function GetLogCount($day = 0, $ext = null) { $name = MakeName($day, $ext); if(!file_exists($name)) return 0; $contents = file_get_contents($name); $contents = trim($contents); return (int) $contents; } function IncrementLog($day = 0) { $name = MakeName($day); $count = GetLogCount($day); if($han = @fopen($name, 'w')) { $c = $count+1; return (@fwrite($han, $c) && (@fclose($han))); } return false; } function GetDays($days = 7) { $amount = 0; for($i = 0; $i < $days; $i++) { $amount += GetLogCount(-$i); } return $amount; } ?> To use it, you would do something like this: IncrementLog(); //add 1 to today IncrementLog(-1); //add 1 to yesterday. Not sure why you would need this. echo GetLogCount(); //put out log count from today echo GetLogCount(-1); //put out log count from yesterday echo GetDays(); //put out log count from the week echo GetDays(31); //put out log count from month It's worth noting, especially on a high traffic site, that you could run into a problem. If two scripts tried to open the file at the same time, it could cause weird problems, like over writing the file and stuff. You could sort of get around that with a script-level file lock, but even that could cause problems. Then again, maybe PHP support OS level locks on linux... Not sure. Quote Link to comment https://forums.phpfreaks.com/topic/83071-site-counters-time-sensitive/#findComment-422751 Share on other sites More sharing options...
cooldude832 Posted December 25, 2007 Share Posted December 25, 2007 SQL is your best option make a table PageID (BigINT) HitTime (Date/Time) insert query <?php //Open sql conneciton $q = "Insert into`PageHits` (PageID,HitTime) VALUES('".$PageID."'", NOW())"; $r = mysql_query($q) or die(myql_error()); ?> Count Query <?php $days_back = 7; $days_sql = Date("U")-($days_back*3600*24); $q = "Select count(*) as `Hits` From `PageHits` Where HitTime >= ".$days_sql." and PageID = '".$pageID."'"; $r = mysql_query($q) Or die(mysql_error()); $hits = mysql_result($r,0); echo $hits; ?> Should work just as you need, site wise just need to add pageids to pages. Quote Link to comment https://forums.phpfreaks.com/topic/83071-site-counters-time-sensitive/#findComment-422760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.