它是关于这种情况的:
我有2张照片,在每张照片下,您可以通过投票来"喜欢"或"不喜欢"照片。为了防止每个用户多次投票,我存储了用户的IP地址。因此,使用相同的IP,用户每张照片只能投票一次。我现在的问题是:如果在照片1下单击像单击一样,他/她不再喜欢或不喜欢Photo2。这应该是可能的。
这是我拥有的HTML代码:
<!-- here is photo 1 with below the buttons-->
<button id="like-btn" class="click-trigger" data-click-id="like">Like</button>
<span id="like" class="click-count"><?php echo $count['like'];?></span> likes.
<br/><br/>
<button id="dislike-btn" class="click-trigger" data-click-id="dislike">Dislike</button>
<span id="dislike" class="click-count"><?php echo $count['dislike'];?> </span> dislikes.
<br/><br/>
<!-- here is photo2 with below the buttons-->
<button id="like-btn" class="click-trigger" data-click-id="like2">Like</button>
<span id="like" class="click-count"><?php echo $count['like2'];?></span> likes.
<br/><br/>
和PHP代码:
// grab users IP address
$ipAddress = $_SERVER['REMOTE_ADDR'];
$UserIptxt = $ipAddress."||";
$all_ips = explode("||", file_get_contents("user_ip.txt"));
if ( ! in_array($ipAddress, $all_ips) ) {
// put the ip address in .txt file
file_put_contents("user_ip.txt", $UserIptxt, FILE_APPEND);
$file = 'counter.txt'; // path to text file that stores counts
$fh = fopen($file, 'r+');
$id = $_REQUEST['id']; // posted from page
$lines = '';
while(!feof($fh)){
$line = explode('||', fgets($fh));
$item = trim($line[0]);
$num = trim($line[1]);
if(!empty($item)){
if($item == $id){
$num++; // increment count by 1
echo $num;
echo '<br />Thanks for voting';
}
$lines .= "$item||$numrn";
}
}
file_put_contents($file, $lines);
fclose($fh);
}
// ip adres is already stored
else {
echo '<br /><div class="alreadyvoted">You have already voted!</div>';
}
文件counter.txt
看起来像这样:
like||117
dislike||184
like2||12
dislike2||18
如何区分每张照片的存储IP地址?
简短的答案,使用此方法,每个照片都需要您在IP地址保存的唯一ID。也许以
之类的形式将其保存在文件中127.0.0.0=photo1
127.0.0.0=photo2
然后爆炸每行
list ($ipAddr,$photoID) = explode ("#", $line);
然后比较传递的光子和IP。
可能希望可能使用数据库来研究。这可能会更容易创建条目,并使用Where子句绘制此信息。