Write in a log file

Discussion in 'PHP' started by minhazikram, Jan 19, 2012.

  1. #1
    I have a log file in which i can write information when clearing the file content.

    but when i try to write information about who is logging in. the information are not written on the file.

    the code is given below:


    this is the function through which to append in file.

    function log_action($action,$message="")
    {
    $logfile = "../logs/log.txt";

    if($file_handle = fopen($logfile,'a')){
    $timestamp = strftime("%Y/%m/%d %H:%M:%S",time());
    $total_content = "{$timestamp} | {$action} : {$message} ";
    fwrite($file_handle,$total_content);
    fclose($file_handle);
    }
    else{
    echo "Couldnot open file";
    }

    }


    this code below works when i clear the log file and writes sth in the log.txt file.

    if(isset($_GET['id'])){
    $getvalue = $_GET['id'];
    if($getvalue == 'true'){
    file_put_contents($logfile,'');
    log_action('logs Cleared',"By user with id {$session->user_id}");//redirect to the same page so that the URL won't have "clear=true" anymore
    header('Location:logfile.php');
    exit;
    }
    }

    the code below (log_action()) is not writing anything in the log.txt file

    if ($found_user) {
    $user = $found_user;
    $session->login($found_user);
    log_action('logged in ", "By user {$found_user} \r\n");
    header('Location: panel.php');
    exit;
    } else {
    // username/password combo was not found in the database
    $message = "Username/password combination incorrect.";
    }
     
    Last edited: Jan 19, 2012
    minhazikram, Jan 19, 2012 IP
  2. michael_gourlay

    michael_gourlay Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not sure if it's just a copy/paste error, but this line:

    log_action('logged in ", "By user {$found_user} \r\n");

    Did you start the logged in literal with a single quote and then end it with a double quote?
     
    michael_gourlay, Jan 19, 2012 IP