From 931c4786ad73eee303dffd9945c6fa8e0e068b23 Mon Sep 17 00:00:00 2001 From: Raghav <raghavsmart1213@gmail.com> Date: Mon, 17 Mar 2025 07:38:55 +0000 Subject: [PATCH] Login test implemented using session --- libs/load.php | 2 ++ logintest.php | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/libs/load.php b/libs/load.php index cc91e43..6136689 100644 --- a/libs/load.php +++ b/libs/load.php @@ -6,6 +6,8 @@ include_once '_includes/Database.class.php'; include_once '_includes/User.class.php'; include_once '_includes/Session.class.php'; +Session::start(); + function load_template($name) { diff --git a/logintest.php b/logintest.php index ff89471..cc7380c 100644 --- a/logintest.php +++ b/logintest.php @@ -4,10 +4,31 @@ include 'libs/load.php'; $user = "devyani "; $pass = "devyani"; -$result = User::login($user, $pass); + +if(isset($_GET['logout'])){ + Session::destroy(); + + die("Successfully Destroyed session..<br><br><a href = logintest.php>Login again</a>"); +} +// Check if the session already exist return this statement +if (Session::get('is_loggedin') == true) { + $userdata = Session::get('session_user'); + print("Welcome back," . $userdata['username']); + + // To logout send "logout" through GET method through link + print("<br><br> <a href = logintest.php?logout>logout</a>"); + $result = $userdata; +} +// else try to login +else { + printf("no session found, trying to login now...."); + $result = User::login($user, $pass); +} + +// check if the user details is present in session and store in session if ($result) { - echo "Login success" . "\n". "password = $result[password]"; + Session::set('is_loggedin', true); + Session::set('session_user', $result); } else { - echo "Login failed"; + echo "login failed"; } - -- GitLab