----- Original Message ----- From: "Bob"
Hi Rob, Yes, linux and php 4.4 but please don't install another version of php just for me, but thanks anyway. My site is a hobby site, but it seems to attract so much abuse. I've had quite a few DOS attacks, but the shared server is DOS protected (limited max per minute). My previous log managed to record 3 attempts per second with no trouble. This stops my visitors seeing the site though, but the max they can afford to keep this up is usually about 2 hours, or their other spam doesn't get delivered. It's just a simple log file I'm trying to create, and it's just to record the failed attempts to spam me. It's not a full blow error log like the server logs provides. I get one genuine message per month (and did get over a100 spam attempts per day). Since I send the "Double Hit" and "Preset Post" messages etc. to a non-existant page, it's all more or less stopped now. This seems to really bugger up their spam list program. I'm not sure whether eveyone knows what a Double Hit is, but it defeats cookie or session protection. No idea how they do it though? In the past 2 days, I've had only 3 Double Hits, 2 Preset Posts, and 3 Profanity errors. Sending them to a non-existant site is causing them to get really annoyed and *try* to swear at me. I originally created a rolling road log (using a array <yes, bad idea>) to display to the spammer his errors, with an arrow pointing to his latest attempt. This drove them crazy and they tried to bring my site down. If I put my nice CATCHA image back on, it would all stop, but I like the challenge, he-he! Re: You are still reading the whole file into memory. If that's what $logdata = file_get_contents($logfile); does, then yes. If I don't, how can I find half of it, then find the next line break to write to the updated file? Re: File locking is only required if more then one script (or thread) can access the log files. I didn't know that, so it isn't needed in my case. Thanks, Bob E. ------------------------------------ Hi Bob, Too late lol, I had already installed php 4.4.4 onto Apache 2.0.63 I have a tested and working script below. I didn't know if you wanted oldest first or newest first. It is not hard to reverse it. flock has issues on some servers that you need to get around by opening the file in 'w' or 'w+' to get around. I used a different way of using a separate lock file altogether. -- <?php // Random test errors $error = array('Double Hit', 'Preset Post', 'No Session', 'Spam Detected', 'Profanity Found'); $fault = $error[array_rand($error)]; $record = date('d-m-Y H:i:s')." [$fault]\n"; $log_filename = 'reject.log'; $backup_filename = 'reject.bak'; $temp_filename = 'reject.tmp'; $lock_filename = 'reject.loc'; $max_filesize = 1000; $error_reporting = TRUE; if(!file_lock($lock_filename)) { die(); } if(!file_exists($log_filename)) { if(file_exists($backup_filename)) { rename($backup_filename, $log_filename); } else { $handle = file_open($log_filename, 'w'); fclose($handle); } } if(!($log_file = file_open($log_filename, 'r'))) { die(); } if(!($temp_file = file_open($temp_filename, 'w'))) { die(); } if(filesize($log_filename) > $max_filesize) { fseek($log_file, -$max_filesize, SEEK_END); $trash = fgets($log_file); } file_passthrough($log_file, $temp_file); fwrite($temp_file, $record); fclose($log_file); fclose($temp_file); @unlink($backup_filename); rename($log_filename, $backup_filename); rename($temp_filename, $log_filename); function file_open($filename, $mode) { if(!($handle = @fopen($filename, $mode))) { throw_error("Unable to OPEN file [" . $filename . "]"); return FALSE; } else { return $handle; } } function file_passthrough($source_handle, $destination_handle) { while(!feof($source_handle)) { $buffer = fread($source_handle, 4096); fwrite($destination_handle, $buffer); } } function file_lock($lock_filename) { if(!($handle = file_open($lock_filename, 'w'))) { return FALSE; } $max_lock_time = 10000; // 10 seconds while($time < $max_lock_time) { if(flock($handle, LOCK_EX)) { return TRUE; } $sleep = rand(1, 10000); $time .= $sleep; usleep($sleep); } throw_error("Unable to LOCK lock file [" . $lock_filename . "]"); return FALSE; } function throw_error($string) { global $error_reporting; if(!$error_reporting) { return; } echo($string . "<br>\n"); } ?> <html> <head> <title>Log file test</title> <meta http-equiv="refresh" content="1"> </head> <body> <?php echo("Log file size = " . filesize($log_filename) . "<br>\n"); $log_file = @fopen($log_filename, 'r'); while($one_line = fgets($log_file)) { echo($one_line . "<br>\n"); } ?> </body> </html> ------------------------------------ Please remember to write your response BELOW the previous text. Community email addresses: Post message: php-list@yahoogroups.com Subscribe: [EMAIL PROTECTED] Unsubscribe: [EMAIL PROTECTED] List owner: [EMAIL PROTECTED] Shortcut URL to this page: http://groups.yahoo.com/group/php-listYahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/php-list/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/php-list/join (Yahoo! ID required) <*> To change settings via email: mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/