Hi all,
I posted this message earlier and couldnt get a working coding to my problem. I am trying to email a log and at the same time log the file in an xml format.
The code below shows how it works however i keep on getting the message that the top tag is missing or not created. Can any one detect the problem why and is this code efficent ?.
Cheers
Hussam
<?php
if (!(getenv("HTTP_X_FORWARDED_FOR"))) {
$ip= getenv("REMOTE_ADDR");
} else {
$proxy_address= getenv("REMOTE_ADDR");
$ip= getenv(HTTP_X_FORWARDED_FOR);
}
$host = gethostbyaddr (getenv("REMOTE_ADDR"));
$ip= getenv("REMOTE_ADDR");
$dt= date("D F j, Y, g:i:s a");
$to= "email@email.com\n";
$headers .= "From: $host <someone@someone.com>\n";
$headers .= "X-Sender: $host <halasad@blanketware.com>\n";
$headers .= "X-Mailer: PHP ". phpversion() ."\n";
$headers .= "X-Priority: 1\n";
$subject = "Error Notification from $ip";
$body = "Error generated at $ip on $dt.";
mail($to, $subject, $body, $headers);
$xml_string =
"<report>
<host>$host</host>
<ipaddress>$ip</ipaddress>
<errmsg>$subject</errmsg>
<date>$dt</date>
</report>
";
$pagename = "errorlog.xml";
if(!file_exists($pagename)) {
touch($pagename);
$fp=fopen($pagename, "a+");
fwrite($fp, "<?xml version='1.0'?><errorlog></errorlog>");
}
$fp=fopen($pagename, "w+");
$pos = strlen($fp)- 11;
$log = fread($fp , $pos);
$newlog = $log . $xml_string . "</errorlog>";
fwrite($fp, $newlog);
fclose($fp);
?>