mikeh.online

Hardware & Software info and projects

Cloudflare Turnstile PHP

Responsive image

index.html

<script type="text/javascript" src="https://challenges.cloudflare.com/turnstile/v0/api.js"></script>
<form action="form.php" method="post">
    <!--Your form fields-->
    <div class="cf-turnstile" data-sitekey="0xxxxxxxxxxxxxxxxxxxxxxx"></div>
</form>

form.php


if (isset($_POST['cf-turnstile-response'])) {
    $captcha = $_POST['cf-turnstile-response'];
  } else {
    echo 'Please check the captcha form.'
    exit;
    }

  $secretKey = "0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  $ip = $_SERVER['REMOTE_ADDR'];

  $url_path = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
  $data = array('secret' = $secretKey, 'response' = $captcha, 'remoteip' = $ip);

  $options = array(
      'http' = array(
      'method' = 'POST',
      'content' = http_build_query($data)
    )
  );

  $stream = stream_context_create($options);

  $response = file_get_contents(
    $url_path,
    false,
    $stream
  );

  $responseKeys = json_decode($response, true);

  if (intval($responseKeys["success"]) !== 1) {
    echo $responseKeys["success"];
    echo 'spam?'
    exit;
  } 

  // YOUR PHP CODE