$expiry_date) {
// Poll has expired
echo "This poll has expired.";
exit;
}
// Display the expiry date of the poll
echo "This poll ends at: " . date('l, F j, Y', $expiry_date);
// Display the poll question
echo "
$question
";
// Check if the form has been submitted
if (isset($_POST['submit'])) {
// Form has been submitted
// Verify the reCaptcha response
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array('secret' => $recaptcha_secret, 'response' => $_POST['g-recaptcha-response']);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$result = json_decode($response);
if ($result->success) {
// reCaptcha was successful
// Check if the user has already voted
$stmt = $db->prepare("SELECT COUNT(*) FROM poll_votes WHERE ip_address = ?");
$stmt->execute([$_SERVER['REMOTE_ADDR']]);
$count = $stmt->fetchColumn();
if ($count > 0) {
// User has already voted
echo "
You have already voted in this poll.
";
exit;
}
// Save the vote to the database
$stmt = $db->prepare("INSERT INTO poll_votes (option_id, ip_address) VALUES (?, ?)");
$stmt->execute([$_POST['option'], $_SERVER['REMOTE_ADDR']]);
// Redirect the user to a different page
header('Location: results.php');
exit;
} else {
// reCaptcha was unsuccessful
// Display an error message
echo "
There was an error with the reCaptcha. Please try again.
";
}
}
// Display the poll form
echo "";
// Display the "View Results" link
// echo "View Results";
?>