brianjw Posted September 28, 2008 Share Posted September 28, 2008 Hi, I apologize if this is the wrong place to post this thread. I have created a log file for a php proxy script. The log is being generated by using this code: $proxytime = date('h:i:s A'); $proxydate = date('d-m-Y'); $stuff = $_SERVER['REMOTE_ADDR'].",".$url." , ".$proxydate." , ".$proxytime."\n"; $fp = fopen('log.txt',"a"); fwrite($fp,$stuff); fclose($fp); I need to know how I can get the log to be limited to only 100 entries or something and then the oldest log would disappear, if you know what I mean. How would I do such a thing? Would it need to modify this code or make a shell? Please understand that I am a huge noob with php, shells, cronjobs, etc. lol and will probably need someones assistance who is willing to explain it to me. Thank you. Brian Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/ Share on other sites More sharing options...
Adam Posted September 28, 2008 Share Posted September 28, 2008 Using a database would be simpler for limiting the entries, if you have one available too you? Adam Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-652573 Share on other sites More sharing options...
brianjw Posted September 28, 2008 Author Share Posted September 28, 2008 Yes, I have a database. However the proxy doesn't use databases so... If it is possible to some how limit it without using a database that would be awesome. I did see a comment here: http://luiscosio.com/add-url-logging-to-phproxy-05b2#comment-14292 Would that work? I have no idea how to set it up as they didn't provide clear instructions. And would that limit it only to 100 entries? Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-652578 Share on other sites More sharing options...
Adam Posted September 28, 2008 Share Posted September 28, 2008 That just moves all the log entries up a file.. like from file 1 to 2.. file 2 to 3 etc.. however frequently you set up the cron to run. There's nothing limiting the logs to 100 entries though.. I'm a little unsure myself about how you could do it.. Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-652584 Share on other sites More sharing options...
brianjw Posted September 29, 2008 Author Share Posted September 29, 2008 Ok, I will wait until someone who does know then. Thanks for posting though. Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-652637 Share on other sites More sharing options...
brianjw Posted October 8, 2008 Author Share Posted October 8, 2008 BUMP ;) Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-660327 Share on other sites More sharing options...
brianjw Posted October 17, 2008 Author Share Posted October 17, 2008 Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-668283 Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 You can use file to read the contents of the file into an array (each line is an element), and array_unshift to prepend an element to that array (insert the new line at the beginning, if you are wanting newest -> oldest) or array_push to put it on the end of the array (insert new line at end, if you are wanting oldest -> newest). From there, if this script is only being called every $x time, you can use array_slice to get the first or last 100 elements. If the script is being triggered every request or something, you can use an array_shift/array_push combo to take first element off while putting new one on end, or use an array_pop/array_unshift combo to chop off the last element while putting new on at the beginning. From there, all you have to do is run a loop to write each element of the array back to the file (don't forget to add the line breaks to the element you are unshifting or pushing). Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-668298 Share on other sites More sharing options...
brianjw Posted October 26, 2008 Author Share Posted October 26, 2008 Thank you for the explanation. However, everything you said is like a foreign language to me. If you could explain it in NOOB terms or something then it would be great. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/126200-how-would-i-get-this-log-to-automatically-clear-or-be-limited-to-how-many/#findComment-675117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.