Using Cloudflare Turnstile with PHP

Using Cloudflare Turnstile with PHP

In web development, ensuring the integrity of form submissions is crucial for maintaining the security of your applications. Cloudflare’s Turnstile service offers a powerful solution for protecting against automated submissions by providing a CAPTCHA-like challenge that users must complete before submitting a form. In this article, we’ll explore how to integrate Turnstile response validation into a PHP-based form submission process.

Prerequisites: Before proceeding, ensure you have access to a Cloudflare account and have configured Turnstile for your website. You’ll need to obtain your Turnstile secret key and site key, which will be used to authenticate requests and render the Turnstile challenge, respectively.

  1. Add Turnstile Challenge to Your Form: Start by including the Turnstile JavaScript API in your HTML form page. Place the following script tag just before the closing </body> tag to load the Turnstile challenge:

<script src=”https://challenges.cloudflare.com/turnstile/v0/api.js” defer></script>
<div class=”cf-turnstile” data-sitekey=”<YOUR_SITE_KEY>”></div>

Replace <YOUR_SITE_KEY> with your actual Turnstile site key.

2. Implement Turnstile Response Validation in PHP: In your PHP script that processes the form submission, integrate the Turnstile response validation logic. Before processing the form data, validate the Turnstile response received from the client. Here’s how to do it:

// Initialize $turnstileSuccess to false
$turnstileSuccess = false;

// Set up parameters for Turnstile check
$secret = ‘<YOUR_SECRET_KEY>’;
$remote_addr = $_SERVER[‘REMOTE_ADDR’];
$cf_url = ‘https://challenges.cloudflare.com/turnstile/v0/siteverify’;

// Check if the token is set in the POST data
if (isset($_POST[‘cf-turnstile-response’])) {
$token = $_POST[‘cf-turnstile-response’];

// Prepare data for the request
$data = array(
“secret” => $secret,
“response” => $token,
“remoteip” => $remote_addr
);

// Initialize cURL
$curl = curl_init();

// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $cf_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
));

// Execute the cURL request
$response = curl_exec($curl);

// Check for cURL errors
if (curl_errno($curl)) {
$error_message = curl_error($curl);
// Handle the error as desired
echo ‘cURL Error: ‘ . $error_message . ‘<br>’;
} else {
// Parse Cloudflare’s response and check for validation errors
$response = json_decode($response, true);
$turnstileSuccess = empty($response[‘error-codes’]);
}

// Close cURL
curl_close($curl);
} else {
// Handle case when token is not set
$turnstileSuccess = false;
}

// Only process form if Turnstile check passed
if ($turnstileSuccess) {
// Process your form here
}

Replace <YOUR_SECRET_KEY> with your actual Turnstile secret key.

Conclusion: By integrating Turnstile response validation into your PHP-based form submission process, you can enhance the security of your applications by preventing automated submissions. This ensures that only legitimate users can submit forms, thereby reducing the risk of spam and malicious activity. Implementing Turnstile validation is a straightforward process that provides robust protection against automated attacks.