PHP

봇 자동등록 방지 로직

생활개발 2024. 5. 31. 17:01
728x90

캡차를 추가해도 봇이 체크해서 데이터를 등록하는 경우가 있다고해서 챗GPT한테 물어보고 방지 로직을 몇개 더 추가하였다

https://zzzyoon4.tistory.com/2

 

PHP 캡챠 연동

https://www.google.com/recaptcha/admin/create 접속 1. V3 AdminConsole 클릭2. 값 입력  위 화면이 안나올 경우 Switch to create a legacy key 클릭라벨: 프로젝트의 이름을 입력합니다. 예를 들어, "My Website reCAPTCHA".reCA

zzzyoon4.tistory.com

 

1. 추가적인 폼 검증 : 봇은 종종 폼 필드를 자동으로 채우기 때문에, 서버 측에서 더 강력한 폼 검증을 통해 봇을 차단할 수 있다.

히든값을 해서 값이 있으면 등록이 안되도록 추가

captcha.php 수정

<body>
    <h1>Google reCAPTHA Test</h1>
    <form id="fncForm" action="" method="post">
        <input type="text" name="name" size="40"><br><br>
        <textarea name="contents" rows="8" cols="40"></textarea><br><br>
        <input type="submit" name="submit" value="Submit"><br><br>
        <div class="g-recaptcha" data-sitekey="<?php echo $sitekey; ?>"></div>
        <input type="hidden" id="autoben" name="autoben" value=""> <!--봇 체크-->
    </form>
</body>

captchaCheck.php 수정

<?
// 봇으로 간주하고 처리 중지
if (!empty($_POST['autoben'])) {
    die('폼 검증 봇 감지.');
}
?>

autoben에 데이터가 있으면 종료

예시

2. 사용자 분석 행동 : 사용자의 마우스 클릭 횟수, 키 입력 등을 분석하여 비정상적인 행동을 감지

captcha.php 수정

<body>
    <h1>Google reCAPTHA Test</h1>
    <form id="fncForm" action="" method="post">
        <input type="text" name="name" size="40"><br><br>
        <textarea name="contents" rows="8" cols="40"></textarea><br><br>
        <input type="submit" name="submit" value="Submit"><br><br>
        <div class="g-recaptcha" data-sitekey="<?php echo $sitekey; ?>"></div>
        <input type="hidden" id="autoben" name="autoben" value="">
        <input type="hidden" name="behaviorData" id="behaviorData">
        <input type="hidden" name="mouseUps" id="mouseUps" value="4">
        <input type="hidden" name="keyUps" id="keyUps" value="10">
        <input type="hidden" name="totalTime" id="totalTime" value="5000"><!--밀리초 1000 = 1초-->
    </form>
    <script>
        let behaviorData = {
            mouseUps: [],
            keyUps: [],
            startTime: Date.now()
        };

        //클릭 횟수 체크
        document.addEventListener('mouseup', function(event) {
            behaviorData.mouseUps.push({x: event.clientX, y: event.clientY, time: Date.now()});
        });
        //키 입력 횟수 체크
        document.addEventListener('keyup', function(event) {
            behaviorData.keyUps.push({key: event.key, time: Date.now()});
        });
        //머무른 시간 체크
        document.getElementById('fncForm').addEventListener('submit', function() {
            behaviorData.totalTime = Date.now() - behaviorData.startTime;
            document.getElementById('behaviorData').value = JSON.stringify(behaviorData);
        });
    </script>
</body>

 

captchaCheck.php 수정

<?
    $behaviorData = json_decode($_POST['behaviorData'], true);

    // 분석 기준 설정
    $_POST['mouseUps'] == '' ? $minMouseUps = 0 : $minMouseUps = $_POST['mouseUps']; // 최소 마우스 클릭 횟수
    $_POST['keyUps'] == '' ? $minKeyUps = 0 : $minKeyUps = $_POST['keyUps']; // 최소 키 입력 횟수
    $_POST['totalTime'] == '' ? $minTimeSpent = 0 : $minTimeSpent = $_POST['totalTime']; // 최소 머무른 시간 (밀리초) 1000 = 1초

    $mouseUps = count($behaviorData['mouseUps']); //마우스 클릭 횟수
    $keyUps = count($behaviorData['keyUps']); //키 입력 획수
    $totalTime = $behaviorData['totalTime']; //머무른 시간

    if ($mouseUps >= $minMouseUps && $keyUps >= $minKeyUps && $totalTime >= $minTimeSpent) {
        echo '사람입니다.';
    } else {
        die('사용자 행동 분석 봇 감지.');
    }
?>

728x90

'PHP' 카테고리의 다른 글

php.ini 수정없이 php설정값 바꾸기  (0) 2024.09.20
PHP 출력문 비교 echo, print, print_r, var_dumb  (0) 2024.07.31
PHP fsockopen() 함수로 API 연동하기  (0) 2024.07.08
PHP file_get_contents(), curl  (0) 2024.07.02
PHP 캡챠 연동  (0) 2024.05.30