Reputation: 283
So I am working on a error page and I need it to log who goes to that page, when they go, what page they are accessing, and what IP they are coming from. I have successfully made it so that everything is logged to a text file in the root directory in the proper format. The only problem is that it keeps over writing what it had written the last time the page was opened rather than writing to the next line.
If anyone is wondering, this is just a test page and in reality, this specific page is copied into multiple directories that I don't want people seeing the contents of.
Here is my code.
<?php
$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$ip = $_SERVER['REMOTE_ADDR'];
$path = $_SERVER['REQUEST_URI'];
$txt_info = "Date (DD/MM/YYYY): $date/$month/$year Time (HH/MM/SS): $hour:$min:$sec IP: $ip URL: $path";
$content = "$txt_info";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/Access_Logs.txt","wb");
fwrite($fp,$content . "\n");
fclose($fp);
?>
If anyone could let me know what is happening, please let me hear what you have.
Upvotes: 0
Views: 650
Reputation: 723
Try
fopen($_SERVER['DOCUMENT_ROOT'] . "/Access_Logs.txt","a");
Notice the 'a' at the end. That will append to the file and not re-write it.
http://www.php.net/manual/en/function.fopen.php
Upvotes: 1