Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number */
- public function login($identifier, $password) {
- // Check if the identifier is an email or username
- $sql = "SELECT * FROM users WHERE username = :identifier OR email = :identifier";
- $stmt = $this->pdo->prepare($sql);
- $stmt->bindParam(':identifier', $identifier);
- $stmt->execute();
- $user = $stmt->fetch();
- if ($user && password_verify($password, $user['password'])) {
- // Start session and set user data
- session_start();
- $_SESSION['user_id'] = $user['id'];
- $_SESSION['username'] = $user['username'];
- return true; // Login successful
- }
- return false; // Login failed
- }
- ?>
- ---
- <?php
- include ($_SERVER['DOCUMENT_ROOT'] . '/inc.php');
- ?>
- <?php
- $errorMessage = '';
- $successMessage = '';
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $identifier = $_POST['identifier']; // This can be username or email
- $password = $_POST['password'];
- // Attempt to log in the user
- if ($user->login($identifier, $password)) {
- $successMessage = 'Login successful! Welcome, ' . htmlspecialchars($identifier) . '.';
- // Redirect to a dashboard or another page
- header('Location: dashboard.php');
- exit;
- } else {
- $errorMessage = 'Invalid username or password.';
- }
- }
- ?>
- <?php
- include ($_SERVER['DOCUMENT_ROOT'] . '/admin/theme/header.php');
- ?>
- <h2>Login</h2>
- <form method="POST" action="">
- <label for="identifier">Username or Email:</label>
- <input type="text" name="identifier" required><br>
- <label for="password">Password:</label>
- <input type="password" name="password" required><br>
- <button type="submit">Login</button>
- </form>
- <p style="color:red;"><?php echo $errorMessage; ?></p>
- <p style="color:green;"><?php echo $successMessage; ?></p>
- <p>Don't have an account? <a href="register.php">Register here</a></p>
- <?php
- include ($_SERVER['DOCUMENT_ROOT'] . '/admin/theme/footer.php');
- ?>
- ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement