From 63fff8843b7944570fb952130dd4405f5e4ed220 Mon Sep 17 00:00:00 2001 From: deepend Date: Mon, 2 Jan 2023 04:51:37 +0000 Subject: [PATCH] added ip check and graph results --- README.md | 1 + index.php | 23 ++++++++++++++++++----- poll.db | Bin 8192 -> 8192 bytes poll_options.txt | 9 ++++----- question.txt | 2 +- results.php | 35 +++++++++++++++++++++++++++++------ styles.css | 4 ++++ 7 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 styles.css diff --git a/README.md b/README.md index 6cf263c..c0934fb 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Basic steps include: 3. Create a new table in the database named 'poll_votes" with the following structure: 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. diff --git a/index.php b/index.php index 0f7bd9a..ed12ee9 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ - + 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 "

You have already voted in this poll.

"; + + exit; + } // Save the vote to the database - $stmt = $db->prepare("INSERT INTO poll_votes (option_id) VALUES (?)"); - $stmt->execute([$_POST['option']]); + $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'); @@ -59,7 +70,7 @@ if ($result->success) { } else { // reCaptcha was unsuccessful // Display an error message - echo "

There was an error with the reCaptcha. Please try again.

"; + echo "

There was an error with the reCaptcha. Please try again.

"; } } @@ -76,6 +87,8 @@ echo ""; echo ""; // Display the "View Results" link -echo "View Results"; +// echo "View Results"; ?> + +
diff --git a/poll.db b/poll.db index fd2cdf0e5c1e7ee2965a6e6e4510fa1d3ba44e67..cc017acc131924c73d94a28f48b6fa3fc3f7efde 100644 GIT binary patch delta 114 zcmZp0XmFSyE$GU?z`z8=Fu*iX$C%%hK`%Ig7bwKYzmS1{;l@H^e$H4%c5y{T#zxl3 zyZK#YbQCfR;uBL+ic*V<6+&DiLNp=D-!bsN+bk&XgkMO2k(oo?+(6IVT+h%5h|Mhl D$Y>Wn delta 93 zcmZp0XmFSyEojTYz`z8=Fu*ub$C%%iK`%Ig7bwKYznOu5^JYN-SAGs}MmBLpMaJaG itN2}bH9@jW{NEY)|L}hY%HQRm7{JHL%*4b9LYx50mJlZZ diff --git a/poll_options.txt b/poll_options.txt index 39379e3..68b537d 100644 --- a/poll_options.txt +++ b/poll_options.txt @@ -1,5 +1,4 @@ -2023-02-01 -Test -This is a song -this is my mother -dude where is my car +2023-01-05 +Yes +No +Depends how major diff --git a/question.txt b/question.txt index 1f23bf9..02a7591 100644 --- a/question.txt +++ b/question.txt @@ -1 +1 @@ -Who am I really? +Changes are possibly on the horizon. Are you open to big changes to tilde.club? diff --git a/results.php b/results.php index fb5a59c..09594b3 100644 --- a/results.php +++ b/results.php @@ -10,16 +10,39 @@ array_shift($options); // Get the total number of votes $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) { // Get the vote count for this option $vote_count = $db->query("SELECT COUNT(*) FROM poll_votes WHERE option_id=$id")->fetchColumn(); - // Calculate the percentage of votes for this option - $percentage = round(($vote_count / $total_votes) * 100); - - // Display the results - echo "$option: $percentage% ($vote_count votes)
"; + // Add the data for this option to the chart data + $data[] = [$option, $vote_count]; } ?> + + + + + + +
+

+ + + diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..ae5c7b7 --- /dev/null +++ b/styles.css @@ -0,0 +1,4 @@ +/* Orange color */ +.orange { + color: #ffa500; +}