Php Access Log In Reverse Order - Request For Help.

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Php Access Log In Reverse Order - Request For Help.

vujsa
So I need help getting data entered into my log correctly. I want the newest entry to be at the beginning (top) of the log instead of at the end (bottom).

Here's what I have:
CODE
<?php
function access_log(){  // Enter data in usage log.
$filename = "access.log";
$entry = gmdate("M d, Y H:i:s T").": ". getenv("REMOTE_ADDR").": ". getenv("HTTP_USER_AGENT")." \n";
fwrite(fopen($filename, "a"), $entry);
fclose(fopen($filename, "a"));
}  //  End function access_log()
?>


And it outputs:
Mar 29, 2005 07:57:16 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 07:57:40 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 07:58:03 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 08:02:01 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 08:02:04 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 08:02:07 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 08:02:09 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)

Notice how the newest entry is at the bottom.

I need the data to read like this:
Mar 29, 2005 08:02:09 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 08:02:07 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 08:02:04 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 08:02:01 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 07:58:03 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 07:57:40 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Mar 29, 2005 07:57:16 GMT Standard Time: 192.168.1.1: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)


I've tried:
CODE
fwrite(fopen($filename, "r+"), $entry);
Set pointer to the beginning.
But that erases the first line when a new entery is added.

Please let me know how to adjust the code to insert an entry at the beginning.

Thanks, sad.gif
vujsa

 

 

 


Reply

Muddyboy
CODE
<?php
function access_log(){  // Enter data in usage log.

$filename = "access.log";
$entry = gmdate("M d, Y H:i:s T").": ". getenv("REMOTE_ADDR").": ". getenv("HTTP_USER_AGENT")." \n";

if (!file_exists($filename)) touch($filename);
$fh = fopen($filename, "r");
$fcontent = fread($fh, filesize($filename));

$towrite = $entry.$fcontent;

$fh22 = fopen($filename, 'w+');
fwrite($fh2, $towrite);
fclose($fh);
fclose($fh2);

}  //  End function access_log()


There ya go smile.gif

 

 

 


Reply

vujsa
Hey thanks Muddyboy.

I was afraid I'd have to do it that way. I was hoping that there was a pointer I didn't know about that would simply insert an entry before instead of overwritting the first line.

Just to make sure I'm reading this correctly and well recap for those that may have trouble reading it;
  1. Create a new entry and assign the variable $entry to it
  2. Open the file for use or create a new file if none exists.
  3. Read the current log and assign the variable $content to it.
  4. Create the new log as $entry + $content and assign the variable $towrite to it.
  5. Write the variable $towrite to the file.
  6. Close the file

Thanks a lot,
vujsa cool.gif

Reply

Muddyboy
Yer i don't think there is a way to add it too the top, if there is i dont know it tongue.gif.

You could on loading the file put the file into an array and reverse the array.

I remember seeing a function for that

Your recap is right tongue.gif
Although it proberly would be much better to use a mysql database, but you cant always have access to a database sad.gif

Reply

vujsa
QUOTE(Muddyboy @ Mar 30 2005, 02:11 AM)
Although it proberly would be much better to use a mysql database, but you cant always have access to a database sad.gif
*



Well, MySQL seemed like overkill for such a simple log file. But yes, you're right, sorting and viewing the data would be better in MySQL. Maybe the reason PHP is lacking a pointer to do what I wanted is because PHP was designed to work with database engines like MySQL. Of course, the reason I didn't know how to manipulate a simple text file was because I learned PHP with MySQL. laugh.gif

Thanks for the input.

vujsa

Reply

mastercomputers
I just wanted to fix up the above a bit and make it more functional with more error checking and using the global function $_SERVER instead of getenv, since it works better.

CODE

<?php
function access_log($log, $new_entry)
{
if(!file_exists($log))
 touch($log);

if(!$fh = @fopen($log, 'r'))
 die('Error: Failed to open file for reading.');

$old_entry = fread($fh, filesize($log));
fclose($fh);

$new_log = $new_entry . $old_entry;

if(!$fh2 = @fopen($log, 'w'))
 die('Error: Failed to open file for writing.');

fwrite($fh2, $new_log, strlen($new_log));
fclose($fh2);
}
$filename = 'access.log';
$entry = gmdate('M d, Y H:i:s T') . ': ' . $_SERVER['REMOTE_ADDR'] . ': ' . $_SERVER['HTTP_USER_AGENT'] . "\n";

access_log($filename, $entry);

?>



Just to explain it:

We'll skip the function for now and start with our filename and entry, this declares the name of our file and the new data to be inputted, which is a timestamp, IP address and browser agent information. This information then gets sent off in our function.

Our function access_log that will accept two inputs, the filename and the new data to be entered in.

We check if the file exists, if not we create it.

We then open the file for reading, if it fails, we exit the program leaving a message about it not being able to be opened for reading, if the error suggests it's reading we know where it's failing at for debugging.

We then store our old entries by reading it up to the last byte into a string.

We now close the handler since we no longer need to have the file opened.

We now create a new string using our new data and adding onto it the old data from the string we stored. If you're concerned about memory storage, we could unset the old_entry variable now that we've stored it in new_log.

Now we open a file for writing, and check whether it opens or not, if not, we exit the programing stating that the file could not be opened for writing, the error suggests it's the writing, so we know where it's failing if we need to debug the code.

We then write the new_log into our file, by using the new file handler we use for writing, we also state the length of what we're writing, not necessarily needed, but for binary safe methods, it is, and if you make this a habit, you'll not be wrong for whatever method you do use.

We then finish up by closing the function, it returns back to where the script left off, since there's nothing more left, the script has finished.

