-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
94 lines (79 loc) · 2.81 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<script src='https://www.google.com/recaptcha/api.js'></script>
<link rel="stylesheet" href="styles.css">
<?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');
// Read the expiry date and poll options from the text file
$lines = file('poll_options.txt', FILE_IGNORE_NEW_LINES);
$expiry_date = strtotime($lines[0]);
$poll_options = array_slice($lines, 1);
// Read the poll question from the text file
$question = file_get_contents('question.txt');
// Check if the poll has expired
$current_date = time();
if ($current_date > $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 "<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' => $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 "<p class='orange'>You have already voted in this poll.</p>";
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 "<p class='orange'>There was an error with the reCaptcha. Please try again.</p>";
}
}
// Display the poll form
echo "<form method='post'>";
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='$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'>";
echo "</form>";
// Display the "View Results" link
// echo "<a href='results.php'>View Results</a>";
?>
<button onclick="location.href='results.php'">View Results</button>
<br>