0

I have a small website keeps several pdfs, but to prevent having all of the pdfs slurped up by someone's script (and to keep track of popularity of each file), I am requiring that people enter the name of the pdf (which they will already know) into a PHP input box. Still, someone could write a script that sends repeated queries to the input box, and bog down my connection. Is there any way to limit the number of search queries of an IP to a certain number per day? For instance, I'd like to prevent users from querying more than 25 times in any 24 period, can this be set up either with a plugin/widget/other?

EDIT: Per request, I am adding the PHP code that I have thus far (I've NEVER coded PHP before, please don't hate!):

<?php
if (isset($_POST['boxname']))
{
$start = microtime(true);
//Store query
$name = $_POST['boxname'];
//Display query from text box
echo "Searching for " . $name . "\n";

$counted = array_count_values(file('./log.txt'));

arsort($counted);

foreach($counted as $person => $count)
    echo "{$person} : {$count}<br />\r\n";

if ($count < 25)
{
//Store current date/time of server and display
date_default_timezone_set('America/Chicago');
$datetime = date('l, F j, Y \a\t G:i:s');
//Display current date/time of server
echo 'Server date and time is: ';
echo $datetime . "\n";

if (!file_exists("/var/www/wordpress/" . $name)) 
{
wp_redirect( "/var/www/wordpress/" . $name );
exit();
}
else
{
echo "File does not exist.";
}
}

//Store guest IP address and display
$ip = $_SERVER['REMOTE_ADDR'];
//Display guest IP address
echo $ip . "\n";

$end = microtime(true);

//Append query, date/time, IP, and execution time to log file
$logfile = "./log.txt";
file_put_contents($logfile, $datetime . ", " . $ip . ", " . $name . ", " . round($end - $start, 4) . "\n", FILE_APPEND);
}
?>
  • Yes, you can do this. If PHP + MySQL can do it, it can be done in WordPress. Have you made any attempt to write the code? While someone here may feel like writing it for you from scratch, I don't typically have that much time. And given the sparsity of information in your question there would be a lot of guessing. – s_ha_dum Jun 17 '15 at 16:55
  • @s_ha_dum I've written the PHP code to handle the requests to find and serve the file, but I haven't been able to find anything in PHP (or Wordpress) documentation on how to do query/search limits. I did try logging the IP address and then checking new IPs against the log, however this didn't seem to work. I wasn't sure how to code the limit attempts per day. Any suggestions on where I might look to learn how to do this? – iwantmyphd Jun 17 '15 at 18:39
  • 1
    You were on the right track. You need to log the IP addresses and check against them. That is the only code that matters for this question. – s_ha_dum Jun 17 '15 at 18:49
  • @s_ha_dum Cool, then how do you limit it over a specified time? – iwantmyphd Jun 17 '15 at 18:50
  • Both PHP and MySQL can do date math. It would really help to see the code that retrieves the data, and hopefully there is reason enough to consider the question WordPress specific. – s_ha_dum Jun 17 '15 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.