我正在尝试构建一个简单的PHP投票系统,要求网站用户对自己喜欢的照片进行投票。每天只允许一票。
投票时,PHP脚本将:
====
1 - 检查用户的IP地址并将其与IP日志文本文件进行比较
2 - 如果在日志中找到用户IP地址,则会检查是否在当前日期提交了最后一次投票(针对该IP地址)
3 - 如果记录了用户的IP地址,并且最后一次投票是在当前日期,则用户被重定向到页面提醒他们每天只允许一次投票
4 - 如果记录了用户的IP地址但最后一次投票是在当前日期之前,则会记录投票并将用户重定向到感谢投票页面
5 - 如果未记录用户IP地址,则会记录投票,并将用户重定向到感谢投票页面。
====
不幸的是,我正在使用的PHP脚本将执行以下两项操作之一:
1 - 如果在if / else语句($ logIP == $ ipAddress)之后包含“break”,则脚本会在日志中添加两个投票和两个IP条目
2 - 如果删除“中断”,则输入一票并记录一个IP地址。但是,无论投票和IP被记录,新选民和返回的选票都被重定向到该页面,提醒他们每天只允许同一个用户进行一次投票
脚本如下:
====
// 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);
}
====
任何对此的帮助都将非常感激,因为我已经有一个星期了,但没有任何进展:(
答案 0 :(得分:0)
此代码可以大大简化,以便将来更容易理解和修改。还有一些重复的代码不需要。
这是未经测试的,但它应该有效,并让您了解如何继续前进。
在您尝试之前,请务必对其进行测试或备份您现有的投票数量!!!
<?php
$userVote = $_REQUEST['vote'];
$ipAddress = $_SERVER["REMOTE_ADDR"];
$ipDate = date("Y/m/d");
$ipLogFile = 'ip_log.txt';
$resultLogFile = 'poll_result.txt';
//IF THE USERS VOTE IS NULL or out of range, REDIRECT TO ERROR PAGE
if (empty($userVote) || $userVote < 0 || $userVote > 5) {
header('Location: errorpage.html');
exit;
}
$file_handle = fopen($ipLogFile, "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];
if ($logIP == $ipAddress && $logDate == $ipDate) {
// IP ADDRESS FOUND AND LOGGED DATE == TODAY FOR 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 for reading and writing
flock($fp, LOCK_EX); // lock file so it can't be changed
$content = fgets($fp); // read contents of file
//PUT VOTE CONTENT INTO ARRAY
$array = explode("||", $content);
// $array[0] = picOne; $array[1] = picTwo; etc
$array[$userVote] = (int)$array[$userVote] + 1;
ftruncate($fp); // truncate the file so it is empty
rewind($fp); // jump to beginning of file
$insertvote = implode('||', $array); // array to 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 TO THANK YOU FOR VOTING PAGE
header("Location: http://www.fotohuis.ca/vote/thankyou.html");
exit;
希望有所帮助!