테스트 사이트 - 개발 중인 베타 버전입니다

문제풀이 캡챠뛰우기 [수정]

· 8개월 전 · 603 · 3

설정한 시간에 맞춰 간단한 문제를 풀어야 하는 캡챠를 뛰우는 소스입니다. 

$captcha_interval = 1800; // 시간설정 (초단위)
$points = rand(1, 10); // 문제 풀었을 때 포인트 지급 1~10포인트 

 

저 같은 경우 상당히 도움이 되는 느낌이라 공유합니다. ^_^ 

25년 2월 20일 새벽2시 48분 수정

그전에 사용한분이 계시다면 수정해주세요. (오류가 좀 있어서)

 

 

[code]

 

<?php
session_start();
include_once('./_common.php'); // 그누보드 기본 함수 (goto_url 등) 포함

// 로그인 여부 체크 (로그인한 경우에만 CAPTCHA 적용)
$logged_in = isset($member) && isset($member['mb_id']) && $member['mb_id'];

// 30개의 아주 쉬운 덧셈 CAPTCHA 문제 배열
$captcha_questions = [
    ["question" => "1 + 1 = ?", "answer" => "2"],
    ["question" => "1 + 2 = ?", "answer" => "3"],
    ["question" => "2 + 2 = ?", "answer" => "4"],
    ["question" => "1 + 3 = ?", "answer" => "4"],
    ["question" => "2 + 3 = ?", "answer" => "5"],
    ["question" => "3 + 3 = ?", "answer" => "6"],
    ["question" => "1 + 4 = ?", "answer" => "5"],
    ["question" => "2 + 4 = ?", "answer" => "6"],
    ["question" => "3 + 4 = ?", "answer" => "7"],
    ["question" => "4 + 4 = ?", "answer" => "8"],
    ["question" => "1 + 5 = ?", "answer" => "6"],
    ["question" => "2 + 5 = ?", "answer" => "7"],
    ["question" => "3 + 5 = ?", "answer" => "8"],
    ["question" => "4 + 5 = ?", "answer" => "9"],
    ["question" => "5 + 5 = ?", "answer" => "10"],
    ["question" => "1 + 6 = ?", "answer" => "7"],
    ["question" => "2 + 6 = ?", "answer" => "8"],
    ["question" => "3 + 6 = ?", "answer" => "9"],
    ["question" => "4 + 6 = ?", "answer" => "10"],
    ["question" => "5 + 6 = ?", "answer" => "11"],
    ["question" => "1 + 7 = ?", "answer" => "8"],
    ["question" => "2 + 7 = ?", "answer" => "9"],
    ["question" => "3 + 7 = ?", "answer" => "10"],
    ["question" => "4 + 7 = ?", "answer" => "11"],
    ["question" => "5 + 7 = ?", "answer" => "12"],
    ["question" => "6 + 7 = ?", "answer" => "13"],
    ["question" => "1 + 8 = ?", "answer" => "9"],
    ["question" => "2 + 8 = ?", "answer" => "10"],
    ["question" => "3 + 8 = ?", "answer" => "11"],
    ["question" => "4 + 8 = ?", "answer" => "12"],
];

// CAPTCHA 노출 간격 (초 단위, 1800초 = 30분)
$captcha_interval = 1800;

// CAPTCHA 적용은 로그인한 사용자에게만 진행
$captcha_required = false;
if ($logged_in) {
    // 세션 대신 쿠키에 저장된 시간을 확인합니다.
    $last_captcha_time = isset($_COOKIE['captcha_time']) ? (int) $_COOKIE['captcha_time'] : 0;
    if (time() - $last_captcha_time >= $captcha_interval) {
        $captcha_required = true;
    }
}

// CAPTCHA가 필요한 경우, 아직 랜덤 문제를 선택하지 않았다면 30문제 중 하나를 선택
if ($logged_in && $captcha_required && !isset($_SESSION['captcha_question_index'])) {
    $_SESSION['captcha_question_index'] = array_rand($captcha_questions);
}

// CAPTCHA 폼 제출 시 처리
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['captcha_answer'])) {
    if ($logged_in) {
        $index = isset($_SESSION['captcha_question_index']) ? $_SESSION['captcha_question_index'] : null;
        if ($index !== null && trim($_POST['captcha_answer']) === $captcha_questions[$index]['answer']) {
            // 정답인 경우: 쿠키에 현재 시각을 저장하여 CAPTCHA 반복 노출을 방지
            setcookie('captcha_time', time(), time() + $captcha_interval, '/');
            unset($_SESSION['captcha_question_index']);
            
            // 2~10포인트 랜덤 지급
            if (function_exists('insert_point')) {
                $points = rand(1, 10); // 1포인트에서 10포인트 사이의 랜덤 값
                $unique_key = 'captcha_' . uniqid(); // 고유 식별자 생성
                insert_point($member['mb_id'], $points, "CAPTCHA 정답 처리 - {$points}포인트 지급", $unique_key, $index);
            }
            
            // 정답 처리 후 팝업 출력 후 현재 페이지로 리다이렉트
            echo "<script>
                    alert('오오! 럭키! {$points} 포인트 획득하셨습니다.');
                    location.href='" . $_SERVER['REQUEST_URI'] . "';
                  </script>";
            exit;
        } else {
            $error = "정답이 틀렸습니다. 다시 시도하세요.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <title>CAPTCHA 예제</title>
  <style>
    /* 모달 팝업 기본 스타일 */
    #captchaModal {
      display: none; /* 기본 숨김 */
      position: fixed;
      z-index: 1000;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0,0,0,0.5);
    }
    #captchaContent {
      background-color: #fff;
      margin: 15% auto;
      padding: 20px;
      border: 1px solid #888;
      width: 300px;
      text-align: center;
      box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    }
  </style>
  <script>
    // DOM 로드 후, PHP에서 전달한 captcha_required 값이 true이면 모달을 표시합니다.
    document.addEventListener("DOMContentLoaded", function(){
      var captchaRequired = <?php echo json_encode($captcha_required); ?>;
      if (captchaRequired) {
        document.getElementById("captchaModal").style.display = "block";
      }
    });
  </script>
</head>
<body>
  <!-- 페이지의 다른 내용들이 있을 수 있습니다. -->
  
  <!-- CAPTCHA 모달 팝업 (한 페이지 내 구현) -->
  <div id="captchaModal">
    <div id="captchaContent">
      <h2>잠시 쉬어가기!</h2>
      아주 간단한 문제를 풀며 잠시 쉬어가세요!<br>
      <?php if(isset($error)) { echo "<p style='color:red;'>$error</p>"; } ?>
      <form method="post" action="">
        <?php
        // 선택된 CAPTCHA 문제 출력 (랜덤 선택된 문제)
        if ($logged_in && isset($_SESSION['captcha_question_index'])) {
            $index = $_SESSION['captcha_question_index'];
            echo "<p>" . $captcha_questions[$index]['question'] . "</p>";
        }
        ?>
        <input type="text" name="captcha_answer" required>
        <br><br>
        <button type="submit">확인</button>
      </form>
    </div>
  </div>
</body>
</html>

[/code]

 

 

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 3개

좋네요 감사합니다

8개월 전

감사 합니다.

8개월 전

감사합니다

게시글 목록

번호 제목
23798
23797
23792
23791
23785
23781
23770
23766
23764
23761
23747
23732
23724
23718
23706
23700
23697
23686
23682
23681
23680
23678
23665
23644
23643
23639
23637
23630
23626
23616