# Relevant bit from the main file while($data = fgets($socket, 128)) { echo nl2br($data); $total .= nl2br($data); flush(); # the function, nothing gets written to the log function writeLog() { global $total; $log = fopen('log.html', 'a'); fwrite($log, $total); fclose($log); }