From f7d2e699ffaaf2de04b1dd482c9438dc873aa940 Mon Sep 17 00:00:00 2001 From: marko Date: Mon, 20 Dec 2021 16:59:20 +0100 Subject: [PATCH] moved loginLogging to own function --- homepage/participo/login.php | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/homepage/participo/login.php b/homepage/participo/login.php index 218f967..aae2fb5 100644 --- a/homepage/participo/login.php +++ b/homepage/participo/login.php @@ -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"; } } }