MP3 throttle using htaccess and php

I am being destroyed by robots and malicious users who are trying to delete my site.

I tried to make the bandwidth limiter mod_rewriting all requests for mp3 files in a php script that will track each ip and limit how much each can load per hour. This worked well, but for some users there were a lot of problems with the fact that the links were not direct links to mp3 files.

I tried all kinds of download files, but what worked for some users would not work for others.

Now the question arises: is it possible to maintain direct links to mp3 files and whenever someone clicks on an mp3 file to simultaneously run php script tracking that would allow or reject the request?

thanks!

(the server does not have mod_bandwidth or any other useful mod)

+4
source share
2 answers

instead of tracking usage at the same time when the user clicks on the link, why not make the landing page a PHP script that checks the user's ip and based on this displays links or hides them? if your files are stored in an obvious folder, for example. YourSite / music / artist / Song.mp3 you can create soft links for them, thereby confusing the path. on linux server, you can use the symlink () php function to create a soft link to the / music directory and display the path as your site /3awE2afeefef4a323/artist/song.mp3, which should still be a direct link to the file, for example:

<?php $target = 'music/'; $link = 'a322ewrr323211'; symlink($target, $link); ?> <html> <body> <a href="a322ewrr323211/artist/song.mp3">link</a> </body> </html> 

then periodically delete these symbolic links every night or 24 hours after creation.

+1
source

A mod_rewrite solution will be quite elegant, and direct links can be supported.

In .htaccess rewrite all .mp3 links to a PHP script:

 RewriteEngine on RewriteRule \.mp3$ download.php 

Inside the PHP file, we can extract the requested URI, check the user's IP address and return the appropriate headers based on this check.

 <?php // Set variables $requestedFile = trim($_SERVER['REQUEST_URI'], '/'); $ip = $_SERVER['REMOTE_ADDR']; $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'; // If the file does not exist, throw a 404 error if (!file_exists($requestedFile)) { header($protocol . ' 404 Not Found'); } // Is the user allowed to download? function validateDownload($ip) { /** * Put your IP tracking and validation code here, returning `TRUE` * or `FALSE`. */ return TRUE; } // Validate and perform appropriate action $canDownload = validateDownload($ip); if ($canDownload) { header('Content-Disposition: attachment; filename="' . basename($requestedFile) . '"'); header('Content-type: audio/mpeg'); readfile($requestedFile); } else { header($protocol . ' 403 Forbidden'); } 

Now all links remain direct, and you return the appropriate headers to the user agent, requesting a download or denied access.

+1
source

All Articles