PHP Voting System Redirect Not Working -
i'm attempting build simple php voting system website user asked vote on favorite photo. 1 vote permitted each day.
when vote cast php script will:
====
1 -- checks users ip address , compares ip log text file
2 -- if users ip address found in log checks see if last vote (for ip address) submitted on current date
3 -- if users ip address logged , last vote on current date user redirected page reminding them 1 vote allowed per day
4 -- if users ip address logged last vote prior current date, vote recorded , user redirected thank voting page
5 -- if user ip address not logged vote recorded , user redirected thank voting page.
====
unfortunately php script i'm working 1 of 2 things:
1 -- if "break" included after if/else statement ($logip == $ipaddress), script adds 2 votes , 2 ip entries log
2 -- if "break" removed, 1 vote entered , 1 ip address recorded. however, regardless of vote , ip being recorded, new voters , returning votes redirected page reminding them 1 vote permitted each day same user
the script follows:
====
// users vote, ip address , todays date $uservote = $_request['vote']; $ipaddress = $_server["remote_addr"]; $ipdate = date("y/m/d"); // if users vote null, redirect 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 , dates addresses logged while (!feof($file_handle)) { $line_of_text = fgets($file_handle); $parts = explode('||', $line_of_text); $logip = $parts[0]; $logdate = $parts[1]; // check if logged ip address matches users ip address if ($logip == $ipaddress) { $userfound = true; // check if logged vote date matches == today current ip address being checked if ($logdate == $ipdate) { // ip address found , logged date == today, rredirect user try again later page header("location: http://www.fotohuis.ca/vote/tryagain.html"); exit; } else { // ip address found logged date != today, keep checking until eof } } else { // ip address not found, keep checking until eof continue; } } // user found if ($userfound == true) { // user found logged vote date != today if ($uservoted == false) { // ip address log content $filename1 = "ip_log.txt"; $content1 = file($filename1); // insert user ip address log $insertip = "\r\n" . $ipaddress . "||" . $ipdate; $fp1 = fopen($filename1, "a"); fputs($fp1, $insertip); fclose($fp1); // poll result content $filename2 = "poll_result.txt"; $content = file($filename2); // put vote content 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 thank voting page header("location: http://www.fotohuis.ca/vote/thankyou.html"); exit; } // user found , logged vote date == today else { // user attempted vote 2+ times in same day, redirect user try again later page header("location: http://www.fotohuis.ca/vote/tryagain.html"); exit; } } // user not found else { // ip address log content $filename1 = "ip_log.txt"; $content1 = file($filename1); // insert user ip address log $insertip = "\r\n" . $ipaddress . "||" . $ipdate; $fp1 = fopen($filename1, "a"); fputs($fp1, $insertip); fclose($fp1); // poll result content $filename2 = "poll_result.txt"; $content2 = file($filename2); // put vote count 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 thank voting page header("location: http://www.fotohuis.ca/vote/thankyou.html"); exit; } fclose($file_handle); } ====
any appreciated i've been @ week no progress :(
this code can simplified make easier understand , modify in future. there repeated code doesn't need be.
this untested should work , give idea on how move forward.
before try make sure test or backup existing vote counts!!!
<?php $uservote = $_request['vote']; $ipaddress = $_server["remote_addr"]; $ipdate = date("y/m/d"); $iplogfile = 'ip_log.txt'; $resultlogfile = 'poll_result.txt'; //if users vote null or out of range, redirect error page if (empty($uservote) || $uservote < 0 || $uservote > 5) { header('location: errorpage.html'); exit; } $file_handle = fopen($iplogfile, "r"); //loop through file ip addrresses , dates addresses logged while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $parts = explode('||', $line_of_text); $logip = $parts[0]; $logdate = $parts[1]; if ($logip == $ipaddress && $logdate == $ipdate) { // ip address found , logged date == today current ip header("location: http://www.fotohuis.ca/vote/tryagain.html"); exit; } } fclose($file_handle); // user has not voted ever, or today $insertip = "\r\n".$ipaddress."||".$ipdate; file_put_contents($iplogfile, $insertip, file_append); //get poll result content $fp = fopen($resultlogfile, 'r+'); // open reading , writing flock($fp, lock_ex); // lock file can't changed $content = fgets($fp); // read contents of file //put vote content array $array = explode("||", $content); // $array[0] = picone; $array[1] = pictwo; etc $array[$uservote] = (int)$array[$uservote] + 1; ftruncate($fp); // truncate file empty rewind($fp); // jump beginning of file $insertvote = implode('||', $array); // array string 0||1||2||3||4||5 fwrite($fp, $insertvote); // write new vote tally flock($fp, lock_un); // unlock file fclose($fp); // close file ///redirect user thank voting page header("location: http://www.fotohuis.ca/vote/thankyou.html"); exit; hope helps!
Comments
Post a Comment