forked from beamzer/EazyPoll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvote.php
More file actions
103 lines (94 loc) · 3.19 KB
/
vote.php
File metadata and controls
103 lines (94 loc) · 3.19 KB
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
95
96
97
98
99
100
101
102
103
<?php
try {
// Connect to SQLite database
$db = new SQLite3('poll_database.db');
// Get parameters
$token = $_GET['token'];
$vote = strtolower($_GET['vote']); // Convert to lowercase for consistent comparison
// Validate token
if (empty($token)) {
throw new Exception('Invalid token');
}
// Validate vote value
if ($vote !== 'yes' && $vote !== 'no') {
throw new Exception('Illegal vote value. Only "yes" or "no" are allowed.');
}
// Check if token exists and hasn't been used
$stmt = $db->prepare('SELECT * FROM polls WHERE token = :token AND vote IS NULL');
$stmt->bindValue(':token', $token, SQLITE3_TEXT);
$result = $stmt->execute()->fetchArray();
if (!$result) {
throw new Exception('Invalid or expired token');
}
// Record the vote
$stmt = $db->prepare('UPDATE polls SET vote = :vote, voted_at = :voted_at WHERE token = :token');
$stmt->bindValue(':vote', $vote, SQLITE3_TEXT);
$stmt->bindValue(':voted_at', date('Y-m-d H:i:s'), SQLITE3_TEXT);
$stmt->bindValue(':token', $token, SQLITE3_TEXT);
$stmt->execute();
// Close database connection
$db->close();
// Show success message
echo "<!DOCTYPE html>
<html>
<head>
<title>Vote Recorded</title>
<meta http-equiv='refresh' content='2;url=myresults.php?token=" . urlencode($token) . "'>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.message {
padding: 20px;
background-color: #dff0d8;
border: 1px solid #d6e9c6;
border-radius: 4px;
color: #3c763d;
max-width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class='message'>
<h2>Thank you for your vote!</h2>
<p>Your response has been recorded.</p>
<p>redirecting you to the results page....</p>
</div>
</body>
</html>";
} catch (Exception $e) {
// Handle errors
http_response_code(400);
echo "<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.error {
padding: 20px;
background-color: #f2dede;
border: 1px solid #ebccd1;
border-radius: 4px;
color: #a94442;
max-width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class='error'>
<h2>Error</h2>
<p>" . htmlspecialchars($e->getMessage()) . "</p>
</div>
</body>
</html>";
}
?>