Advertisement
lamorfini

Untitled

Aug 11th, 2024
149
0
261 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. <?php
  2.  
  3. /* Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number */
  4.  
  5.     public function login($identifier, $password) {
  6.         // Check if the identifier is an email or username
  7.         $sql = "SELECT * FROM users WHERE username = :identifier OR email = :identifier";
  8.         $stmt = $this->pdo->prepare($sql);
  9.         $stmt->bindParam(':identifier', $identifier);
  10.         $stmt->execute();
  11.         $user = $stmt->fetch();
  12.    
  13.         if ($user && password_verify($password, $user['password'])) {
  14.             // Start session and set user data
  15.             session_start();
  16.             $_SESSION['user_id'] = $user['id'];
  17.             $_SESSION['username'] = $user['username'];
  18.             return true; // Login successful
  19.         }
  20.         return false; // Login failed
  21.     }
  22. ?>
  23.  
  24. ---
  25.  
  26. <?php
  27. include ($_SERVER['DOCUMENT_ROOT'] . '/inc.php');
  28. ?>
  29. <?php
  30.  
  31. $errorMessage = '';
  32. $successMessage = '';
  33.  
  34. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  35.     $identifier = $_POST['identifier'];  // This can be username or email
  36.     $password = $_POST['password'];
  37.  
  38.     // Attempt to log in the user
  39.     if ($user->login($identifier, $password)) {
  40.         $successMessage = 'Login successful! Welcome, ' . htmlspecialchars($identifier) . '.';
  41.         // Redirect to a dashboard or another page
  42.         header('Location: dashboard.php');
  43.         exit;
  44.     } else {
  45.         $errorMessage = 'Invalid username or password.';
  46.     }
  47. }
  48. ?>
  49. <?php
  50. include ($_SERVER['DOCUMENT_ROOT'] . '/admin/theme/header.php');
  51. ?>
  52. <h2>Login</h2>
  53. <form method="POST" action="">
  54.     <label for="identifier">Username or Email:</label>
  55.     <input type="text" name="identifier" required><br>
  56.  
  57.     <label for="password">Password:</label>
  58.     <input type="password" name="password" required><br>
  59.  
  60.     <button type="submit">Login</button>
  61. </form>
  62. <p style="color:red;"><?php echo $errorMessage; ?></p>
  63. <p style="color:green;"><?php echo $successMessage; ?></p>
  64. <p>Don't have an account? <a href="register.php">Register here</a></p>
  65. <?php
  66. include ($_SERVER['DOCUMENT_ROOT'] . '/admin/theme/footer.php');
  67. ?>
  68.  
  69. ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement