first commit

This commit is contained in:
deepend 2023-01-02 01:45:57 +00:00
parent c9d5419ac6
commit 60e6d851f6
4 changed files with 33 additions and 26 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
recaptcha_keys.php

2
README.md Normal file
View File

@ -0,0 +1,2 @@
Basic Poll Script
# poll

View File

@ -1,6 +1,9 @@
<script src='https://www.google.com/recaptcha/api.js'></script>
<?php
// Include the secret and site key from a separate file
require 'recaptcha_keys.php';
// Connect to the SQLite database
$db = new PDO('sqlite:poll.db');
@ -30,32 +33,33 @@ echo "<h1>$question</h1>";
// 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' => '6LdClsMjAAAAAEaoHxGRckp38hNpZJRU8oR8MNzf', '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
// Save the vote to the database
$stmt = $db->prepare("INSERT INTO poll_votes (option_id) VALUES (?)");
$stmt->execute([$_POST['option']]);
// Redirect the user to a different page
header('Location: results.php');
exit;
} else {
// reCaptcha was unsuccessful
// Display an error message
echo "<p style='color: orange;'>There was an error with the reCaptcha. Please try again.</p>";
// 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
// Save the vote to the database
$stmt = $db->prepare("INSERT INTO poll_votes (option_id) VALUES (?)");
$stmt->execute([$_POST['option']]);
// Redirect the user to a different page
header('Location: results.php');
exit;
} else {
// reCaptcha was unsuccessful
// Display an error message
echo "<p style='color: orange;'>There was an error with the reCaptcha. Please try again.</p>";
}
}
@ -65,7 +69,7 @@ foreach ($poll_options as $id => $option) {
echo "<input type='radio' name='option' value='$id'> $option<br>";
}
echo "<br>";
echo "<div class='g-recaptcha' data-sitekey='6LdClsMjAAAAAF38bS2qAPTERQQRMnEQVFYeCRou'></div>";
echo "<div class='g-recaptcha' data-sitekey='$recaptcha_site_key'></div>";
echo "<input type='hidden' name='expiry_date' value='02/01/2023'>";
echo "<br>";
echo "<input type='submit' name='submit' value='Vote'>";

BIN
poll.db

Binary file not shown.