What is the purpose of using reCAPTCHA in PHP forms?
reCAPTCHA is used in PHP forms to prevent spam and automated submissions. It helps verify that the form is being filled out by a human and not a bot. By implementing reCAPTCHA, you can improve the security of your forms and ensure that only legitimate submissions are accepted.
<?php
// Include the reCAPTCHA library
require_once('recaptchalib.php');
// Your reCAPTCHA site key
$siteKey = 'YOUR_SITE_KEY';
// Your reCAPTCHA secret key
$secret = 'YOUR_SECRET_KEY';
// Verify the reCAPTCHA response
$recaptcha = new ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response']);
if ($resp->isSuccess()) {
// Process the form submission
// Your code here
} else {
// Handle reCAPTCHA verification failure
echo 'reCAPTCHA verification failed.';
}
?>
Related Questions
- How can the issue of gaps in ID numbers after deleting specific data records be addressed in PHP?
- What are the potential security risks associated with allowing users to input PHP scripts in their signatures, and how can these risks be mitigated?
- In the context of PHP, what are the best practices for handling database table creation and modification?