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> <script src='https://www.google.com/recaptcha/api.js'></script>
<?php <?php
// Include the secret and site key from a separate file
require 'recaptcha_keys.php';
// Connect to the SQLite database // Connect to the SQLite database
$db = new PDO('sqlite:poll.db'); $db = new PDO('sqlite:poll.db');
@ -30,20 +33,21 @@ echo "<h1>$question</h1>";
// Check if the form has been submitted // Check if the form has been submitted
if (isset($_POST['submit'])) { if (isset($_POST['submit'])) {
// Form has been submitted // Form has been submitted
// Verify the reCaptcha response
$url = 'https://www.google.com/recaptcha/api/siteverify'; // Verify the reCaptcha response
$data = array('secret' => '6LdClsMjAAAAAEaoHxGRckp38hNpZJRU8oR8MNzf', 'response' => $_POST['g-recaptcha-response']); $url = 'https://www.google.com/recaptcha/api/siteverify';
$options = array( $data = array('secret' => $recaptcha_secret, 'response' => $_POST['g-recaptcha-response']);
$options = array(
'http' => array( 'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST', 'method' => 'POST',
'content' => http_build_query($data), 'content' => http_build_query($data),
), ),
); );
$context = stream_context_create($options); $context = stream_context_create($options);
$response = file_get_contents($url, false, $context); $response = file_get_contents($url, false, $context);
$result = json_decode($response); $result = json_decode($response);
if ($result->success) { if ($result->success) {
// reCaptcha was successful // reCaptcha was successful
// Save the vote to the database // Save the vote to the database
$stmt = $db->prepare("INSERT INTO poll_votes (option_id) VALUES (?)"); $stmt = $db->prepare("INSERT INTO poll_votes (option_id) VALUES (?)");
@ -52,7 +56,7 @@ if (isset($_POST['submit'])) {
// Redirect the user to a different page // Redirect the user to a different page
header('Location: results.php'); header('Location: results.php');
exit; exit;
} else { } else {
// reCaptcha was unsuccessful // reCaptcha was unsuccessful
// Display an error message // Display an error message
echo "<p style='color: orange;'>There was an error with the reCaptcha. Please try again.</p>"; 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 "<input type='radio' name='option' value='$id'> $option<br>";
} }
echo "<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 "<input type='hidden' name='expiry_date' value='02/01/2023'>";
echo "<br>"; echo "<br>";
echo "<input type='submit' name='submit' value='Vote'>"; echo "<input type='submit' name='submit' value='Vote'>";

BIN
poll.db

Binary file not shown.