求助:为已购PHP文件管理器添加用户每日下载次数限制
Hey there! I see you need to add a daily download limit feature to your PHP file manager, and since you're solid on HTML but new to PHP, let's break this down step by step. We'll extend the existing Downloader class you provided to track and enforce daily download counts per user.
Step 1: Add a Daily Limit Check Method
First, we'll add a new method to the Downloader class that checks if the user has exceeded their daily download quota. This method will handle resetting counts for new days, checking against the limit, and updating the user's download stats.
Add this inside the Downloader class (place it before the download method for organization):
/** * Check if user has exceeded their daily download limit * * @return bool True if download is allowed, false if limit is reached */ public function checkDailyDownloadLimit() { global $gateKeeper, $setUp; // Fetch existing user download stats (default to 0 if not set) $dailyDownloads = (int)$gateKeeper->getUserInfo('daily_downloads') ?: 0; $lastDownloadDate = $gateKeeper->getUserInfo('last_download_date') ?: date('Y-m-d'); // Reset download count if today is a new day if ($lastDownloadDate !== date('Y-m-d')) { $dailyDownloads = 0; $gateKeeper->updateUserInfo('last_download_date', date('Y-m-d')); } // Get daily limit from config (fallback to 5 if not configured) $dailyLimit = (int)$setUp->getConfig('daily_download_limit') ?: 5; // Block download if limit is reached if ($dailyDownloads >= $dailyLimit) { return false; } // Increment count and save to user info $gateKeeper->updateUserInfo('daily_downloads', $dailyDownloads + 1); return true; }
Step 2: Enforce the Limit in the Download Method
Now we need to trigger this check before allowing any download. Modify the download method to run the limit check first—if the user has hit their quota, we'll send a 403 Forbidden response and stop the download process.
Update the download method like this:
public function download( $file, $filename, $file_size, $content_type, $disposition = 'inline', $android = false ) { // Check daily download limit before proceeding if (!$this->checkDailyDownloadLimit()) { header("HTTP/1.1 403 Forbidden"); echo "Sorry, you've reached your daily download limit. Please try again tomorrow."; // You can replace the plain text above with custom HTML to match your manager's design return false; } // Rest of the existing download code remains unchanged // Gzip enabled may set the wrong file size. if (function_exists('apache_setenv')) { @apache_setenv('no-gzip', 1); } if (ini_get('zlib.output_compression')) { @ini_set('zlib.output_compression', 'Off'); } @set_time_limit(0); session_write_close(); header("Content-Length: ".$file_size); if ($android) { header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"".$filename."\""); } else { header("Content-Type: $content_type"); header("Content-Disposition: $disposition; filename=\"".$filename."\""); header("Content-Transfer-Encoding: binary"); header("Expires: -1"); } if (ob_get_level()) { ob_end_clean(); } readfile($file); return true; }
Step 3: Configure the Daily Limit (Optional but Recommended)
To make the limit adjustable without editing code, add a new setting to your file manager's configuration file (usually located in etc/config.php):
// Add this line to set your desired daily download limit (change 5 to any number) $setUp->setConfig('daily_download_limit', 5);
Quick Notes for Your Setup
- User Info Storage: This code assumes your
$gateKeeperclass has anupdateUserInfomethod to save download stats. If it doesn't, adjust this part to match how user data is stored (e.g., using$_SESSIONfor session-based users, or direct database queries if user data is in a DB). - Anonymous Users: If you need to limit unlogged users, track counts using their IP address stored in a session or database instead of user-specific info.
- Error UI: Since you're comfortable with HTML, feel free to replace the plain error message with a styled HTML template that fits your file manager's look.
That should implement the daily download limit you need! This setup will automatically reset counts at midnight each day and block downloads once the quota is hit.
内容的提问来源于stack exchange,提问作者Balder Bauer

