Search code examples
phploggingfwrite

PHP fwrite() not writing to log file


as one of my first PHP projects I'm creating an IP logging script that logs a user's IP address. For some reason my fwrite() function doesn't seem to be writing to my logfile.

Can someone help me out?

<?php
// IP Logger Script
// By Sam Lev
// sam@levnet.us
$iplogfile = 'iplog.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('m/d/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
chmod($iplogfile, 0777);
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$webpage.' '.$browser. "\r\n");
fclose($fp);
echo "IP ADDRESS: $ipaddress <br />\n";
echo "TIMESTAMP: $timestamp <br />\n";
echo "BROWSER: $browser <br />\n";
echo "Information logged to server. <br />\n";
?>

iplog.txt is still blank after running the script. Everything echos out fine.

Thanks


Solution

  • Your code checks out and it's a permissions issue.

    Either manually chmod your file to 0777

    or add chmod($iplogfile, 0777); after $fp = fopen($iplogfile, 'a');

    chmod is a standard server command which is not exclusive to PHP.