Guest User

Untitled

a guest
Sep 14th, 2025
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. index.php--------------
  2. <?php
  3.  
  4. session_start();
  5.  
  6. $errors = [
  7. 'login' => $_SESSION['login_error'] ?? '',
  8. 'register' => $_SESSION['register_error'] ?? ''
  9. ];
  10. $activeForm = $_Session['active_form'] ?? 'login';
  11.  
  12. session_unset();
  13.  
  14. function showError($error){
  15. return !empty($error) ? "<p class='error-message'>$error</p>" : '';
  16. }
  17.  
  18. function isActiveForm($formName, $activeForm){
  19. return $formName === $activeForm ? 'active' : '';
  20. }
  21.  
  22. ?>
  23.  
  24. <!DOCTYPE html>
  25. <html lang="en">
  26.  
  27. <head>
  28. <meta charset="UTF-8">
  29. <meta name="viewpoint" content="width=device-width, intitial-scale=1.0">
  30. <title>NoNedry</title>
  31. <link rel="stylesheet" href="style.css">
  32. </head>
  33.  
  34. <body>
  35. <div class="container">
  36.  
  37. <div class="form-box <?= isActiveForm('login', $activeForm); ?>" id="login-form">
  38. <form action="login_register.php" method="post">
  39. <h2>login</h2>
  40. <?= showError($errors['login']); ?>
  41. <input type="email" name="email" placeholder="Email" required>
  42. <input type="password" name="password" placeholder="Password" required>
  43. <button type="submit" name="login">Login</button>
  44. <p>Dont have an account? <a href="#" onclick="showForm('register-form')">Register</a></p>
  45. </form>
  46. </div>
  47.  
  48. <div class="form-box <?= isActiveForm('register', $activeForm); ?>" id="register-form">
  49. <form action="login_register.php" method="post">
  50. <h2>Register</h2>
  51. <?= showError($errors['register']); ?>
  52. <input type="text" name="name" placeholder="Name" required>
  53. <input type="email" name="email" placeholder="Email" required>
  54. <input type="password" name="password" placeholder="Password" required>
  55. <select name="role" required>
  56. <option value="">--Select Role--</option>
  57. <option value="user">User</option>
  58. <option value="admin">Admin</option>
  59. </select>
  60. <button type="submit" name="register">Register</button>
  61. <p>Already have an account? <a href="#" onclick="showForm('login-form')">Login</a></p>
  62. </form>
  63. </div>
  64. </div>
  65.  
  66. <script src="script.js"></script>
  67.  
  68. </body>
  69.  
  70. </html>
  71.  
  72. config.php---------------
  73. <?php
  74.  
  75. $host = "localhost";
  76. $user = "root";
  77. $password = "";
  78. $database = "users_db";
  79.  
  80. $conn = new mysqli($host, $user, $password, $database);
  81.  
  82. if($conn->connect_error){
  83. die("Connection failed: ".$conn->connect_error);
  84. }
  85.  
  86. ?>
  87.  
  88. script.js------------
  89. function showForm(formID){
  90. document.querySelectorAll(".form-box").forEach(form => form.classList.remove("active"));
  91. document.getElementById(formID).classList.add("active");
  92.  
  93. }
  94.  
  95. login_register.php---------------
  96. <?php
  97.  
  98. session_start();
  99. require_once 'config.php';
  100.  
  101. if (isset($_POST['register'])) {
  102. $name = $_POST['name'];
  103. $email = $_POST['email'];
  104. $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
  105. $role = $_POST['role'];
  106.  
  107. $checkEmail = $conn->query("SELECT email FROM users WHERE email = '$email'");
  108. if ($checkEmail->num_rows > 0){
  109. $_SESSION['register_error'] = 'Email is already registered!';
  110. $_SESSION['active_form'] = 'register';
  111. } else {
  112. $conn->query("INSERT INTO users (name, email, password, role) VALUES ('$name' , '$email', '$password', '$role')");
  113. }
  114.  
  115. header("Location: index.php");
  116. exit();
  117.  
  118. }
  119.  
  120. if (isset($_POST['login'])){
  121. $email = $_POST['email'];
  122. $password = $_POST['password'];
  123.  
  124. $result = $conn->query("SELECT * FROM users WHERE email = '$email'");
  125. if ($result->num_rows > 0){
  126. $user = $result->fetch_assoc();
  127. if(password_verify($password, $user['password'])){
  128. $_SESSION['name'] = $user['name'];
  129. $_SESSION["email"] = $user['email'];
  130.  
  131. if ($user['role'] === 'admin'){
  132. header("Location: admin_page.php");
  133. } else {
  134. header("Location: user-page.php");
  135. }
  136. exit();
  137. }
  138. }
  139.  
  140. $_SESSION['login_error'] = 'Incorrect email or password';
  141. $_SESSION['active_form']= 'login';
  142. header("Location: index.php");
  143. exit();
  144. }
  145.  
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment