I'm attempting to build a simple PHP voting system where a website user is asked to vote on their favorite photo. Only one vote is permitted each day.
When a vote is cast the PHP script will:
====
1 -- checks the users IP address and compares it to the IP Log text file
2 -- if the users IP address is found in the log it checks to see if the last vote (for that IP address) was submitted on the current date
3 -- If the users IP address was logged and the last vote was on the current date the user is redirected to page reminding them only one vote is allowed per day
4 -- If the users IP address was logged but the last vote was prior to the current date, the vote is recorded and the user is redirected to a thank you for voting page
5 -- If the user IP address is not logged the vote is recorded and the user is redirected to a thank you for voting page.
====
Unfortunately the PHP script I'm working with will do 1 of 2 things:
1 -- If a "break" is included after the if/else statement ($logIP == $ipAddress), the script adds two votes and two IP entries into the log
2 -- If the "break" is removed, one vote is entered and one IP address is recorded. However, regardless of the vote and IP being recorded, new voters and returning votes are redirected to the page reminding them only one vote is permitted each day by the same user
The script is as follows:
====
// GET USERS VOTE, IP ADDRESS AND TODAYS DATE
$userVote = $_REQUEST['vote'];
$ipAddress = $_SERVER["REMOTE_ADDR"];
$ipDate = date("Y/m/d");
// IF THE USERS VOTE IS NULL, REDIRECT TO ERROR PAGE
if ($userVote != "") {
$userFound = false;
$userVoted = false;
echo $userFound . " -- " . $userVoted . "<br /><br />";
$file_handle = fopen("ip_log.txt", "r");
// LOOP THROUGH FILE IP ADDRRESSES AND DATES THOSE ADDRESSES WHERE LOGGED
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('||', $line_of_text);
$logIP = $parts[0];
$logDate = $parts[1];
// CHECK IF THE LOGGED IP ADDRESS MATCHES THE USERS IP ADDRESS
if ($logIP == $ipAddress) {
$userFound = true;
// CHECK IF THE LOGGED VOTE DATE MATCHES == TODAY FOR THE CURRENT IP ADDRESS BEING CHECKED
if ($logDate == $ipDate) {
// IP ADDRESS FOUND AND LOGGED DATE == TODAY, RREDIRECT USER TO TRY AGAIN LATER PAGE
header("Location: http://www.fotohuis.ca/vote/tryagain.html");
exit;
}
else {
// IP ADDRESS FOUND BUT LOGGED DATE != TODAY, KEEP CHECKING UNTIL EOF
}
}
else {
// IP ADDRESS NOT FOUND, KEEP CHECKING UNTIL EOF
continue;
}
}
// USER FOUND
if ($userFound == true) {
// USER FOUND BUT THE LOGGED VOTE DATE != TODAY
if ($userVoted == false) {
// GET IP ADDRESS LOG CONTENT
$filename1 = "ip_log.txt";
$content1 = file($filename1);
// INSERT USER IP ADDRESS INTO LOG
$insertip = "\r\n" . $ipAddress . "||" . $ipDate;
$fp1 = fopen($filename1, "a");
fputs($fp1, $insertip);
fclose($fp1);
// GET POLL RESULT CONTENT
$filename2 = "poll_result.txt";
$content = file($filename2);
// PUT VOTE CONTENT INTO ARRAY
$array = explode("||", $content[0]);
$picOne = $array[0];
$picTwo = $array[1];
$picThree = $array[2];
$picFour = $array[3];
$picFive = $array[4];
$picSix = $array[5];
// CALCULATE VOTE TOTAL
if ($userVote == 0) {
$picOne = $picOne + 1;
}
if ($userVote == 1) {
$picTwo = $picTwo + 1;
}
if ($userVote == 2) {
$picThree = $picThree + 1;
}
if ($userVote == 3) {
$picFour = $picFour + 1;
}
if ($userVote == 4) {
$picFive = $picFive + 1;
}
if ($userVote == 5) {
$picSix = $picSix + 1;
}
// INSERT USER VOTE
$insertvote = $picOne . "||" . $picTwo . "||" . $picThree . "||" . $picFour . "||" . $picFive . "||" . $picSix;
$fp2 = fopen($filename2, "w");
fputs($fp2, $insertvote);
fclose($fp2);
// /REDIRECT USER TO THANK YOU FOR VOTING PAGE
header("Location: http://www.fotohuis.ca/vote/thankyou.html");
exit;
}
// USER FOUND AND THE LOGGED VOTE DATE == TODAY
else {
// USER ATTEMPTED TO VOTE 2+ TIMES IN THE SAME DAY, REDIRECT USER TO TRY AGAIN LATER PAGE
header("Location: http://www.fotohuis.ca/vote/tryagain.html");
exit;
}
}
// USER NOT FOUND
else {
// GET IP ADDRESS LOG CONTENT
$filename1 = "ip_log.txt";
$content1 = file($filename1);
// INSERT USER IP ADDRESS INTO LOG
$insertip = "\r\n" . $ipAddress . "||" . $ipDate;
$fp1 = fopen($filename1, "a");
fputs($fp1, $insertip);
fclose($fp1);
// GET POLL RESULT CONTENT
$filename2 = "poll_result.txt";
$content2 = file($filename2);
// PUT VOTE COUNT INTO ARRAY
$array = explode("||", $content2[0]);
$picOne = $array[0];
$picTwo = $array[1];
$picThree = $array[2];
$picFour = $array[3];
$picFive = $array[4];
$picSix = $array[5];
// CALCULATE VOTE TOTAL
if ($userVote == 0) {
$picOne = $picOne + 1;
}
if ($userVote == 1) {
$picTwo = $picTwo + 1;
}
if ($userVote == 2) {
$picThree = $picThree + 1;
}
if ($userVote == 3) {
$picFour = $picFour + 1;
}
if ($userVote == 4) {
$picFive = $picFive + 1;
}
if ($userVote == 5) {
$picSix = $picSix + 1;
}
// INSERT USER VOTE
$insertvote = $picOne . "||" . $picTwo . "||" . $picThree . "||" . $picFour . "||" . $picFive . "||" . $picSix;
$fp2 = fopen($filename2, "w");
fputs($fp2, $insertvote);
fclose($fp2);
// REDIRECT USER TO THANK YOU FOR VOTING PAGE
header("Location: http://www.fotohuis.ca/vote/thankyou.html");
exit;
}
fclose($file_handle);
}
====
Any help with this will be greatly appreciated as I've been at it for a week now with no progress :(