From 1cab2e0daca3fcd2196afca1ee750911ccd2a419 Mon Sep 17 00:00:00 2001 From: Raghav <raghavsmart1213@gmail.com> Date: Wed, 12 Mar 2025 04:58:29 +0000 Subject: [PATCH] Tried to implement login credential verification: Failed --- _includes/Database.class.php | 11 ++++++ _includes/User.class.php | 21 +++++++++- _templates/login-content.php | 76 ++++++++++++++++++------------------ libs/load.php | 9 +++-- test.php | 44 +++++++++++---------- 5 files changed, 97 insertions(+), 64 deletions(-) diff --git a/_includes/Database.class.php b/_includes/Database.class.php index 1f00096..703c452 100644 --- a/_includes/Database.class.php +++ b/_includes/Database.class.php @@ -3,6 +3,7 @@ class Database { public static $conn = null; + public static $result; public static function getConnection() { @@ -29,4 +30,14 @@ class Database return Database::$conn; } } + + public static function getUserData($email, $password){ + + $sql = "SELECT password from 'auth' where email = $email;"; + Database::$result = Database::$conn-> fetch($sql); + } + + public static function setUserData(){ + echo "password = " . Database::$result["password"]; + } } diff --git a/_includes/User.class.php b/_includes/User.class.php index b9c656e..f92db81 100644 --- a/_includes/User.class.php +++ b/_includes/User.class.php @@ -1,6 +1,8 @@ <?php + class User { + public static $sql; public static function signup($user, $pass, $email, $phone) { // Store $conn values from class Database @@ -8,7 +10,7 @@ class User // To save password as md5 hash format $pass = md5($pass); - + $sql = "INSERT INTO `auth` (`username`, `password`, `email`, `phone`, `block`, `active`) VALUES ('$user', '$pass', '$email', '$phone', '0', '1');"; $error = false; @@ -24,4 +26,21 @@ class User $conn->close(); return $error; } + + public static function getCredential($email, $pass) + { + + // Connect to Database + $conn = Database::getConnection(); + + Database::getUserData($email, $pass); + + + + } + + public static function setCredential() + { + Database::setUserData(); + } } diff --git a/_templates/login-content.php b/_templates/login-content.php index c2e2de8..5257581 100644 --- a/_templates/login-content.php +++ b/_templates/login-content.php @@ -1,44 +1,44 @@ -<? +<?php // Getting the values from the form and storing them in variables. - $username = $_POST['email']; - $password = $_POST['password']; - $validate = validate_credentials($username, $password); - - if($validate){?> - <!-- If it is true is display true page --> - <main class="container"> - <div class="bg-body-tertiary p-5 rounded"> - <h1>Login Success</h1> - <p class="lead">This example is a quick exercise to illustrate How to login with simple POST method</p> - <a class="btn btn-lg btn-primary" href="/docs/5.3/components/navbar/" role="button">View navbar docs »</a> - </div> - </main> - <?}else{?> +$email = $_POST['email']; +$password = $_POST['password']; +$validate = validate_credentials($email, $password); - <!-- If it is false it displays the login form --> +if ($validate) {?> +<!-- If it is true is display true page --> +<main class="container"> + <div class="bg-body-tertiary p-5 rounded"> + <h1>Login Success</h1> + <p class="lead">This example is a quick exercise to illustrate How to login with simple POST method</p> + <a class="btn btn-lg btn-primary" href="/docs/5.3/components/navbar/" role="button">View navbar docs »</a> + </div> +</main> +<?} else {?> + +<!-- If it is false it displays the login form --> <main class="form-signin w-100 m-auto"> - <form action="/photogram-project-php/login.php" method="post"> - <img class="mb-4 center" src="/photogram-project-php/assets/brand/devyani_logo_edit_light copy.svg" alt="" width="72" height="77"> - <h1 class="h3 mb-3 fw-normal">Please sign in</h1> + <form action="/photogram-project-php/login.php" method="post"> + <img class="mb-4 center" src="/photogram-project-php/assets/brand/devyani_logo_edit_light copy.svg" alt="" + width="72" height="77"> + <h1 class="h3 mb-3 fw-normal">Please sign in</h1> - <div class="form-floating"> - <input name="email" type="email" class="form-control" id="floatingInput" placeholder="name@example.com"> - <label for="floatingInput">Email address</label> - </div> - <div class="form-floating"> - <input name="password" type="password" class="form-control" id="floatingPassword" placeholder="Password"> - <label for="floatingPassword">Password</label> - </div> + <div class="form-floating"> + <input name="email" type="email" class="form-control" id="floatingInput" placeholder="name@example.com"> + <label for="floatingInput">Email address</label> + </div> + <div class="form-floating"> + <input name="password" type="password" class="form-control" id="floatingPassword" placeholder="Password"> + <label for="floatingPassword">Password</label> + </div> - <div class="form-check text-start my-3"> - <input class="form-check-input" type="checkbox" value="remember-me" id="flexCheckDefault"> - <label class="form-check-label" for="flexCheckDefault"> - Remember me - </label> - </div> - <button class="hvr-grow-shadow btn btn-primary w-100 py-2 " type="submit">Sign in</button> - - </form> -</main> -<?}?> + <div class="form-check text-start my-3"> + <input class="form-check-input" type="checkbox" value="remember-me" id="flexCheckDefault"> + <label class="form-check-label" for="flexCheckDefault"> + Remember me + </label> + </div> + <button class="hvr-grow-shadow btn btn-primary w-100 py-2 " type="submit">Sign in</button> + </form> +</main> +<?}?> \ No newline at end of file diff --git a/libs/load.php b/libs/load.php index 0351992..27ead7a 100644 --- a/libs/load.php +++ b/libs/load.php @@ -12,13 +12,14 @@ function load_template($name) } // Function to validate the credentials. -function validate_credentials($username, $password) +function validate_credentials($email, $password) { - if ($username == "raghav@gmail.com" and $password == "raghav") { + + // pass credential to class function + User::getCredential($email, $password); + if ($email == "raghav@gmail.com" and $password == "raghav") { return true; } else { return false; } } - - diff --git a/test.php b/test.php index e4a797b..7bc5a6f 100644 --- a/test.php +++ b/test.php @@ -21,35 +21,37 @@ // echo "Error: " . $sql . "<br>" . $conn->error; // } - // Testing whether the class is called :: - $mic1 = new Mic(); - $mic2 = new Mic(); + // // Testing whether the class is called :: + // $mic1 = new Mic(); + // $mic2 = new Mic(); - $mic1->brand = "Switf"; - $mic2 -> brand = "Hyundai"; + // $mic1->brand = "Switf"; + // $mic2 -> brand = "Hyundai"; - // Adding values to $light with $this keyword - $mic1 -> light = "RGB"; - $mic1 -> setLight("yellow"); + // // Adding values to $light with $this keyword + // $mic1 -> light = "RGB"; + // $mic1 -> setLight("yellow"); - // Inserting the string to setModel() - $mic1 -> setModel("toyata innova"); + // // Inserting the string to setModel() + // $mic1 -> setModel("toyata innova"); - // Displaying the string - $mic1-> getModel(); + // // Displaying the string + // $mic1-> getModel(); - printf("mic1 = %s\n", $mic1 -> brand); - printf("mic2 = %s\n", $mic2 -> brand); + // printf("mic1 = %s\n", $mic1 -> brand); + // printf("mic2 = %s\n", $mic2 -> brand); - // Inserting value to the private property through public method - $mic1 -> setColorProxy("Red"); - print("From private color: ". $mic1-> getColorProxy()); + // // Inserting value to the private property through public method + // $mic1 -> setColorProxy("Red"); + // print("From private color: ". $mic1-> getColorProxy()); - // Check whether connection is established or returns existing connection - Database::getConnection(); - Database::getConnection(); - Database::getConnection(); + // // Check whether connection is established or returns existing connection + // Database::getConnection(); + // Database::getConnection(); + // Database::getConnection(); + User::getCredential("admin@example.com", "admin"); + User::setCredential(); ?> </pre> \ No newline at end of file -- GitLab