Re: count clicks to count most important news
| From: | Stuart Dallas | Date: | Sun, 01 Jan 2012 20:10:26 +0000 |
| Subject: | Re: count clicks to count most important news | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316148@lists.php.net to get a copy of this message | ||
On 1 Jan 2012, at 16:26, muad shibani wrote:
> I have a website that posts the most important news according to the number
> of clicks to that news
> the question is : what is the best way to prevent multiple clicks from the
> same visitor?
I'm assuming this is not a voting system, and the news items you're counting are sourced
from your own site and, with all due respect to Ash, unlikely to be a target for false clicks. All
you're really wanting to do is prevent the site from registering multiple hits from the same
user in a short period of time.
I would probably use memcached on the server-side to store short-term information about clicks. When
a news item is loaded...
1) Construct the memcache key: "newsclick_<article_id>_<ip_address>".
2) Fetch the key from memcache.
3a) If it does not exist, log the hit.
3b) If it does exist, compare time() with the value and only log the hit if time() is greater.
4) Store the key with a value of time() + 300 and an expiry of the same value.
This will prevent hits being logged for the same news item from the same IP address within 5 minutes
of other hits.
Other alternatives would be to use cookies (could get messy, and not very reliable since it requires
the response from click 1 to be processed before click 2 gets started), Javascript (as suggested by
tedd but without the token - it would work pretty well and would be a lot easier to implement than
the above, but you sacrifice having full control over it).
If I'm interpreting the requirement correctly my solution is almost certainly overkill, and a
simple Javascript solution would be more than sufficient.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/