Flat log file
I'm currently wanting to learn about flat file databases so decided to make a flat file log.
I have the following code:
<?php
$file = \"log.txt\";
$filetr = fopen($file, \"r+\");
$log_string = array(
gethostbyname($_SERVER['HTTP_HOST']),
date(\"F j Y\"),
date(\"g:i A\"),
$_SERVER['HTTP_REFERER'],
$_SERVER['PHP_SELF'],
$_SERVER['HTTP_USER_AGENT']
);
$data = implode(\"~\", $log_string);
fwrite($filetr, $data);
$filetr = fclose($filetr);
?>
I would like it to write to the beginning of the file pushing the next one down, however, it just overwrites.
Some help please?
Renegade posted this at 01:17 — 21st September 2003.
He has: 3,022 posts
Joined: Oct 2002
I figured it out now, heres the code that I used:
<?php
$file = \"log.txt\";
$filetr = fopen($file, \"r+\");
$data_old = fread($filetr, filesize($file));
$filetr = fclose($filetr);
$filetr = fopen($file, \"w+\");
$log_string = array(
gethostbyname($_SERVER['HTTP_HOST']),
date(\"F j Y\"),
date(\"g:i:s A\"),
$_SERVER['HTTP_REFERER'],
$_SERVER['PHP_SELF'],
$_SERVER['HTTP_USER_AGENT']
);
$data_new = implode(\"~\", $log_string);
$data = $data_new . \"\n\" . $data_old;
fwrite($filetr, $data);
$filetr = fclose($filetr);
?>
Renegade posted this at 01:32 — 23rd September 2003.
He has: 3,022 posts
Joined: Oct 2002
I'm using
<?php
gethostbyname($_SERVER['HTTP_HOST'])
?>
to get an IP but I realised that I was getting the IP of my host, how do I get the IP of the viewer of my site?
Abhishek Reddy posted this at 04:36 — 23rd September 2003.
He has: 3,348 posts
Joined: Jul 2001
http://www.php.net/manual/en/reserved.variables.php#reserved.variables.server
Renegade posted this at 10:57 — 23rd September 2003.
He has: 3,022 posts
Joined: Oct 2002
Thanks.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.