Page 1 of 1
Custom error log
Posted: Tue May 20, 2003 9:50 am
by mikusan
Hi, i have recently been cracking my head at making my own error function, and although i feel i have it all right, it is not exactly behaving as though "allright"

well... here is the code...if someone has suggestions or can see a flaw in my code...
Code: Select all
function site_log($error)
{
if ($file = fopen('/home/mysite/site_log.txt' , 'a'))
{
//Display Date/Time
$dt = getdate();
$month = $dt['month'];
$wday = $dt['wday'];
$year = $dt['year'];
$hours = $dt['hours'];
$minutes = $dt['minutes'];
$seconds = $dt['seconds'];
$date = '[' . $year . '-' . $month .'] - ' . $wday . ' - ' . $hours . ' '
. $minutes . ' ' . $seconds . ' -> ';
fwrite($file, $date . $error . '\n');
fclose($file);
}
else
{
return(false);
}
}
Posted: Tue May 20, 2003 9:51 am
by mikusan
BTW... The file in question has CHMOD 664 while the folder 755...
Posted: Tue May 20, 2003 10:10 am
by DoraBear
That's good, but this code:
Code: Select all
<?php
//Display Date/Time
$dt = getdate();
$month = $dt['month'];
$wday = $dt['wday'];
$year = $dt['year'];
$hours = $dt['hours'];
$minutes = $dt['minutes'];
$seconds = $dt['seconds'];
$date = '[' . $year . '-' . $month .'] - ' . $wday . ' - ' . $hours . ' '
. $minutes . ' ' . $seconds . ' -> ';
?>
can be replaced by "date()" function,
which can work better.
Posted: Tue May 20, 2003 10:49 am
by mikusan
That's not the problem, it does not seem to write to the file, besides i wanted a customized output for the date function which i don't think is covered by just the date().
Although it does not write to the designated file still the idea is that all errors are logged in the manner
[Year - Month] - Weekday - Hours Minutes Seconds ->
I am not concerned with that just yet...i need to be able to see at least one log so that i can confirm my site_log function actually works...this has not been the case yet.
Posted: Tue May 20, 2003 4:52 pm
by scorphus
Your function is working.
sitelog.php:
Code: Select all
<?php
function site_log($error) {
if ($file = fopen('site_log.txt' , 'a')) { // note I changed this line, creating the file in the sitelog.php root dir
//...
}
site_log('This should be sent to file.');
?>
The script runs without error and creates the site_log.txt file:
Code: Select all
ї2003-May] - 2 - 18 23 24 -> This should be sent to file.\n
Note the \n at the end of the line. \n is parsed when it is between " like this "\n".
I think there is a permission problem. You said the file is 664 and the directory 755. So only the directory owner has write permission to create files in this directory.
Some answers should help:
- Which user/group your server runs as? (it is commonly nobody/nobody)
- Does this user have write premission to '/home/mysite/site_log.txt'?
- Do you think there is another thing that matters?
Regards,
Scorphus.
ps.: please excuse my english.
Posted: Tue May 20, 2003 6:46 pm
by mikusan
Awkward like many things on my website it fixed itself... but anyone know why the "\n" doen't quite work when it's written to the file... i mean it wrote it in as \n but no line break
on another note anyone know a good CHMOD tutorial available online... there are many but one that is worthwile reading and learning from...i am concerned with the good ways to do it that are secure at the same time ...thx
Posted: Tue May 20, 2003 7:13 pm
by mikusan
Oh i see, it doesn't like single quotes for the line breaks '\n'...hmmm weird...
Anyone know good sites?
Posted: Tue May 20, 2003 11:41 pm
by scorphus
Single quotes and double quotes work differently:
Code: Select all
<?php
echo ',\n'; // print ,\n
echo ",\n"; // print , and add a new line
$var = 'PHP rocks';
echo ',\n$var';
echo ",\n$var";
$var2 = ',\n$var -> just \$var';
echo $var2;
$var2 = ",\n$var -> \$var contents"; // note the \ to scape $
echo $var2;
?>
Above code's output:
Code: Select all
,\n,
,\n$var,
PHP rocks,\n$var -> just \$var,
PHP rocks -> $var contents
About chmod I think you could check out this article:
http://en.tldp.org/linuxfocus/English/J ... cle77.html
I hope it helps. Let us know if it doesn't.
Cheers,
Scorphus.