Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Step 1: Start the session at the very top of the script.
- session_start();
- require_once __DIR__ . '/config.php';
- require_once __DIR__ . '/captcha_handler.php';
- $message = '';
- $message_type = '';
- // Step 2: Check for a message from the session (after a redirect).
- if (isset($_SESSION['message'])) {
- $message = $_SESSION['message'];
- $message_type = $_SESSION['message_type'];
- // Unset the session variables so the message doesn't show again on the next refresh.
- unset($_SESSION['message']);
- unset($_SESSION['message_type']);
- }
- // Step 3: Process the form submission.
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- // 1. CAPTCHA Verification
- if (!verify_captcha($_POST['captcha_code'] ?? '')) {
- $_SESSION['message'] = 'Invalid security code. Please try again.';
- $_SESSION['message_type'] = 'error';
- header("Location: /contact"); // Corrected Path
- exit();
- }
- $name = strip_tags(trim($_POST["name"]));
- $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
- $subject = strip_tags(trim($_POST["subject"]));
- $content = strip_tags(trim($_POST["message"]));
- // 2. Form Field Validation
- if (empty($name) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($subject) || empty($content)) {
- $_SESSION['message'] = 'Please fill out all fields correctly.';
- $_SESSION['message_type'] = 'error';
- header("Location: /contact"); // Corrected Path & Added Redirect/Exit
- exit();
- }
- // 3. Database Insertion
- try {
- $stmt = $pdo->prepare("INSERT INTO mails (name, email, subject, message, date) VALUES (:name, :email, :subject, :message, :date)");
- $stmt->execute([':name' => $name, ':email' => $email, ':subject' => $subject, ':message' => $content, ':date' => time()]);
- $_SESSION['message'] = 'Thank you! Your message has been received.';
- $_SESSION['message_type'] = 'success';
- } catch (PDOException $e) {
- $_SESSION['message'] = 'Sorry, there was a database error and we could not save your message.';
- $_SESSION['message_type'] = 'error';
- }
- // 4. Final Redirect
- header("Location: contact.php"); // Corrected Path
- exit();
- }
- $page_title = 'Contact Us - ' . htmlspecialchars($config['site_title']);
- require_once __DIR__ . '/partials/header.php';
- ?>
- <main class="container main-content">
- <div class="contact-form-container">
- <h1 class="page-title" style="text-align: center; margin-bottom: var(--spacing-3);">Contact Us</h1>
- <?php if ($message): ?>
- <div style="
- padding: 15px;
- margin-bottom: 20px;
- border: 1px solid transparent;
- border-radius: 4px;
- color: <?= $message_type === 'success' ? '#3c763d' : '#a94442' ?>;
- background-color: <?= $message_type === 'success' ? '#dff0d8' : '#f2dede' ?>;
- border-color: <?= $message_type === 'success' ? '#d6e9c6' : '#ebccd1' ?>;
- ">
- <?= htmlspecialchars($message) ?>
- </div>
- <?php endif; ?>
- <form action="/contact" method="post">
- <div class="form-group">
- <label for="name" class="form-label">Name</label>
- <input type="text" id="name" name="name" required class="form-input">
- </div>
- <div class="form-group">
- <label for="email" class="form-label">Email</label>
- <input type="email" id="email" name="email" required class="form-input">
- </div>
- <div class="form-group">
- <label for="subject" class="form-label">Subject</label>
- <input type="text" id="subject" name="subject" required class="form-input">
- </div>
- <div class="form-group">
- <label for="message" class="form-label">Message</label>
- <textarea id="message" name="message" rows="5" required class="form-textarea"></textarea>
- </div>
- <div class="form-group">
- <label for="captcha_code" class="form-label">Security Code</label>
- <div style="display: flex; align-items: center; gap: 1rem;">
- <img src="captcha.php" alt="CAPTCHA" style="border-radius: 5px;">
- <input type="text" id="captcha_code" name="captcha_code" required class="form-input" placeholder="Enter code">
- </div>
- </div>
- <button type="submit" class="btn btn-primary" style="width: 100%;">Send Message</button>
- <div style="text-align: center; margin-top: 2rem;">
- <?php echo $config['site_contact_email']; ?>
- </div>
- </form>
- </div>
- </main>
- <?php require_once __DIR__ . '/partials/footer.php'; ?>
Advertisement
Add Comment
Please, Sign In to add comment