Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- index.php--------------
- <?php
- session_start();
- $errors = [
- 'login' => $_SESSION['login_error'] ?? '',
- 'register' => $_SESSION['register_error'] ?? ''
- ];
- $activeForm = $_Session['active_form'] ?? 'login';
- session_unset();
- function showError($error){
- return !empty($error) ? "<p class='error-message'>$error</p>" : '';
- }
- function isActiveForm($formName, $activeForm){
- return $formName === $activeForm ? 'active' : '';
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewpoint" content="width=device-width, intitial-scale=1.0">
- <title>NoNedry</title>
- <link rel="stylesheet" href="style.css">
- </head>
- <body>
- <div class="container">
- <div class="form-box <?= isActiveForm('login', $activeForm); ?>" id="login-form">
- <form action="login_register.php" method="post">
- <h2>login</h2>
- <?= showError($errors['login']); ?>
- <input type="email" name="email" placeholder="Email" required>
- <input type="password" name="password" placeholder="Password" required>
- <button type="submit" name="login">Login</button>
- <p>Dont have an account? <a href="#" onclick="showForm('register-form')">Register</a></p>
- </form>
- </div>
- <div class="form-box <?= isActiveForm('register', $activeForm); ?>" id="register-form">
- <form action="login_register.php" method="post">
- <h2>Register</h2>
- <?= showError($errors['register']); ?>
- <input type="text" name="name" placeholder="Name" required>
- <input type="email" name="email" placeholder="Email" required>
- <input type="password" name="password" placeholder="Password" required>
- <select name="role" required>
- <option value="">--Select Role--</option>
- <option value="user">User</option>
- <option value="admin">Admin</option>
- </select>
- <button type="submit" name="register">Register</button>
- <p>Already have an account? <a href="#" onclick="showForm('login-form')">Login</a></p>
- </form>
- </div>
- </div>
- <script src="script.js"></script>
- </body>
- </html>
- config.php---------------
- <?php
- $host = "localhost";
- $user = "root";
- $password = "";
- $database = "users_db";
- $conn = new mysqli($host, $user, $password, $database);
- if($conn->connect_error){
- die("Connection failed: ".$conn->connect_error);
- }
- ?>
- script.js------------
- function showForm(formID){
- document.querySelectorAll(".form-box").forEach(form => form.classList.remove("active"));
- document.getElementById(formID).classList.add("active");
- }
- login_register.php---------------
- <?php
- session_start();
- require_once 'config.php';
- if (isset($_POST['register'])) {
- $name = $_POST['name'];
- $email = $_POST['email'];
- $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
- $role = $_POST['role'];
- $checkEmail = $conn->query("SELECT email FROM users WHERE email = '$email'");
- if ($checkEmail->num_rows > 0){
- $_SESSION['register_error'] = 'Email is already registered!';
- $_SESSION['active_form'] = 'register';
- } else {
- $conn->query("INSERT INTO users (name, email, password, role) VALUES ('$name' , '$email', '$password', '$role')");
- }
- header("Location: index.php");
- exit();
- }
- if (isset($_POST['login'])){
- $email = $_POST['email'];
- $password = $_POST['password'];
- $result = $conn->query("SELECT * FROM users WHERE email = '$email'");
- if ($result->num_rows > 0){
- $user = $result->fetch_assoc();
- if(password_verify($password, $user['password'])){
- $_SESSION['name'] = $user['name'];
- $_SESSION["email"] = $user['email'];
- if ($user['role'] === 'admin'){
- header("Location: admin_page.php");
- } else {
- header("Location: user-page.php");
- }
- exit();
- }
- }
- $_SESSION['login_error'] = 'Incorrect email or password';
- $_SESSION['active_form']= 'login';
- header("Location: index.php");
- exit();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment