PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
How do I go about writing to one line of a file for one piece of information and then the next piece of information is on the next line. Can you make it so the info gets written at the same time or do you have to use multiple fwrite functions.
My current file writing situation is as follows:
<?php
$filename = 'admin_access.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when i fwrite() it.
if (!$handle = fopen($filename, 'a+')) {
print "This area is currently not being monitored";
exit;
}
// Write $somecontent to our opened file.
if (!fwrite($handle,"$time $date --- $_SESSION[username] Was viewing the admin login screen\n")) {
print "Cannot write to file ($filename)";
exit;
}
print "Your actions are being recorded, this is for security";
fclose($handle);
} else {
print "The file $filename is not writable";
}
?>
just so you know this is what it outputs for me (notice new line) works on both linux and windows
3:47pm 28 January 2004 --- malcolmboston Was viewing the admin login screen
12:24pm 29 January 2004 --- malcolmboston Was viewing the admin login screen
5:15pm 29 January 2004 --- malcolmboston Was viewing the admin login screen
5:16pm 29 January 2004 --- malcolmboston Was viewing the admin login screen
2:20pm 30 January 2004 --- malcolmboston Was viewing the admin login screen
2:20pm 30 January 2004 --- malcolmboston Was viewing the admin login screen
4:40pm 2 February 2004 --- malcolmboston Was viewing the admin login screen
3:10pm 3 February 2004 --- malcolmboston Was viewing the admin login screen
12:18pm 4 February 2004 --- malcolmboston Was viewing the admin login screen
12:18pm 4 February 2004 --- malcolmboston Was viewing the admin login screen
12:21pm 4 February 2004 --- malcolmboston Was viewing the admin login screen
2:58pm 6 February 2004 --- malcolmboston Was viewing the admin login screen
2:59pm 6 February 2004 --- malcolmboston Was viewing the admin login screen
10:32am 9 February 2004 --- malcolmboston Was viewing the admin login screen
10:52am 9 February 2004 --- malcolmboston Was viewing the admin login screen
11:21am 15 February 2004 --- malcolmboston Was viewing the admin login screen
6:35pm 18 February 2004 --- malcolmboston Was viewing the admin login screen
thats not quite what I mean, the file it writes to is time().txt so it is always different. So how could I say in the first example above, get the 3:47pm to be on the first line and then on the next line have the 28 Jan 2004 then the next line malcomboston etc.
Most text editors these days recognise either \r\n or \n as a new line however, if you still don't see new lines then try replacing the \n above with \r\n you don't require the \n at the end of the call as this is handled by the function.