Total page view count
I store a total for how many times a members profile has been viewed by adding 1 to the total number in the DB on page load.
But want to somehow limit the amount of times a person viewing will add 1 to the total profile views.
Obviously without storing somehow when a person has viewed the profile, clicking page refresh will add one more view.
The profiles are also available to non-members, so tracking them with member data is not possible.
I guess it wont ever be possible to make a fool proof system where each person will only add 1 view to the total, but what's the best way to get a decent system?
I don't want to store IP's or other data in the DB, the profiles are already quite code heavy.
So I thought about Cookies. But the site could have thousands of profiles, and I "presume" storing a potential of hundreds of cookies on a persons PC just for checking if they have viewed a profile or not is a bit over the top.
I tried to add an array to a cookie, which I can easily check with in_array for the profile they are viewing, if no add 1 page view. But I can't seem to get a cookie to store an array as an array. Only a single cookie for each value in the array, which is back to hundreds of cookies.
Cheers
pr0gr4mm3r posted this at 17:30 — 25th June 2008.
He has: 1,502 posts
Joined: Sep 2006
Doing it by IP address is probably a bad idea because if a profile gets hit several times by a large network (different users), it will only count as one. Same for a popular proxy.
The cookie method isn't fool proof either because someone can just clear cookies to increase their page's count, but I think it is the best option.
I would create a PHP array with the profile IDs as the keys, so when a page is viewed, run something like this:
<?php
$viewed_profiles[$profile_id] = true;
?>
Then to see if the page was view before, just test if that index exists.
<?php
if (isset($viewed_profiles[$profile_id]))
{
// profile was viewed before
}
else
{
// profile was not viewed before
}
?>
If you are having trouble storing arrays in cookies, I would just serialize it first. At the end of your script, do something like this:
<?php
$viewed_profiles = serialize($viewed_profiles);
setcookie('viewed_profiles', $viewed_profiles);
?>
Then, at the top of the page, run something like this to retrieve it:
<?php
if (isset($_COOKIE['viewed_profiles']))
{
$viewed_profiles = unserialize($_COOKIE['viewed_profiles']);
}
else
{
$viewed_profiles = array();
}
?>
decibel.places posted this at 01:55 — 26th June 2008.
He has: 1,494 posts
Joined: Jun 2008
if the page view requires a log in you could append the viewer's user id
but not if it is being anonymously viewed
just a random thought
greg posted this at 16:19 — 27th June 2008.
He has: 1,581 posts
Joined: Nov 2005
I haven't had chance to have a full test, but the serialize() seems like the solution to my issue.
Cheers
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.