1

你能帮帮我吗?我在 php + mongodb 中制作我的投票系统,我想保留已经投票的 ip 地址。最好的方法是什么?我正在考虑这样做:

$ip=$_SERVER['REMOTE_ADDR'];
$ipData = array('$push' => array('ips' => $ip), '$inc' => array('votes' => 1));
$collection->update(array( '_id' => $id), $ipData);

这是最好的方法吗?您将如何比较ips数组的所有元素以查看该 ip 是否尚未投票?该列表将如下所示(192.168.0.1, 127.0.0.1, 123.45.67.8)

谢谢!

4

2 回答 2

2

使用唯一索引UPSERT

$collection->ensureIndex(array('ips'), array('unique' => true));
于 2011-05-02T14:32:05.340 回答
-3

使用 PHP 函数 in_array() 将用户的 ip 与 IP 数组进行比较

http://php.net/manual/en/function.in-array.php

于 2011-05-02T14:20:41.197 回答