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"; ?> + +