FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need help with logger script. (half done)





[FuN]goku
Right someone that posts around in the php forums here gave me a part of a script and ive edited to fit what i need, but i need a bit more. Its getting annoying to clear my log file every few days so what i need is some code that will start a new log file every day or every 2 days and make a log file like
logs/nov.20.06-8.30.log
or something like that. What i mean is the log name will be the time and date when it creates a new log.

Help?
Thanks in advance.

Here is my current code.
Quote:

<?php
$time = gmdate("d M Y H:i:s T",time());

$page = $_SERVER['SCRIPT_NAME'];

$ip = $_SERVER['REMOTE_ADDR'];

$browser = $_SERVER['HTTP_USER_AGENT'];


if(strstr($ip,"XXX.")){
$ip = "Hidden";
$page = "Hidden";
$browser = "Hidden";
$os = "Hidden";
$time = "$time\noMejA has accessed part of the site, his info won't be shown.";
} elseif (strstr($ip,"XX.XXX.")) {
$ip = "Hidden";
$page = "Hidden";
$browser = "Hidden";
$os = "Hidden";
$time = "$time\nRyan has accessed part of the site, his info won't be shown.";
}else{
if(strstr($browser,"NT 5.1"))
$os = "Microsoft WindowsXP";

if(strstr($browser,"NT 5.0"))
$os = "Microsoft Windows2000";

if (strstr($browser, 'Linux'))
$os = "Linux";

if(strstr($browser,"MSIE 5.0"))
$browser = "Microsoft Internet explorer v5.0";

if(strstr($browser,"MSIE 5.5"))
$browser = "Microsoft Internet explorer v5.5";

if(strstr($browser,"MSIE 6.0"))
$browser = "Microsoft Internet explorer v6.0";

if(strstr($browser,"Mozilla/5.0"))
$browser = "Mozilla Firefox 5.0";

if(strstr($browser,"Mozilla/4.0"))
$browser = "Mozilla Firefox 4.0";

if(strstr($page,"/admin/"))
$page = "Warning: Someone has accessed Admin Panel";
}

$f = fopen("actions.log","a");
fwrite($f, "Time: $time\n");
fwrite($f, "Page: $page\n");
fwrite($f, "IP: $ip\n");
fwrite($f, "Browser: $browser\n");
fwrite($f, "Operating System: $os\n\n");
fclose($f);

?>
chris20
Here is something that will create you a new log file every day in the same format that you listed... nov.20.06.log, but it doesn't do the timestamp when it was created. The timestamp part seems useless anyway since it will always be the same time, whatever the start of the new day is, 12am usually. If you do need this though, just change the date() function to include the time, then fix the file_exists() function to use some kind of string check like strstr or something.

The script just checks if the file is already there, if it is already there, it opens it to append to it, if it doesnt exist, it opens it to write to it, thus creating it.

Hope it helps you, here it is....

Btw, I tested it all out and it worked for me, let me know if you have any issues. Also, keep in mind I wrote it under the influence of NyQuil.

<?php

$log_filename = date('M.d.y').'.log';
if (file_exists($log_filename)) {
// file exists so open the file up to append to it
$f = fopen($log_filename,"a");
} else {
// file doesn't exist so open the file up to write to it
$f = fopen($log_filename,"w");
}

fwrite($f, "Time: $time\n");
fwrite($f, "Page: $page\n");
fwrite($f, "IP: $ip\n");
fwrite($f, "Browser: $browser\n");
fwrite($f, "Operating System: $os\n\n");
fclose($f);

?>
[FuN]goku
chris20 wrote:
Also, keep in mind I wrote it under the influence of NyQuil.
LOL!!
EDIT : Getting this error.

Quote:

Warning: fopen(Nov.13.06.log) [function.fopen]: failed to open stream: Permission denied in /home/anzerou/public_html/l0gg3r.php on line 58

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 61

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 62

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 63

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 64

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 65

Warning: fclose(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 66
chris20
hmm, I think maybe you don't have access to use fopen to create new files, the files need to already be there in order for it to work for you

you will have to ask someone at the hosting place for the answer to that for sure though, if that is the case, you could just make your files prior to the script using them by just creating files called Nov.13.06.log, Nov.14.06.log etc etc etc so you won't get that permission denied error
Diablosblizz
Quote:
Warning: fopen(Nov.13.06.log) [function.fopen]: failed to open stream: Permission denied in /home/anzerou/public_html/l0gg3r.php on line 58

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 61

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 62

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 63

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 64

Warning: fwrite(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 65

Warning: fclose(): supplied argument is not a valid stream resource in /home/anzerou/public_html/l0gg3r.php on line 66


Try CHMODing the Files?
anilande
try this code. if it says 'unable to open file' then it means file do not have permission to get open by u.

<?php

$log_filename = date('M.d.y').'.log';
if (file_exists($log_filename)) {
// file exists so open the file up to append to it
$f = fopen($log_filename,"a") or die("unable to open file");
} else {
// file doesn't exist so open the file up to write to it
$f = fopen($log_filename,"w") or die("unable to create file");
}

fwrite($f, "Time: $time\n");
fwrite($f, "Page: $page\n");
fwrite($f, "IP: $ip\n");
fwrite($f, "Browser: $browser\n");
fwrite($f, "Operating System: $os\n\n");
fclose($f);

?>
Related topics
New server on DirectAdmin
php counter
Help with upload form script! [RESOLVED]
[*RESOLVED*] Please close for me.
What did you give up for lent?
a challenge?
Could somebody test out my order form?
The Middle East Conflict
Please help. php script installation error
GlacierBoard 0.5 Beta!
Please help about this..
[help] [tecg] script php para publicar en twitter
Worrying news about US Education.
[Help] php script to post to twitter
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.