如何限制远程服务器对Image的请求

I have a free image upload center.Can I limit requests for files on my server from remote servers? How?

in example:

if test.png is requested more then 1000 per day, until next day requests not accepted or redirected to other path.

It is depended on your webserver.

Yes, you can store image lists in MySQL or handle Images true PHP, but if you just need simple control files request limitation, you can do it with your webserver, also sometimes with firewall !

NGINX as I know can limited the requests accepted for special folder.

Very interesting, perhaps what you can do is:

  1. Have a unique id for your images and store said ids in the database along with the count of requests for the current day
  2. Have your clients request your images via a handler, for example, www.yourserver.com/getImage.php?id=1234ABCD
  3. Check the count of requests from the database, if it is less than your limit, then read in the file from your database (for example file_get_contents(your_url_to_file) into a variable
  4. echo that variable and stop processing
  5. if it is more than the limit then do the same thing but instead read in a standard "over the limit" image

I hope this helps

I can suggest you, with this solution:

  1. Remove the request of direct access to image file to the server through htaccess
  2. Create a php page with a parameter of the image file name
  3. Store the IP addressed or Host and display the images through php script

Hope this options are useful to you, I am not attaching any code sample. Will help you afterwords.

That is possible. For every Image uploaded, have a column in the database called 'hits' or something like this.

Instead of directly serving the image, serve it trough a PHP file. Set the header content type to your image format update your counter .

A sample implementation could look like this. Note that this is not ready to use code, but you'll see yourself.

<?php

$image = $db->getWhere('images', array('img_id' => $_GET['img']));
if($image->hits >= 1000)
{
    header('location: /imageviewsexceeded.php');
    die();
}

header("Content-type: image/png");
$im     = imagecreatefrompng('images/'.$_GET['img'].'.png');
imagepng($im);
imagedestroy($im);

$db->query('UPDATE `images` SET `hits` = `hits`  + 1 WHERE `img_id` = ?', array($_GET['img']));

?>

You may want to customize your database connection and implement checks whether the image exists. But I hope it's a good place to go for you.

Happy Coding! :)