how can i setup a counter which..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

how can i setup a counter which..

Post 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?
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post 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
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post 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.
Wandrer
Forum Newbie
Posts: 21
Joined: Thu Jun 06, 2002 8:43 am

Post 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.
Wandrer
Forum Newbie
Posts: 21
Joined: Thu Jun 06, 2002 8:43 am

Post 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.
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post 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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

hmm all of them are greate ideas but, how do i set to auto delete? :?:
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

$yesterday = date("mdy",strtotime("yesterday");
DELETE FROM table WHERE date<'$yesterday'

I don't know if that will work but try it.
Wandrer
Forum Newbie
Posts: 21
Joined: Thu Jun 06, 2002 8:43 am

Post 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...
Post Reply