Forum Moderators: coopster
I have written a script which increments a MySQL database value every time a user views a particular article on a site I am developing. Only catch is, the corresponding count field for each article needs to increment only once for each user, and disallow multiple refreshes.
I was thinking of writing a script which sets a timeout value of say 30mins, whereby if a user refreshes the page multiple times it does not increment the value until after they most likely leave the page (30mins), however, I have not been able to locate any good resources on setting a timeout per user IP address, and have tried a cookie based solution, which I do not really want to use.
Does anyone know how to do this, or have any good resources they can point me to?
Thanks in advance.
A session may be the way you want to go. As you could use that to store all sorts of info and the user can only look at there session id. So you can add functionality to this later.
If you want everything to be server side then you would need to store the ip addresses of each page view then check that the same ip address hadn't already been used to increment the page count within 30 mins.
I would suggest sessions, as if you are recording information about your users then you are bound to want to add to that later. So a session is forward compatible. Storing info in a database is not so easily forward compatible.
- Get IP
- Store url's visited next to that IP in a Database temporarily
- Clear the results at a later date
Each article page includes a unique id, but other than storing a concatenated list of these unique id's in a db field, is there a better and more efficient way to do this that does not put undue pressue on the database with regular calls.
I am really not comfortable with storing into a database because of the overhead...any suggestions.
To handle your "no increment within 30mins per page", you need to store a timestamp of when the counter was last incremented. When your user next visits (or refreshes), check this timestamp against the current time. If < 30mins then exit.
It may depend on how you want to report on your data, but I would have thought that if you want to record the number of article views per user per article then you would need at least the fields:
1. IP/USER ID [PKEY]
2. ARTICLE ID [PKEY]
3. TIMESTAMP
4. COUNTER
You'll get lots of small records. But at least this way, the queries should be quick... using indexes. And you can easily delete all results older than a certain age etc. With the volume of data that could result from this, I would have thought a DB would be the best option?
So you could have a multilevel array with:
$_SESSION = array(
'ip' => '10.0.0.0',
'page' => array(
'somepage.php' => array(
'time' => 1000000000, //some time stamp. From time();
'visits' => 1),
'anotherpage.php' => array(
'time' => 1000000001,
'visits' => 2)
)
)
Sessions should work well for temporary storage. You could then write the session data to your logs before you destroy the session. Maybe after there has been no activity for 30 mins or something like that.
[edited by: PHP_Chimp at 9:19 pm (utc) on Aug. 30, 2008]