Fixing ShortStat
Posted by Jonathan Ng | Filed under Technical
After struggling for a couple of days trying to make ShortStat work (under stress and fatigue) to no avail, some prodding today revealed a simple (but unexplainable) workaround. The original ShortStat code uses the following to get the time (in seconds past, whenever it was) for 12:00 AM, today.
$dt = strtotime(gmdate("j F Y",time()+(((gmdate('I'))?($this->tz_offset+1):$this->tz_offset)*3600)));
$dt = $dt-(3600*2); // The above is off by two hours. Don't know why yet...
$dt = $dt-(3600*24);
Perhaps it worked in an older version of PHP 4.3.x, or maybe when time passes a certain point it wouldn’t work. I’m not a PHP guru so I have no idea. Heck, even the author had no idea. So when that code broke, the “number of visitors today” showed funny numbers. After much trial and error, I finally fixed mine. However, I’m not sure whether it’ll work on all servers, so here’s the logical steps to debug, and fix ShortStat.
Look for a funtion called getTodaysHits() in the codes (wp-shortstat.php). My shortstat is already modified so I don’t know which line it was at originally. You’ll see the code listed above at the second line. Add the last line of code:
$dt = strtotime(gmdate("j F Y",time()+(((gmdate('I'))?($this->tz_offset+1):$this->tz_offset)*3600)));
$dt = $dt-(3600*2); // The above is off by two hours. Don't know why yet...
$dt = $dt-(3600*24);
echo gmdate("g:i a j F Y",$dt); // add this line!
Re-upload the file (wp-shorstat.php) to your plugins folder and refresh the ShortStat page. Under “Just Today as of xxx”, you’ll see a date and time. Well, if you could see from the previous codes, some adjustments were made to the variable $dt (dateTime). The general format is “$dt-(3600*x)”. In line 2, $dt was reduced by 2 hours, and then in line 3, by 24 hours. Hence, a total of 26 hours were deducted from $dt. Back to the date and time you see.For the statistics to function properly, it should show 12:00 AM (today’s date). For example, mine showed 3:00 AM, (yesterday). That meant mine was 21 hours too early. Some simple math: 26 - 21 = 5.
$dt = strtotime(gmdate("j F Y",time()+(((gmdate('I'))?($this->tz_offset+1):$this->tz_offset)*3600)));
$dt = $dt-(3600*5);
That’s all it takes. Weird enough huh? Refresh your page again and it should show 12:00 AM (today). When you’re done, you also need to make the same changes to getTodaysUniqueHits() and getWeeksHits(), which is somwehere below getTodaysHits().
Anyways, besides fixing that, I also discovered that half my hits were from search engines by adding “AND browser != ‘Crawler/Search Engine’ ” to the SQL queries for the statistics. I also added “Recent Resources” and “Recent Referrers” stats. Sigh, I’ve got boring blog.
Tags: web_apps