• 전화하지 마셈
  • hyungseob@ssim.pe.kr
Codeigniter 3.x에서 4.x로 갈아 엎느라 기존 게시판 주소로 접근시 새 주소로 리다이렉션 된다.

Developerment [PHP]

[CI] Codeigniter 구글 reCaptcha2를 사용자 폼검증으로 사용하기

  • 빛그림
  • 683
CodeIgniter 에서 구글 reCaptcha를 사용할때 form_validation으로 사용하면 코드를 훤씬 간결해 진다.

아래의 POST 데이터를 받아줄 메소드에 아래와 같이 입력해주고,
$this->form_validation->set_rules('g-recaptcha-response', '보안인증', 'trim|required|callback__check_recaptcha');


컨트롤러 안에 아래의 메소드를 넣어준다. $secret 변수에 구글의 reCaptcha Secret Key를 넣어준다.
function _check_recaptcha($code)
{
    // 구글 reCaptcha
    $recaptchaResponse = $code;

    $user_ip = $this->input->ip_address();
    $secretkey = 'abcdefghijklmnopqrstuvwxyz';

    $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secretkey . '&response=' . $recaptchaResponse . '&remoteip=' . $user_ip;
    $this->load->library('curl');
    $response = $this->curl->simple_get($url);
    $status = json_decode($response, true);

    if ($status['success']) {
        return true;
    } else {
        $this->form_validation->set_message('_check_recaptcha', '보안 인증에 실패 했습니다.');
        return false;
    }

    return true;
}


그리고 view의 폼 안에 삽입한다. data-sitekey에는 구글 reCapcha siteky를 넣어준다.
<div class="g-recaptcha" data-sitekey="abcdefghijklmnopqrstuvwxyz"></div>

새댓글 등록