1

plan: I want to grab the users last login data such as user-agent,ip,time,os etc. i successfully post all the data that is needed using the bellow:

$userfolder = $_SERVER['DOCUMENT_ROOT'].'/account/users/'.$auth_user->user_folder_name($userRow["user_id"],$userRow["username"]).'/';
if (isset($_SERVER['REQUEST_URI']) == '?Login=Success') {
    $uid = $userRow['user_id'];
    $last_login = $date->getTimestamp();
    $last_login_ip = $_SERVER['REMOTE_ADDR'];
    if (!file_exists($userfolder.$userRow['username'].'-login.log')) {
        $filename = $userfolder.$userRow['username'].'-login.log';
        $data = json_encode(array("user"=>$userRow['username'],"time" => $last_login,"ip_addy"=>$last_login_ip,"browser"=>$obj->showInfo('browser'),"browser_version"=>$obj->showInfo('version'),"os"=>$obj->showInfo('os')));
        $save = fopen($filename, "w");
        fwrite($save, $data."\r\n");
        fclose($save);        
    }    
}

Which would save something like this:

{"user":"Admin","time":1521638903,"ip_addy":"127.0.0.1","browser":"Google Chrome","browser_version":"64.0.3282.","os":"Windows"}

Although now I set another if statement so if the users login data changes or the user logs in using another computer on their network it should do a line break and insert it into the file.

$get_data = file_get_contents($userfolder.$userRow['username']."-login.log");
$get_log = json_decode($get_data, true);
if ($get_log['ip_addy'] != $userRow['user_last_login_ip'] && $get_log['time'] != $last_login && $get_log['browser'] != $obj->showInfo('browser'))
{
    $filename = $userfolder.$userRow['username'].'-login.log';
    $data = json_encode(array("user"=>$userRow['username'],"time" =>  $date->getTimestamp(),"ip_addy"=>$_SERVER['REMOTE_ADDR'],"browser"=>$obj->showInfo('browser'),"browser_version"=>$obj->showInfo('version'),"os"=>$obj->showInfo('os')));
    $save = fopen($filename, "w");
    fwrite($save, $data."\r\n");
    fclose($save);    
}

which outputs the following:

{"user":"Admin","time":1521639136,"ip_addy":"127.0.0.1","browser":"Firefox","browser_version":"59.0","os":"Windows"}

Notice that the previous 'Chrome' entry is no longer in the file as it removes the previous entry and writes a new one

my intended output is to display it like the following:

{
    "result":{
        "admin":[
            {
                "time":1521639136,
                "ip_addy":"127.0.0.1",
        "browser":"Firefox",
        "browser_version":"59.0",
        "os":"Windows"
            }
        ]
    }
}

it does not have to be formatted with space, it can simply be in one line and break line for the next input

Current Problem: when data is save to the file and it needs to be updated the file then gets overwritten with the new data and old data is disgarded.

Anyone able to help me achieve this would be greatly appreciated.

JamesBond
  • 312
  • 2
  • 17
  • 2
    check out json_encode() parameter JSON_PRETTY_PRINT – Honk der Hase Mar 21 '18 at 16:04
  • Possible duplicate of [Pretty-Printing JSON with PHP](https://stackoverflow.com/questions/6054033/pretty-printing-json-with-php) – Taki Mar 21 '18 at 16:13
  • @Taki Just had a look and its different as i know how to print them, the problem is that it keeps writing a new file and removes the old last_login data – JamesBond Mar 21 '18 at 16:15
  • 1
    so no problem with printing, the problem is the file gets overwritten right ? then you should update the `my intended output` part to put both jsons – Taki Mar 21 '18 at 16:18
  • What exactly is the problem? Are you searching for `fopen($filename, 'a');`? – Nico Haase Mar 21 '18 at 16:20

3 Answers3

1

According to http://php.net/manual/en/function.json-encode.php, you can use JSON_PRETTY_PRINT option in json_encode()
$data = json_encode(array(...), JSON_PRETTY_PRINT);
This should be avalible if you have php 5.4.0 or above.

sjiforgot
  • 11
  • 2
0

if you want to Append the content to a file and not overwrite it try :

$data = json_encode(array(...), JSON_PRETTY_PRINT);

file_put_contents($filename , $data , FILE_APPEND | LOCK_EX);

instead of fwrite()

this will create the file if it doesn't exist, and add content to it if it does

If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

Full documentation : http://php.net/manual/en/function.file-put-contents.php

Taki
  • 17,320
  • 4
  • 26
  • 47
0

Finally figured out the error in the following:

if (!file_exists($userfolder.$userRow['username'].'-login.log')) {
    $filename = $userfolder.$userRow['username'].'-login.log';
    $data = json_encode(array("user"=>$userRow['username'],"time" => $last_login,"ip_addy"=>$last_login_ip,"browser"=>$obj->showInfo('browser'),"browser_version"=>$obj->showInfo('version'),"os"=>$obj->showInfo('os')));
    $save = fopen($filename, "w");
    fwrite($save, $data."\r\n");
    fclose($save);        
}    

Basically !file_exists created a new file every time it logged the data so i thought why not try the if statement with just file_exists and have an else statement to handle if the file/folder does not exists.

if (file_exists($userfolder.$userRow['username'].'-login.log')) {
    //If the file exists do the following
    $filename = $userfolder.$userRow['username'].'-login.log';
    $data = json_encode(array("user"=>$userRow['username'],"time" => $last_login,"ip_addy"=>$last_login_ip,"browser"=>$obj->showInfo('browser'),"browser_version"=>$obj->showInfo('version'),"os"=>$obj->showInfo('os')));
    $save = fopen($filename, "w");
    fwrite($save, $data."\r\n"); //break works without the !file_exists
    fclose($save);        
}   else 
    {    
        //else if the file/folder does not exists do the following
        mkdir($usersfolder, 755);
        $save = fopen($usersfolder."/".$userRow['username']."-login.log", "a+");
        $data = $last_login.",".$last_login_ip.",".$obj->showInfo('os').",".$obj->showInfo('browser').",".$obj->showInfo('version');
        fwrite($save, $data."\r\n");
        fclose($save);
    } 
JamesBond
  • 312
  • 2
  • 17