Advertisement
zoldos

php captcha

Apr 25th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.57 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. include 'includes/core.php';
  5. include_once 'includes/head.php';
  6. include 'menu/menu.php';
  7.  
  8. $id = $_GET['id']; // Get the content ID from the URL parameter
  9.  
  10. // Generate a timestamp-based unique identifier for this CAPTCHA instance
  11. $captchaId = time();
  12. $_SESSION['captchaId'] = $captchaId;
  13.  
  14. $num1 = rand(1, 10);
  15. $num2 = rand(1, 10);
  16. $operation = rand(0, 1) ? '+' : '-';
  17. $correctAnswer = $operation == '+' ? $num1 + $num2 : $num1 - $num2;
  18. $captchaQuestion = "What is $num1 $operation $num2?"; // Define the CAPTCHA question here
  19.  
  20. // Store CAPTCHA answer in the database with the unique identifier
  21. $insertStmt = $pdo->prepare("INSERT INTO captcha_answers (session_id, answer) VALUES (:session_id, :answer)");
  22. $insertStmt->bindParam(':session_id', $captchaId);
  23. $insertStmt->bindParam(':answer', $correctAnswer);
  24. $insertStmt->execute();
  25.  
  26. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  27.     $page_id = $_POST['page_id'];
  28.     $comment = $_POST['comment'];
  29.     $name = $_POST['name'];
  30.     $captcha_answer = isset($_POST['captcha_answer']) ? trim($_POST['captcha_answer']) : '';
  31.     $submittedCaptchaId = $_SESSION['captchaId'];
  32.  
  33.     // Retrieve the CAPTCHA answer from the database using the unique identifier
  34.     $selectStmt = $pdo->prepare("SELECT answer FROM captcha_answers WHERE session_id = :session_id");
  35.     $selectStmt->bindParam(':session_id', $submittedCaptchaId);
  36.     $selectStmt->execute();
  37.     $row = $selectStmt->fetch(PDO::FETCH_ASSOC);
  38.  
  39.     if ($row) {
  40.         if (intval($captcha_answer) === intval($row['answer'])) {
  41.             // Correct CAPTCHA answer, proceed with form processing
  42.             // (Insert comment into database, etc.)
  43.             // Redirect to prevent form resubmission
  44.             header("Location: ?id=$id");
  45.             exit();
  46.         } else {
  47.             // Log or output for debugging
  48.             error_log("CAPTCHA validation failed. Expected: {$row['answer']}, Received: {$captcha_answer}");
  49.             echo "<script>alert('Incorrect CAPTCHA answer, please try again.');</script>";
  50.         }
  51.     } else {
  52.         echo "<script>alert('CAPTCHA validation error. Please try again.');</script>";
  53.     }
  54.  
  55.     // Cleanup after attempt
  56.     $deleteStmt = $pdo->prepare("DELETE FROM captcha_answers WHERE session_id = :session_id");
  57.     $deleteStmt->bindParam(':session_id', $submittedCaptchaId);
  58.     $deleteStmt->execute();
  59.  
  60.     unset($_SESSION['captchaId']); // Clear the CAPTCHA ID for the next attempt
  61. }
  62. ?>
  63. <body>
  64. <div class="parent-container">      
  65.         <div class="content">
  66.         <?php if ($id == 1): ?>
  67.             <img src="images/cooltext454173021986469.png">
  68.         <?php elseif ($id == 2): ?>
  69.             <img src="images/cooltext454172974574051.png">
  70.         <?php elseif ($id == 3): ?>
  71.             <img src="images/cooltext454172942527369.png">
  72.         <?php endif; ?>
  73.                
  74.             <div class="comments_title">Prior Comments</div>
  75.             <div class="comment">
  76.              <?php if (!empty($comments)): ?>
  77.                     <?php foreach ($comments as $comment) : ?>
  78.                         <div class="comment-block">
  79.                             <div class="commenter-name">Commenter Name: <?php echo htmlspecialchars($comment['name']); ?></div>
  80.                             <div class="comment-text">Comment: <?php echo htmlspecialchars($comment['comment']); ?></div>
  81.                         </div>
  82.                     <?php endforeach; ?>
  83.                 <?php else: ?>                    
  84.                     <div class="no-comments">No comments yet...</div>                    
  85.             <?php endif; ?>
  86.             </div>            
  87.             <form method="post" action="">
  88.             <input type="hidden" name="page_id" value="<?php echo htmlspecialchars($id); ?>">
  89.                 <label for="name"><span class="text">Name or Alias:</span></label><br>
  90.                 <input type="text" id="name" name="name" size="30" minlength="3" maxlength="40" required><br>
  91.                 <label for="comment"><span class="text">Enter Comment:</span></label><br>
  92.                 <textarea id="comment" name="comment" rows="5" cols="60" minlength="10" maxlength="200" required></textarea><br>                
  93.                 <label for="captcha_answer"><span class="text"><?php echo $captchaQuestion; ?></span></label><br>
  94.                 <input type="text" id="captcha_answer" name="captcha_answer" required><br>         <button type="submit" id="button22">Proceed</button>
  95.             </form>
  96.             <?php include 'includes/back.php' ?>
  97.         </div>
  98.     </div>
  99. </body>
  100. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement