Re: how do you prevent spamming votes and page views
Posted: Mon Mar 15, 2010 7:10 pm
You need to determine what is acceptable in terms of how many times a user may make a page request within an allotted time. Also, are page views restricted only to authenticated users?
If only authenticated users are allowed to view your site, then you can create a traffic table in your database. Insert a new row on each page request and store the user's Id and timestamp (along with any other traffic you want to analyze). On each page request, retrieve the records from the database and count how many page requests have been made. If they've exceeded their allotted views, throttle them.
If anonymous users are allowed to view your site, bind your traffic table based on IP, rather than user id.
If only authenticated users are allowed to view your site, then you can create a traffic table in your database. Insert a new row on each page request and store the user's Id and timestamp (along with any other traffic you want to analyze). On each page request, retrieve the records from the database and count how many page requests have been made. If they've exceeded their allotted views, throttle them.
Code: Select all
<?php
/* psuedo-code */
# Vars
$traffic_window = 60; // 60 seconds
$traffic_max_requests_per_window = 10; // Allow the user 10 requests per minute.
# On page Request - Fetch Traffic
$traffic = "SELECT `timestamp` FROM `traffic` WHERE `user_id`='1' AND `timestamp`>'" . (time() - $traffic_window) . "';";
# Check Activity
if(num_rows($traffic) > $traffic_max_requests_per_window) {
// Throttle User. Redirect them to error page or something.
} else {
# Acceptable traffic pattern. Increment activity.
"INSERT INTO `traffic` (`user_id`, `timestamp`) VALUES ('1', '" . time() . "')";
# Garbage Collection
"DELETE FROM `traffic` WHERE `timestamp`<'" . (time() - $traffic_window) . "';";
}
?>