Forum Moderators: coopster

Message Too Old, No Replies

Page View Count with IP Check

         

username

5:52 am on Aug 29, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all,

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.

PHP_Chimp

8:36 am on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The advantage of a cookie based solution is that some of the information is stored on the client machine. If you want to get away from cookies you could use a session--although that may well still involve a cookie--or you could store that information in your database.

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.

username

9:23 am on Aug 29, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks. The real challenge is storing data temporarily I guess. I really need some pointers on storing the info temporarily. I would need to:

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

penders

12:50 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem with using the IP to identify visitors is that visitors from the same organisation are all likely to have the same IP. Is this an issue for you? Using a cookie/session gets around this.

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?

PHP_Chimp

9:17 pm on Aug 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are going with sessions then you could use an array to store the pages visited along with the time.

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)
)
)

Then you can just check that ['page']['somepage.php']['time'] +30mins < time(), and if so ['page']['somepage.php']['visits'] +1;

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]