----- Original Message ----- From: <[EMAIL PROTECTED]> > 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>
Hi Rob, Wow, I wasn't expecting you to go to so much trouble, and it's much appreciated. There's certainly a lot for me to learn from this, from file handling to the way you've organised it. I'm still looking at it in amazement! It works a treat and the layout and use of functions is great, making it easier to understand. Also, the http-equiv="refresh" is a neat trick for testing it. I'm slowly realizing not to load a file or array with the data, and work directly on the files. It's finally sunk in. It's not normally the done thing to give someone the complete code, but it would have taken a boat load of explaining on how to do it (well in my case, grin!). This line threw me for I while: $trash = fgets($log_file); as $trash isn't used anywhere. But, then I realised this is how you were finding the record line ending, which moves the file pointer to it. That's a really cool way of doing it! I can see now why I couldn't get flock to work correctly, as I'd got it all wrong. I noticed you didn't use flock($handle, LOCK_UN), so is this not needed in this case? I'm still trying to understand the use of a reject.loc ? $sleep = rand(1, 10000); $time .= $sleep; usleep($sleep); I haven't work out why the random sleep yet? I think you maybe doing something crafty that I'm missing? Many thanks for this, Bob E. ------------------------------------ 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/