Guest User

Untitled

a guest
May 20th, 2026
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.45 KB | None | 0 0
  1. <?php
  2. // Step 1: Start the session at the very top of the script.
  3. session_start();
  4.  
  5. require_once __DIR__ . '/config.php';
  6. require_once __DIR__ . '/captcha_handler.php';
  7.  
  8. $message = '';
  9. $message_type = '';
  10.  
  11. // Step 2: Check for a message from the session (after a redirect).
  12. if (isset($_SESSION['message'])) {
  13.   $message = $_SESSION['message'];
  14.   $message_type = $_SESSION['message_type'];
  15.  
  16.   // Unset the session variables so the message doesn't show again on the next refresh.
  17.   unset($_SESSION['message']);
  18.   unset($_SESSION['message_type']);
  19. }
  20.  
  21. // Step 3: Process the form submission.
  22. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  23.  
  24.   // 1. CAPTCHA Verification
  25.   if (!verify_captcha($_POST['captcha_code'] ?? '')) {
  26.     $_SESSION['message'] = 'Invalid security code. Please try again.';
  27.     $_SESSION['message_type'] = 'error';
  28.     header("Location: /contact"); // Corrected Path
  29.     exit();
  30.   }
  31.  
  32.   $name = strip_tags(trim($_POST["name"]));
  33.   $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
  34.   $subject = strip_tags(trim($_POST["subject"]));
  35.   $content = strip_tags(trim($_POST["message"]));
  36.  
  37.   // 2. Form Field Validation
  38.   if (empty($name) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($subject) || empty($content)) {
  39.     $_SESSION['message'] = 'Please fill out all fields correctly.';
  40.     $_SESSION['message_type'] = 'error';
  41.     header("Location: /contact"); // Corrected Path & Added Redirect/Exit
  42.     exit();
  43.   }
  44.  
  45.   // 3. Database Insertion
  46.   try {
  47.     $stmt = $pdo->prepare("INSERT INTO mails (name, email, subject, message, date) VALUES (:name, :email, :subject, :message, :date)");
  48.     $stmt->execute([':name' => $name, ':email' => $email, ':subject' => $subject, ':message' => $content, ':date' => time()]);
  49.  
  50.     $_SESSION['message'] = 'Thank you! Your message has been received.';
  51.     $_SESSION['message_type'] = 'success';
  52.  
  53.   } catch (PDOException $e) {
  54.     $_SESSION['message'] = 'Sorry, there was a database error and we could not save your message.';
  55.     $_SESSION['message_type'] = 'error';
  56.   }
  57.  
  58.   // 4. Final Redirect
  59.   header("Location: contact.php"); // Corrected Path
  60.   exit();
  61. }
  62.  
  63. $page_title = 'Contact Us - ' . htmlspecialchars($config['site_title']);
  64. require_once __DIR__ . '/partials/header.php';
  65. ?>
  66.  
  67. <main class="container main-content">
  68.   <div class="contact-form-container">
  69.     <h1 class="page-title" style="text-align: center; margin-bottom: var(--spacing-3);">Contact Us</h1>
  70.  
  71.     <?php if ($message): ?>
  72.       <div style="
  73.         padding: 15px;
  74.         margin-bottom: 20px;
  75.         border: 1px solid transparent;
  76.         border-radius: 4px;
  77.         color: <?= $message_type === 'success' ? '#3c763d' : '#a94442' ?>;
  78.         background-color: <?= $message_type === 'success' ? '#dff0d8' : '#f2dede' ?>;
  79.         border-color: <?= $message_type === 'success' ? '#d6e9c6' : '#ebccd1' ?>;
  80.       ">
  81.         <?= htmlspecialchars($message) ?>
  82.       </div>
  83.     <?php endif; ?>
  84.  
  85.     <form action="/contact" method="post">
  86.       <div class="form-group">
  87.         <label for="name" class="form-label">Name</label>
  88.         <input type="text" id="name" name="name" required class="form-input">
  89.       </div>
  90.       <div class="form-group">
  91.         <label for="email" class="form-label">Email</label>
  92.         <input type="email" id="email" name="email" required class="form-input">
  93.       </div>
  94.       <div class="form-group">
  95.         <label for="subject" class="form-label">Subject</label>
  96.         <input type="text" id="subject" name="subject" required class="form-input">
  97.       </div>
  98.       <div class="form-group">
  99.         <label for="message" class="form-label">Message</label>
  100.         <textarea id="message" name="message" rows="5" required class="form-textarea"></textarea>
  101.       </div>
  102.       <div class="form-group">
  103.         <label for="captcha_code" class="form-label">Security Code</label>
  104.         <div style="display: flex; align-items: center; gap: 1rem;">
  105.           <img src="captcha.php" alt="CAPTCHA" style="border-radius: 5px;">
  106.           <input type="text" id="captcha_code" name="captcha_code" required class="form-input" placeholder="Enter code">
  107.         </div>
  108.       </div>
  109.       <button type="submit" class="btn btn-primary" style="width: 100%;">Send Message</button>
  110.      
  111.         <div style="text-align: center; margin-top: 2rem;">
  112.             <?php echo $config['site_contact_email']; ?>    
  113.         </div>
  114.     </form>
  115.    
  116.   </div>
  117. </main>
  118.  
  119. <?php require_once __DIR__ . '/partials/footer.php'; ?>
Advertisement
Add Comment
Please, Sign In to add comment