phplogginghttp-status-code-403fputs

fputs doesn't write before 403 header is triggered


I have a function forbid() that is supposed to write to a log file that access was denied, along with a reason for the denial. For some reason, it's not writing to the logfile.

// function to forbid access
function forbid($reason) {
    // explain why
    if ($reason) fputs($file, "=== ERROR: " . $reason . " ===\n");
    fputs($file, "*** ACCESS DENIED ***" . "\n\n\n");
    fclose($file);

    // forbid
    header("HTTP/1.0 403 Forbidden");
    exit;
}

$file is defined earlier in the code, and other fputs() prior to this function are working correctly; I think it's something about the 403 header causing it to not write.


Solution

  • Looks like a scope issue. Since it's a function, it's looking for the file handle $file in the functions scope, not global. You need to pass the file handle into the function.

    I'm also going to assume that error reporting is turned off, suppressing the problem from you.