I too don't know of any other way of writing to the beginning of a file without overwriting it. Thought maybe writing a new file and adding the old file onto the end of it, but it's just the same as above without the extra file, also thought maybe a pattern match and replace method, but again, you'll be overwriting while still appending the old data on the end.

If anyone does come up with a better method, or some ideas that could work, It would be good to know.

Cheers,


MC

Reply

vizskywalker
Sorry vujsa, but I checked and rechecked the PHP documentation for you and couldn't find anything. THe method of opening files used for what you are talking about is the "random method" (that isn't what the "random" method was designed for, but it can be used for that) and PHP won't open files in random.

Reply

mastercomputers
Same code, different method, overboard with checking for problems.

Also, decided to remove adding the file if not found, due to the new file being created, it would be owned by the server's username than your user, which means if it's not owned by you, you'll have problems, you should instead manually create this file yourself and set the permissions.

Also checked whether the file is readabe/writable, if not you'll need to set permissions yourself as well.

Decided to change the function name to something more appropriate too and also display a success message when completed.

Changed the method, by reading the file in as an array too.

Similar code, overboard fail checking, same results.

CODE

<?php

function prepend($file, $data)
{
if(!is_writable($file) || !is_readable($file))
 if(!file_exists($file))
  die('Error: File not found (create file)');
 if(!chmod($file, 0666))
  die('Error: File not readable/writable (chmod 0666)');

if(!$old_data = file($file))
 die('Error: Storing array of file');

if(!$fh = fopen($file, 'w'))
 die('Error: Opening file for writing');

if(!fwrite($fh, $data, strlen($data)))
 die('Error: Writing to file');

foreach($old_data as $value)
 if(!fwrite($fh, $value, strlen($value)))
  die('Error: Restoring old data');

if(!fclose($fh))
 die('Error: Closing handler');
}

$filename = 'access.log';
$entry = gmdate('M d, Y H:i:s T') . ': ' . $_SERVER['REMOTE_ADDR'] . ': ' . $_SERVER['HTTP_USER_AGENT'] . "\n";

prepend($filename, $entry);
echo 'Updating file successful';
?>


I think PHP5 is more promising, and will definitely lessen the amount of code, but uncertain about the prepending to files, it still seems this is a problem.

Things that could be worked on is the type of entry data, checking the data, trimming spaces, escaping slashes, making sure that it is reading to the end of line, etc. Could possibly break it down into more functions, or even turn it into a class.

Cheers,


MC

Reply

vujsa
Wow mastercomputers,

I wasn't expecting so much information. Thanks for the hard work. Iwas surprised that you hadn't replied to my post sooner. I know you're a fan of PHP.

The first post you had is very similar to what I actually coded. Great minds I guess.

I simplified my example problem in order to avoid getting lost in variables. My log function actually passes about ten arguments.

Actually, I learned something really useful here. The touch() function. It reduced my code considerably.

Again, thanks for the information. It gives me ideas for other text files and logs. also, I'm going to replace my getenv with $_SERVER as you suggested.

vujsa

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Recent Queries:-
  1. accesslog fwrite - 108.29 hr back. (2)
  2. how to reverse a log function - 287.53 hr back. (1)
  3. php read log file reverse order html - 582.63 hr back. (1)
Similar Topics

Keywords : php, access, log, reverse, order, request

  1. Script Request
    script request (2)
  2. Wierd Problem With $_post/$_get/$_request
    (11)
    I'm having a bad problem and no idea how to fix it...it's setting my back on my PHP work. I
    have 7 if statements that do something depending on what $_POST is, it works for the first 6th and
    on the last one it adds a 1 (only ads the 1 at the end IF its the one in the if statement). Example
    of the failing line: CODE if ($_POST == 'yes' AND $_POST == 'lol' AND $_POST
    == 'cmd') { The other ifs are like that, just $_POST == other things. Those if
    statements work. It does this with $_GET and $_REQUEST. It does this no matter what....
  3. Connecting Ms Access To Php Using Odbc
    (5)
    Dear Friends I have been trying to connect Ms Access using PHP for couples of days. Finally I have
    done it. It was dome using Open DataBase Connectivity, popularly known as ODBC (pronounced as
    separate letters). With an ODBC connection, you can connect to any database, on any computer in your
    network, as long as an ODBC connection is available. Here is how to create an ODBC connection to a
    MS Access Database: Open the Administrative Tools icon in your Control Panel. Double-click on the
    Data Sources (ODBC) icon inside. Choose the System DSN tab. Click on Add in the....
  4. How To Do POP Access In PHP + Need AJAX Info
    (4)
    I'm writing a mail checker in PHP. I need to access the POP server. In PHP, I can send mail. But
    I didn't know how to receive mail yet. So, please help me. I also need some document about
    AJAX. This topic really interests me. But I don't know where to start.....
  5. Php Regular Expressions
    Request For Help. (11)
    So I'm trying to learn how to pull useful content from a web page. Here's what I got so
    far: CODE $filename = "http://www.forum500.com"; $html = file_get_contents($filename); /*
    **************************************************************** */ /*                              
                                       */ /*             Got this code @ http://www.php.net/            
         */ /*                                                                  */ /*
    **************************************************************** */ // $document should contain an H....
  6. PHP Based Site Access Authentication - Help
    How to block parts of your web-site ?? (4)
    How can i program my web page using php that when the value of the login box is equal to some string
    then go to my success.html otherwise on my fail.html????help me guys!
    ------------------------------------ It would help the readers far better to understand what your
    problem is - if you state the nature of it in short in your topic title, instead of just "Php help".
    It'll also get you a lot more responses. Am changing your topic title to give you an example.
    All the best /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> m^e....

    1. Looking for php, access, log, reverse, order, request






*SIMILAR VIDEOS*
Searching Video's for php, access, log, reverse, order, request
advertisement




Php Access Log In Reverse Order - Request For Help.