Can anyone help the noob?

$logfile= 'log.txt';
$IP = $_SERVER['REMOTE_ADDR'];
$logdetails=  date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].' target=_blank>'.$_SERVER['REMOTE_ADDR'].'</a>';
$fp = fopen($logfile, "a"); 
fwrite($fp, $logdetails);
fwrite($fp, "<br>");
fclose($fp);

As you probably know the code writes the text, IP address, and html code to a text file in chronological order. I would like it to list the events in reverse-chronological order. I muddled with it for a while, trying to make the php write to the beginning_of_file, but anything that was left in the text file was overwritten. I thought perhaps I could make the php read the text file backwards, but that didn’t work. So I’ve tried. Any help would be greatly appreciated!

Thanks,

J_

Hi J_

fopen allows you to append contnet to the beginning of the file by supplying 'r+' as the second param.

Something like:

$logfile= 'log.txt';
$IP = $_SERVER['REMOTE_ADDR'];
$logdetails=  date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].' target=_blank>'.$_SERVER['REMOTE_ADDR'].'</a>';

// open the file for reading and writing
$fp = fopen($logfile, "r+");
// write out new log entry to the beginning of the file
fwrite($fp, $logdetails, strlen($logdetails));
fclose($fp);

A few suggestions:

Usually you would not want to add formatting (html or any other) into a log file, or file of the sort.

ie:

$logdetails=  date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].' target=_blank>'.$_SERVER['REMOTE_ADDR'].'</a>';

What you would wnat to do is add just the data, and seperate each new entry either with a new line, or some other delimiter.
This way, you actually avoid having to write your content in any order.
You can format and sort the content in any way you want when you read your logs with php.
It also ensures that the data will be small and access to the file will be faster.

A simple example:

$logfile= 'log.txt';
/*
Your log could have a format like:
date : IP \n
date : IP \n
*/

// get log contents, trim starting, ending \n
$contents = trim(file_get_contents('log.txt'));

// create an array with each log entry using \n as seperator
$contents_arr = explode("\n", trim($contents));

// you can now apply any sorting you want to the array
// eg: in reverse order
if (is_array($contents_arr));
rsort($contents_arr);

// set count to var to speed up for loop
$count = sizeof($contents_arr);
// check to make sure you have contents
if ($count > 0) {

    // loop through our array of log entries
    for ($i = 0; $i < $count; $i++) {

        // format our log entries any way you want here
        $log_entry = &$contents_arr[$i];
        // create an array of each log entry field with : as seperator
        $log_entry_fields = explode(':', $log_entry);

        // write formatted html to page

        echo 'time: '.date("F j, Y, g:i a", $log_entry_fields[0]).' IP: '.'<a href="http://dnsstuff.com/tools/city.ch?ip='.urlencode($log_entry_fields[1]).'" target="_blank">'.$log_entry_fields[1].'</a><br />';

    }
}

You dont even want to format the time. You can always format it when you read the log. And conversion to a differnet format is easy.

:confused:

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.