Cloudflare Turnstile CAPTCHA in PHP (Block Automated Bots)
Cloudflare's Turnstile is a modern alternative to other traditional CAPTCHA services that helps to balance a website's security without compromising users' privacy. Learn how to add this CAPTCHA checkbox to PHP applications and ensure human form submissions.

Turnstile checkbox is a CAPTCHA service that is used by many web applications for human verification. It is used as an alternative to other services like Google reCAPTCHA v2, reCAPTCHA v3, and hCAPTCHA. It comes without tracking cookies, with minimal data collection, and users are not required to complete complex challenges. This post walks through steps to integrate Turnstile CAPTCHA into PHP applications. The integration will require the following files:
- index.php: Contains the HTML content with the Cloudflare checkbox.
- verify-turnstile.php: The server-side script to verify the Turnstile response.
- javascript.js: A JavaScript file that prevents form submission if the Turnstile response is not ready.
- style.css: CSS Stylesheet for the page.
How to Integrate Cloudflare Turnstile CAPTCHA in PHP?
Integrating the Turnstile checkbox service requires a Cloudflare account, a Turnstile widget, a site key and site secret, and a user interface where we want to add this CAPTCHA checkbox.
Step 1: Create a Turnstile Widget in Cloudflare
We first need to create a Turnstile widget in the Cloudflare account. The following are the steps to create the widget:
- Log in to the Cloudflare account dashboard.
- Click "Turnstile" in the left-side navigation menu.
- Click on the "+ Add Widget" button on the widget page.
- Enter a meaningful widget name.
- Click on "+ Add Hostnames".
- Add the hostnames that are allowed to use this widget.
- Keep the "Managed" option checked in the "Widget Mode" section.
- Click on "Create".

Step 2: Get Turnstile Site Key and Secret Key
The next page after creating the widget will display a site key and site secret. We will need both in our integration to render the checkbox and communicate with Cloudflare's API endpoint.
Step 3: Create Cloudflare Constants File
We need to define constants for the site key, site secret, and challenges URL in a constants.php file.
constants.php
<?php
define('TURNSTILE_CHALLENGES_URL', 'https://challenges.cloudflare.com/turnstile/v0');
define('TURNSTILE_SITE_KEY', 'YOUR_TURNSTILE_SITE_KEY');
define('TURNSTILE_SECRET_KEY', 'YOUR_TURNSTILE_SECRET_KEY');
Step 4: Add Turnstile Checkbox to HTML Form
We create an HTML page that contains the form along with a CAPTCHA checkbox that the user needs to check for the human verification process.
index.php
<?php include_once 'constants.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>Cloudflare Turnstile CAPTCHA in PHP - Demo</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport"/>
<link rel="stylesheet" href="css/style.css"/>
<script src="<?= TURNSTILE_CHALLENGES_URL; ?>/api.js" defer></script>
<script src="js/javascript.js" type="text/javascript"></script>
</head>
<body>
<section class="section py-4">
<div class="container">
<form action="verify-turnstile.php" method="POST" class="turnstile-form mb-4">
<div class="mb-4">
<div class="cf-turnstile"
data-sitekey="<?= TURNSTILE_SITE_KEY; ?>"
data-size="flexible"
data-response-field-name="cf_turnstile_response"></div>
</div>
<div class="mb-4">
<button type="submit" class="btn btn-blue">Send</button>
</div>
</form>
</div>
</section>
</body>
</html>
Step 5: Prevent Form Submissions without Human Verification
We need to make sure the form does not submit unless the user completes the human verification by making the checkbox field required. We can prevent form submission and show an alert if a user has not checked the checkbox with the following JavaScript code.
javascript.js
document.addEventListener("DOMContentLoaded", function () {
document.querySelector(".turnstile-form")
.addEventListener("submit", function (e) {
let turnstile_response = document.querySelector('input[name="cf_turnstile_response"]');
if (!turnstile_response || !turnstile_response.value) {
e.preventDefault(); // Stop form submission
alert("Cloudflare CAPTCHA is required.");
}
});
});Step 6: Verify Turnstile Response on Server-Side
When the user submits the form, we need to process the form in a server-side script and verify the Turnstile response with Cloudflare's challenges API. We can process the form after we receive a successful response of verification. The following is a step-by-step explanation of the server-side code:
- Include the constants file in the script.
- Sanitize the
$_POSTarray. - Create a new cURL instance.
- Add the challenges API endpoint URL to the cURL object.
- Pass the secret key as a
secretparameter in the data of the cURL request and the Turnstileresponsesubmitted with the form for verification. - Execute a cURL request and close the cURL connection.
- Decode the API response with the
json_decode()function. - Check if the response was not successful, then redirect the user back to the same page. You can add an error message in the session here.
- If the response was successful, process the form.
verify-turnstile.php
<?php
include_once 'constants.php';
$post = filter_input_array(INPUT_POST);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, TURNSTILE_CHALLENGES_URL . '/siteverify');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query([
'secret' => TURNSTILE_SECRET_KEY,
'response' => $post['cf_turnstile_response'],
]));
$response = curl_exec($curl_handle);
curl_close($curl_handle);
// Decode the JSON response
$response_data = json_decode($response, true);
// Redirect the user with an error message if Turnstile response was not successful
if (!$response_data['success']) {
header('Location: ' . filter_var($_SERVER['HTTP_REFERER'], FILTER_SANITIZE_URL));
}
// Everything worked fine, and we can process the form
Step 7: Add CAPTCHA Interface CSS Styles
We also need to add the following CSS styles for the entire page.
style.css
* {
box-sizing: border-box;
}
html,body {
margin: 0;
padding: 0;
}
body {
background-color: #f6f6f6;
font-family: "Segoe UI", "Roboto", "Helvetica", sans-serif;
font-size: 15px;
font-weight: normal;
font-style: normal;
}
.container {
max-width: 1024px;
margin: 0 auto;
padding-left: 15px;
padding-right: 15px;
}
.py-4 {
padding-top: 1rem;
padding-bottom: 1rem;
}
.my-1, .mb-1 {
margin-bottom: .25rem;
}
.my-4, .mb-4 {
margin-bottom: 1rem;
}
.btn {
display: inline-block;
padding: .375rem .75rem;
cursor: pointer;
font-size: inherit;
}
.btn-blue {
background-color: #0369a1;
border: 1px solid #0369a1;
color: #ffffff;
}
.btn-blue:hover {
background-color: #005D95;
}
.cf-turnstile{
min-height: 65px;
}Cloudflare's Turnstile CAPTCHA provides privacy-friendly protection for the website forms to prevent bots and automated submissions. We demonstrated the implementation of this service in a PHP application that provides protection against bots while maintaining a smooth user experience.