Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/253328-writing-log-file/
Share on other sites

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);

 

Link to comment
https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298655
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298656
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298661
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298666
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/253328-writing-log-file/#findComment-1298676
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.