pranshu82202 Posted December 16, 2011 Share Posted December 16, 2011 I have a code for writing a log file... The code is working fine but it inserts the new details at the bottom of the file but i want to insert in the top... So what function should i use... Here is the file my_log.php <?php $usname = $_SESSION['usname']; date_default_timezone_set('Asia/Calcutta'); $date = date("l dS \of F Y h:i:s A"); $file = "log.php"; $open = fopen($file, "a+"); fseek($open,289); fwrite($open "<b><br/>USER NAME:</b> ".$usname . "<br/>"); fwrite($open, "<b>Date & Time:</b> ".$date. "<br/>"); fwrite($open, "<b>What have they done :</b> ".$reason . "<br/><br/>"); fclose($open); ?> and here is my log.php file : <?php session_start(); if($_SESSION['stage']!=1 || $_SESSION['stage2']!=2) {header('location:index.php'); die(" "); } ?> <?php if($_GET['valu']=="view_log") { $reason=" ".$_SESSION['usname']." Viewed the LOG"; include('my_log.php'); header('location:log.php'); die(""); } // bytes till here are 289 //LINE 1 : Log details have to insert here I want to insert every detail from the top of the page but just below the php code.. What should i do... ANy helo would be appreciated Pranshu Agrawal pranshu.a.11@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/ Share on other sites More sharing options...
kicken Posted December 16, 2011 Share Posted December 16, 2011 You should write to a file without any PHP code, then prepending the contents would be simply: $logFile = 'log.txt'; $newContents = 'blah blah blah'; $oldContents = file_get_contents($logFile); file_put_contents($logFile, $newContents, $oldContents); Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298655 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 It seems that you'd better change your logic... 1. Write log to a separate file. 2. Write include('this_separate_file.log') in the log.php at the bottom, at the place where you are trying to write log info, instead of this log info. 3. White log info in any sequence as you wish. 4. Enjoy Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298656 Share on other sites More sharing options...
pranshu82202 Posted December 16, 2011 Author Share Posted December 16, 2011 Kicken that was nice... But i need to keep those php code... What shuld i do for that ... ??????????? Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298657 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 But i need to keep those php code... What shuld i do for that ... ??????????? ...read my previous answer.... And do what I said Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298659 Share on other sites More sharing options...
pranshu82202 Posted December 16, 2011 Author Share Posted December 16, 2011 Thanks for responding SergeiSS, but i didnt get you.... Even according to you when i write the log details in my_seperate_file.php i would be writing them at th bottom of that file.... But i need to write them at the top...... And also i want to keep some php check on that file .... so i need to have php code even on that my_seperate_file.php... Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298661 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 You may put include() in any part of you PHP file! The main idea is that you create this php file one time and then never rewrite it. Instead of rewriting php file you rewrite log file only. Every time when php file is loaded it includes the current version of log file. Next time an updated log file will be called. And once more: place include('log_file.log'); anywhere you wish. I mean that you have to look on a problem from another point of view. And you would find that my idea solves the task that you asked - as I think, of course Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298666 Share on other sites More sharing options...
ManiacDan Posted December 16, 2011 Share Posted December 16, 2011 There's a reason all logs are written at the end: Writing to the end of a log file is much faster and easier than writing to the beginning. The best solution is to make all your log entries a single line, then use a unix command like tail to view only the bottom of the file. The best way to solve your problem is: why do you want your log file in reverse-chronological order? Also, you can't do what kicken suggested (hi kicken!) because (a) log files of significant size will overflow your memory and (b) more than one simultaneous write request will ruin this file. Quote Link to comment https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298676 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.