Page 1 of 1

how can i setup a counter which..

Posted: Mon Jun 10, 2002 5:34 pm
by qads
how can i setup a counter which can record site hits on daily basis?

by that i mean a counter which can goes like

"Total hits for day XXX"

and every day it starts from 0.

i can code it, have no problem with that, but HOW?

Posted: Mon Jun 10, 2002 6:39 pm
by Zmodem
Get the user agent each time the page loads (automagically stored in the $HTTP_USER_AGENT variable). After getting the user agent, write it a DB, along with a time/date stamp.

Now, for your counter, just select all hits in the DB on today's date.

voila

Posted: Mon Jun 10, 2002 6:40 pm
by chris12295
you could open a file using the write option and change a number everytime the script is accessed and use an auot-date feature before your return results to show the date. You could also add a line in your script that gets the date to determine if its a new day and the script should reset.

Posted: Mon Jun 10, 2002 7:27 pm
by Wandrer
you could open a file using the write option and change a number everytime
The only problem with that is you have to watch out for the script being called more than once at the same time. You can use file locking, but that can also cause problems. Best solution is to put the info in a database.

Posted: Mon Jun 10, 2002 7:34 pm
by Wandrer
A good way would be to:

Store date("mdy") in a variable.

Insert into your counter database the variable stored above.

Do a query -- 'select count(date) as Count from counterdb where date="mdy" limit 1'

Use mysql fetch array to get value of Count.

Display it in whatever form you desire.

Posted: Tue Jun 11, 2002 5:54 am
by MattF
It might be an idea to have an automatic delete after a few days other wise your database will get very full if you have lots of visitors/pageviews

Posted: Tue Jun 11, 2002 6:03 am
by qads
hmm all of them are greate ideas but, how do i set to auto delete? :?:

Posted: Tue Jun 11, 2002 6:18 am
by MattF
$yesterday = date("mdy",strtotime("yesterday");
DELETE FROM table WHERE date<'$yesterday'

I don't know if that will work but try it.

Posted: Tue Jun 11, 2002 7:29 am
by Wandrer
It might be an idea to have an automatic delete after a few days other wise your database will get very full if you have lots of visitors/pageviews
With only one record per day, it would take hundreds of years before it would fill up.

Instead of deleting the historical data, you could generate really interesting graphs with it instead:

last week visitor graphs
a graph comparing last week to the first week in may
yesterday compared to the average stats of monday
last 30 days graph
etc...