Jump to content

counting hits


dhimo

Recommended Posts

You'll need to have something on each page that records the hit in either a log file or a database. A database would make it a lot easier to process the log entries. You would want to store the page they accessed and the date and time when they access it. So you would need a piece of PHP code on every page that you want to track to add an entry to the database whenever someone loads the page, then you can write a piece of code to search through the database and get all the info.Keep in mind that keeping track of things like this start to take up a lot of disk space very quickly. If you don't clean out the old log entries regularly you are going to run into out-of-space messages eventually.Most hosts also come with pre-installed stats packages that provide this type of information already.

Link to comment
Share on other sites

The SQL statement is entirely dependent on how you set up the database in the first place. If it is a date field, you can use the SQL date functions to return results in a certain range. If it is an integer field with a Unix timestamp, you can use the date functions in PHP to figure out which range to get.

Link to comment
Share on other sites

This is the UPDATE query somewhat like what JustSomeGuy may be hinting to

mysql_query("UPDATE hitcount_table SET hits = hits + 1 WHERE hour = '" . date("H") . "'");

For this you only need a single table (hitcount_table) with two columns: hour INT(2) which has all the hours preset (from 0 to 23), and the hits INT(6) DEFAULT 0 column, which will record the hits for each hour.I'm not too familiar with GD, but to select the results, you would just run

$result = mysql_query("SELECT * FROM hitcount_table");while ($hour = mysql_fetch_assoc($result)) {$hour['hour']; //This is the hour$hour['hits']; //This is the number of hits//You could use these values to add a chart element}

Link to comment
Share on other sites

That is true... hmm... you are right, that will only record the server time that the client comes at, not the user time, so would be unhelpful in some respects, although you still will be able to see at what server times the people come on.There is no way of detecting the client's time purely through PHP, you will need to use AJAX to get JavaScript to send the client's time through to your PHP code, like in the example below. All database tables remain the same, and the method of selection will still be as above.This goes in the header of the document you want to count

<script type="text/javascript">function count_hit() {   var ua = navigator.userAgent.toLowerCase();   if (!window.ActiveXObject) request = new XMLHttpRequest();   else if (ua.indexOf('msie 5') == -1) request = new ActiveXObject("Msxml2.XMLHTTP");   else request = new ActiveXObject("Microsoft.XMLHTTP");   today = new Date();   thishour = today.getHours();   request.open("GET", "count_hit.php?hour=" + thishour, true);   request.send(null);}</script>

And set the body onload to run the script

<body onload="count_hit()">

Then, have a page called count_hit.php that will update the database. This is all it needs to have:

mysql_connect("host", "username", "password"); //Replace with actual MySQL detailsmysql_select_db("hits"); //Replace with actual database namemysql_query("UPDATE hitcount_table SET hits = hits + 1 WHERE hour = '" . $_GET['hour'] . "'");

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...