How can PHP scripts be vulnerable to spam or malicious bots, and what steps can be taken to prevent this?
PHP scripts can be vulnerable to spam or malicious bots through form submissions, where bots can submit spam messages or perform malicious actions. To prevent this, developers can implement CAPTCHA verification to ensure that the submission is done by a human user.
<?php
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
$secretKey = "YOUR_SECRET_KEY_HERE";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha);
$responseKeys = json_decode($response, true);
if(intval($responseKeys["success"]) !== 1) {
// CAPTCHA verification failed, handle accordingly
} else {
// CAPTCHA verification passed, process form submission
}
}
}
?>