[PHP] Statistics
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Statistics
Daniel Schierbeck wrote: 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: Is there a simpler way of approaching this? You can save yourself a lot of time and effort by installing something like analog or webalizer (both free), unless ofcourse your hosting company is ripping you off by not giving access to the raw log files. The other factor you would want to consider is that inserting into a mysql database is much slower than retrieving from it particularly when you have lots of indices - you will need plenty of them if you are going to build any kind of report. -- Raditha Dissanayake. http://www.radinks.com/sftp/ | http://www.raditha.com/megaupload Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. Just 128 KB | with progress bar. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Statistics
From: Daniel Schierbeck [EMAIL PROTECTED] Is there a simpler way of approaching this? Use an already written program that parses your web logs and already gives you this information? ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Statistics
John Holmes wrote: From: Daniel Schierbeck [EMAIL PROTECTED] Is there a simpler way of approaching this? Use an already written program that parses your web logs and already gives you this information? ---John Holmes... I feel like doing it myself :) -- Daniel Schierbeck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Statistics function
the connection will close when you hit cancel but the PHP code can continue running if you choose. Really?!? How do you choose to have the PHP script continue even if a browser leaves/dumps? I've always wanted to do that. Didn't know it was possible. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Statistics function
On Thursday 15 March 2001 08:34, you wrote: If you want to get round-trip times including transmission to the user, you'd need to have some way of getting the user's browser to record a second request, which introduces a lot of potential variables. Why not simply use "ab" (the benchmarking app that comes with apache)? -- Christian Reiniger LGDC Webmaster (http://sunsite.dk/lgdc/) Pretty cool, the kind of power information technology puts in our hands these days. - Securityfocus on probing 3600 hosts for known problems in 3 weeks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Statistics function
Hello everybody, How does the behind the scenes work by PHP exactly go? Does the PHP thread remain active so long as the information is sent to the visitor? Or will PHP parse the code and send it to Apache which will send the data to the user itself? The reason that I'm asking this is I'm writing a statistics add-on for one of my sites and was wondering whether the code below would give the time in which PHP parses the code or in which the data gets streamed to the user. $time_one = microtime(void); fpassthru($file); $time_two = microtime(void); // + here will be the code to calculate the difference //betweeen $time_one and $time_two Would I get information about my server (parsing time) or would I get some information about the requesters internet connection (how fast will my files get to the user). Both would be pretty important! PS What are the advanced statistics programs out there? I wouldn't mind seeing a sample report (any commercial or non-commercial). Thanks in advance in advance! Yours, Lauri -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Statistics function
On 14 Mar 2001 22:11:10 -0800, Rick St Jean [EMAIL PROTECTED] wrote: All through this process there is a live thread between your browser and the server. unless you send a cancel. One minor addition - the connection will close when you hit cancel but the PHP code can continue running if you choose. This can be quite handy if you want to make sure your code can finish running or clean up nicely before quitting. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Statistics function
On 14 Mar 2001 21:39:05 -0800, Lauri Vain [EMAIL PROTECTED] wrote: How does the behind the scenes work by PHP exactly go? Does the PHP thread remain active so long as the information is sent to the visitor? Or will PHP parse the code and send it to Apache which will send the data to the user itself? 1 Apache receives the request and determines that it's a PHP script. 2 PHP parses your code 3 PHP executes your code 4 PHP passes output back to Apache 5 Apache sends output to the browser (optionally through things like SSL) 6 Apache finishes request (e.g. logs it, closes a non-persistent connection) The catch is that steps 4/5 may repeat - this depends on your output buffering setup. PHP can wait and send everything when the script finishes, send output in blocks or send every output from each echo/print immediately. (PHP can also do things like compress that output on the fly, but that's a separate conversation) The reason that I'm asking this is I'm writing a statistics add-on for one of my sites and was wondering whether the code below would give the time in which PHP parses the code or in which the data gets streamed to the user. You'd get the time needed to read the file and either send it or add it to the output buffer (depending on your output buffer config as mentioned above). By the time your code is executed, PHP will have already parsed your script (with the exception of things like conditional / variable include()s and require()s), so you'd only get the time required to execute the code, not parse it. If you want to get round-trip times including transmission to the user, you'd need to have some way of getting the user's browser to record a second request, which introduces a lot of potential variables. It might be interesting to see how something relying entirely on client-side calculation like the code below would compare with other approaches (e.g. Header("Location: measure.php?start=" . time()) where measure.php just does $elapsed = time() - $start). It should remove almost all of the server-side latency and, in theory, would give some reasonable indication of how long it takes to transfer a file over the intermediate network connection, which would give you a reasonable idea of available bandwidth. (Disclaimer: completely untested code written just now which has never been seen by anything other than vi. Caveat programmer.) html head script lang="javascript" Start = new Date(); function record_elapsed() { Now = new Date(); Elapsed = Now.getTime() - Start.getTime(); recordTimeImg = new Image(); recordTimeImg = "recorder.php?Elapsed=" + Elapsed; } /script ? flush(); /* send this as quickly as possible */ ? /head body OnLoad="record_elapsed();" ... a fair amount of text / HTML ... /body /html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]