mirror of https://github.com/tildeclub/poll.git
added ip check and graph results
This commit is contained in:
parent
a1b58be28b
commit
63fff8843b
|
@ -17,6 +17,7 @@ Basic steps include:
|
||||||
3. Create a new table in the database named 'poll_votes" with the following structure:
|
3. Create a new table in the database named 'poll_votes" with the following structure:
|
||||||
|
|
||||||
CREATE TABLE poll_votes (option_id INTEGER);
|
CREATE TABLE poll_votes (option_id INTEGER);
|
||||||
|
ALTER TABLE poll_votes ADD COLUMN ip_address TEXT;
|
||||||
|
|
||||||
4. Create a text file named poll_options.txt and add the expiry date of the poll and the poll options to the file. The expiry date should be the first line of the file, followed by the poll options. Each poll option should be on a new line.
|
4. Create a text file named poll_options.txt and add the expiry date of the poll and the poll options to the file. The expiry date should be the first line of the file, followed by the poll options. Each poll option should be on a new line.
|
||||||
|
|
||||||
|
|
23
index.php
23
index.php
|
@ -1,6 +1,7 @@
|
||||||
<script src='https://www.google.com/recaptcha/api.js'></script>
|
<script src='https://www.google.com/recaptcha/api.js'></script>
|
||||||
|
<link rel="stylesheet" href="styles.css">
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Include the secret and site key from a separate file
|
// Include the secret and site key from a separate file
|
||||||
require 'recaptcha_keys.php';
|
require 'recaptcha_keys.php';
|
||||||
|
|
||||||
|
@ -49,9 +50,19 @@ $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
|
||||||
|
// 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
|
// 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, ip_address) VALUES (?, ?)");
|
||||||
$stmt->execute([$_POST['option']]);
|
$stmt->execute([$_POST['option'], $_SERVER['REMOTE_ADDR']]);
|
||||||
|
|
||||||
// Redirect the user to a different page
|
// Redirect the user to a different page
|
||||||
header('Location: results.php');
|
header('Location: results.php');
|
||||||
|
@ -59,7 +70,7 @@ if ($result->success) {
|
||||||
} 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 class='orange'>There was an error with the reCaptcha. Please try again.</p>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +87,8 @@ echo "<input type='submit' name='submit' value='Vote'>";
|
||||||
echo "</form>";
|
echo "</form>";
|
||||||
|
|
||||||
// Display the "View Results" link
|
// Display the "View Results" link
|
||||||
echo "<a href='results.php'>View Results</a>";
|
// echo "<a href='results.php'>View Results</a>";
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
<button onclick="location.href='results.php'">View Results</button>
|
||||||
|
<br>
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
2023-02-01
|
2023-01-05
|
||||||
Test
|
Yes
|
||||||
This is a song
|
No
|
||||||
this is my mother
|
Depends how major
|
||||||
dude where is my car
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Who am I really?
|
Changes are possibly on the horizon. Are you open to big changes to tilde.club?
|
||||||
|
|
35
results.php
35
results.php
|
@ -10,16 +10,39 @@ array_shift($options);
|
||||||
// Get the total number of votes
|
// Get the total number of votes
|
||||||
$total_votes = $db->query("SELECT COUNT(*) FROM poll_votes")->fetchColumn();
|
$total_votes = $db->query("SELECT COUNT(*) FROM poll_votes")->fetchColumn();
|
||||||
|
|
||||||
// Display the results
|
// Prepare the data for the chart
|
||||||
|
$data = [['Option', 'Votes']];
|
||||||
foreach ($options as $id => $option) {
|
foreach ($options as $id => $option) {
|
||||||
// Get the vote count for this option
|
// Get the vote count for this option
|
||||||
$vote_count = $db->query("SELECT COUNT(*) FROM poll_votes WHERE option_id=$id")->fetchColumn();
|
$vote_count = $db->query("SELECT COUNT(*) FROM poll_votes WHERE option_id=$id")->fetchColumn();
|
||||||
|
|
||||||
// Calculate the percentage of votes for this option
|
// Add the data for this option to the chart data
|
||||||
$percentage = round(($vote_count / $total_votes) * 100);
|
$data[] = [$option, $vote_count];
|
||||||
|
|
||||||
// Display the results
|
|
||||||
echo "$option: $percentage% ($vote_count votes)<br>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
google.charts.load('current', {'packages':['corechart']});
|
||||||
|
google.charts.setOnLoadCallback(drawChart);
|
||||||
|
|
||||||
|
function drawChart() {
|
||||||
|
var data = google.visualization.arrayToDataTable(<?php echo json_encode($data); ?>);
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
title: 'Poll Results'
|
||||||
|
};
|
||||||
|
|
||||||
|
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
|
||||||
|
chart.draw(data, options);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="chart_div"></div>
|
||||||
|
<br><br>
|
||||||
|
<button onclick="location.href='index.php'">Return to Poll</button>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
/* Orange color */
|
||||||
|
.orange {
|
||||||
|
color: #ffa500;
|
||||||
|
}
|
Loading…
Reference in New Issue