I need to store a record for a user per IP address to limit the rate at which a certain IP address can perform an action.
I know that storing the IP in a database would work but I am worried that on a small VPS with limited resources, too much processing power would be taken by the MySQL process. Is there another way of storing data for an IP?
I thought of a system similar to:
/ips/
/ips/127/
/ips/127/0
/ips/127/0/0
/ips/127/0/0/1.txt
Sample Code:
$ip_parts = explode('.', $_SERVER['REMOTE_ADDR']);
$records = intval(file_get_contents('ips/' . implode('/', $ip_parts)));
if($records > 50) {
echo 'Error - credits used.';
} else {
// Do something
}
With the 1.txt file containing the needed data. Would I encounter issues with the number of files or folders causing this method to be slower than a database?
Check out this question / answer on ServerFault regarding DBMS optimization. You’re trying to reinvent the wheel for no particular reason (unless you won’t ever use MySQL on that VPS). And, while on the subject of storing IP addresses in MySQL, take a look at the INET_ATOI() function for MySQL.