moved loginLogging to own function

This commit is contained in:
marko
2021-12-20 16:59:20 +01:00
parent 0abfd49781
commit f7d2e699ff

View File

@@ -37,15 +37,8 @@ function checkCredentials($username, $password, $db_server, $db_user, $db_passwo
PHPCount::AddHit("participo-Login-".$_POST['f']['username']);
// Logging Logins
$userName = $_SESSION['user']['username'];
$lastLogins = json_decode(file_get_contents("lastLogins.json"), true);
if(!array_key_exists($userName, $lastLogins))
$lastLogins[$userName] = [];
if(!array_key_exists('lastLogins', $lastLogins[$userName]))
$lastLogins[$userName]['lastLogins'] = [];
$lastLogins[$userName]['lastLogins'] = array_merge( array( date('Y-m-d H:i:s') ), $lastLogins[$userName]['lastLogins'] );
file_put_contents("lastLogins.json", json_encode($lastLogins));
logLoginsToJsonFile($_SESSION['user']['username']);
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/participo?user=' . $_POST['f']['username']);
} else {
sleep(5);
@@ -56,6 +49,27 @@ function checkCredentials($username, $password, $db_server, $db_user, $db_passwo
return $message;
}
/**
* Log the Login of an user into a logFile
*
* @param string $userName name of the user
* @param string $fileName filename to log to
* @return void
*/
function logLoginsToJsonFile($userName, $fileName="lastLogins.json"){
try{
$lastLogins = json_decode(file_get_contents($fileName), true);
if(!array_key_exists($userName, $lastLogins))
$lastLogins[$userName] = [];
if(!array_key_exists('lastLogins', $lastLogins[$userName]))
$lastLogins[$userName]['lastLogins'] = [];
$lastLogins[$userName]['lastLogins'] = array_merge( array( date('Y-m-d H:i:s') ), $lastLogins[$userName]['lastLogins'] );
file_put_contents($fileName, json_encode($lastLogins));
}
catch (Exception $e){
// silently ignore errors
}
}
if (isset($_SESSION['login'])) {
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/index.php');
@@ -71,7 +85,7 @@ else{
$message = checkCredentials($_POST['f']['username'], $_POST['f']['password'], $db_server, $db_user, $db_password, $db_name);
if( !isset($message['error']) )
$message['notice'] = "Achievement-System der Judoka des Chemnitzer WSV";
$message['notice'] = "OnlineApps - cwsvJudo";
}
}
}