diff --git a/config.php b/config.php index 85589c72f6e277da18c099ffa8f652351120d6de..6b2cdeed4b7c79354fbbd25b6f15e51a4307224e 100644 --- a/config.php +++ b/config.php @@ -25,13 +25,35 @@ class Database { $fields, ":" . implode(", :", array_keys($data)) ); - var_dump($sql); $stmt = $this->conn->prepare($sql); $stmt->execute($data); } -} -$database = new Database(); -$conn = $database->connect(); + public function select($tableName) { + $sql = "SELECT * FROM $tableName"; + $stmt = $this->conn->prepare($sql); + $stmt->execute(); + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + + public function selectById($tableName, $id) { + $sql = "SELECT * FROM $tableName WHERE id = :id"; + $stmt = $this->conn->prepare($sql); + $stmt->execute(['id' => $id]); + return $stmt->fetch(PDO::FETCH_ASSOC); + } + + public function login($id) { + $sql = 'UPDATE users SET is_logged = 1 WHERE id = :id'; + $stmt = $this->conn->prepare($sql); + $stmt->execute(['id' => $id]); + } + + public function logout($id){ + $sql = 'UPDATE users SET is_logged = 0 WHERE id = :id'; + $stmt = $this->conn->prepare($sql); + $stmt->execute(['id' => $id]); + } +} ?> \ No newline at end of file diff --git a/index.html b/index.php similarity index 63% rename from index.html rename to index.php index c3444c3bc6506470d56f8ddfb94263e59d13d089..fb32f8af83a5029d42f1a6c0f25f4dceaa6383c7 100644 --- a/index.html +++ b/index.php @@ -1,3 +1,21 @@ +<?php + +require_once 'config.php'; + +$db = new Database(); +$db->connect(); +session_start(); + +if(isset($_SESSION["id"])) { + + $user = $db->selectById("users", $_SESSION['id']); + $userId = $user['id']; + +} + + +?> + <!DOCTYPE html> <html lang="en"> <head> @@ -27,12 +45,17 @@ </div> <div class="links"> <a href="./register.php" class="btn btn-outline-primary hover-effect" + style="display : <?php echo isset($userId) ? 'none' : 'block'; ?>" >Register</a > <a href="./login.php" class="btn btn-outline-warning hover-effect" + style="display : <?php echo isset($userId) ? 'none' : 'block'; ?>" >Login</a > - <a href="" class="btn btn-outline-danger hover-effect">Logout</a> + <a href="./logout_logic.php" class="btn btn-outline-danger hover-effect" + style="display : <?php echo isset($userId) ? 'block' : 'none'; ?>" + >Logout</a + > </div> <div id="open-menu"> @@ -40,6 +63,18 @@ </div> </nav> + <?php + + if (isset($_SESSION['success'])) { + echo "<div class='alert alert-info' role='alert'> + {$_SESSION['success']} + </div>"; + unset($_SESSION['success']); + } + + + ?> + <div class="tablet-menu bg-sky-950/95 slide-down"> <div id="close-menu"> <div class="tablet-logo"> @@ -50,9 +85,9 @@ <img src="./images/close.png" alt="close-menu" class="cross" /> </div> <div class="flex flex-col gap-y-4"> - <a href="" class="btn btn-outline-primary hover-effect">Register</a> - <a href="" class="btn btn-outline-warning hover-effect">Login</a> - <a href="" class="btn btn-outline-danger hover-effect">Logout</a> + <a href="./register.php" class="btn btn-outline-primary hover-effect" style="display : <?php echo isset($userId) ? 'none' : 'block'; ?>">Register</a> + <a href="./login.php" class="btn btn-outline-warning hover-effect" style="display : <?php echo isset($userId) ? 'none' : 'block'; ?>">Login</a> + <a href="./logout_logic.php" class="btn btn-outline-danger hover-effect" style="display : <?php echo isset($userId) ? 'block' : 'none'; ?>">Logout</a> </div> </div> diff --git a/login.php b/login.php index 5fbe4700654f078ec0f51501e659e1b55f75bc0c..3e7fcda37c8f196afeeb3652abbb4ff3b7c9c876 100644 --- a/login.php +++ b/login.php @@ -1,12 +1,59 @@ -<?php +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <script src="https://cdn.tailwindcss.com"></script> + <link + href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" + rel="stylesheet" + integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" + crossorigin="anonymous" + /> + <link rel="stylesheet" href="./style.css" /> + <title>Document</title> +</head> +<body> + <div class="main text-center bg-sky-950/90 py-3 shadow-2xl"> + <h2 class="text-3xl font-semibold uppercase font-mono tracking-wider text-cyan-200">Login</h2> + </div> + <?php -session_start(); + session_start(); -if (isset($_SESSION['success'])) { - echo $_SESSION['success']; - unset($_SESSION['success']); -} + if (isset($_SESSION['success'])) { + echo "<div class='alert alert-info' role='alert'> + {$_SESSION['success']} + </div>"; + unset($_SESSION['success']); + } -echo 'u proces...'; + if(isset($_SESSION['error'])){ + echo "<div class='alert alert-danger' role='alert'> + {$_SESSION['error']} + </div>"; + unset($_SESSION['error']); + } -?> \ No newline at end of file + ?> + + <form action="./login_logic.php" method="POST" class="login-form mt-5"> + <div> + <label for="username" class="form-label">Username</label> + <input type="text" name="username" id="username" placeholder="Enter your username" class="form-control"> + </div> + <div> + <label for="password" class="form-label">Password</label> + <input type="password" name="password" id="password" placeholder="Enter your password" class="form-control"> + </div> + <div class="d-flex justify-center"> + <button class="submit-btn">Login</button> + </div> + </form> + + <div class="footer-msg"> + <img src="./images/world-book-day (1).png" alt="book" class="footer-book"> + <p>Brainster Library</p> + </div> +</body> +</html> \ No newline at end of file diff --git a/login_logic.php b/login_logic.php new file mode 100644 index 0000000000000000000000000000000000000000..975af96a105ffddc7187afc9c7bc9d042ecc5b5e --- /dev/null +++ b/login_logic.php @@ -0,0 +1,28 @@ +<?php + +if($_SERVER['REQUEST_METHOD'] == 'POST') { + + require_once './config.php'; + session_start(); + + $db = new Database(); + $db->connect(); + + $allUsers = $db->select("users"); + + foreach ($allUsers as $user) { + if($user['username'] == $_POST['username'] && password_verify($_POST['password'], $user['password'])) { + $db->login($user['id']); + $_SESSION['id'] = $user['id']; + header('location: ./index.php'); + die(); + } + } + + $_SESSION['error'] = 'Invalid credentials'; + header('location: ./login.php'); + die(); + +} + +?> \ No newline at end of file diff --git a/logout_logic.php b/logout_logic.php new file mode 100644 index 0000000000000000000000000000000000000000..f7302b390b623fc4ddf9a9a68a885e1592699584 --- /dev/null +++ b/logout_logic.php @@ -0,0 +1,22 @@ +<?php + +session_start(); + + +if(isset($_SESSION["id"])) { + + require_once 'config.php'; + + $db = new Database(); + $db->connect(); + + $db->logout($_SESSION['id']); + + $_SESSION['success'] = 'You have been logged out'; + unset($_SESSION['id']); + header('location: index.php'); + die(); + +} + +?> \ No newline at end of file diff --git a/register.php b/register.php index 9ea2cb8cd2d99f1d916db3245f2f1d7c4f6cbb31..5dfe73baaf74f9958e8acb711cfef1e9809a48aa 100644 --- a/register.php +++ b/register.php @@ -24,7 +24,9 @@ session_start(); if(isset($_SESSION["errors"])) { foreach ($_SESSION["errors"] as $error) { - echo $error . '<br>'; + echo "<div class='alert alert-danger' role='alert'> + $error + </div>"; } } diff --git a/register_logic.php b/register_logic.php index b0d44fd4fb0da5e2a2804dfa4cc7f0d90b662db6..284ba634916f4535cb56a678fa71074ef5e4f624 100644 --- a/register_logic.php +++ b/register_logic.php @@ -18,25 +18,36 @@ if($_SERVER['REQUEST_METHOD'] == 'POST') { $errorFlag = true; } - - // password validation if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/', $password)) { array_push($_SESSION['errors'], 'Password must be at least 8 characters long, contain at least one lowercase letter, one uppercase letter, one number and one special character'); $errorFlag = true; } + $db = new Database(); + $db->connect(); + + $allUsers = $db->select("users"); + + foreach ($allUsers as $user) { + if($user['username'] == $_POST['username'] || $user['email'] == $_POST['email']) { + array_push($_SESSION['errors'], 'Username or email already exists'); + $errorFlag = true; + break; + } + } + if($errorFlag) { header('location: register.php'); exit(); } - $db = new Database(); - $db->connect(); + $info = $_POST; $info['is_admin'] = 0; $info['is_logged'] = 0; + $info['password'] = password_hash($info['password'], PASSWORD_DEFAULT); $db->insert("users", $info); diff --git a/style.css b/style.css index 88725f56f89825b5c1f6a99c2050371323ceed54..aab4f8864c916b0531c9aa0bb061e855b0077bb3 100644 --- a/style.css +++ b/style.css @@ -58,12 +58,14 @@ h1 { align-items: center; } +.login-form, .register-form { width: 60%; margin: 0 auto; } -.register-form > div { +.register-form > div, +.login-form > div { margin-bottom: 20px; }