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.