I am having a survey and I want to prevent users from voting more than once. Does anyone have a code that I can include in the page
-
3First show us some code of yours. – Voitcus Mar 01 '13 at 10:32
-
2`if(userHasVoted){call_Ignore_Vote(true);}` – Nanne Mar 01 '13 at 10:33
-
Read this http://stackoverflow.com/q/1042247/1920232 – peterm Mar 01 '13 at 10:39
-
https://stackoverflow.com/questions/572723/stopping-users-voting-multiple-times-on-a-website/60477979 – James Mar 01 '20 at 17:49
2 Answers
Depending on your server/hosting setup, here are a few options:
Easy: Set a cookie on the browser when the user votes. Check the cookie before accepting a vote. Can easily circumevented (delete cookies)
Med: Get the IP and store it in a table. Check the table before accepting the vote. "hard" to circumvent (requeires proxies). May not be a good solution if you are accepting votes from people that are behind a NAT and have the same IP.
What I would do: Do not reinvent the wheel. Try surveymonkey another similar online service.

- 2,762
- 1
- 24
- 31
in order to prevent people from entering multiple answers, you want to use sessions (this implies that you have to put that page behind a login). Alternatively, you can use the IP.
However, the IP changes. This implies that, even if you use the IP only and store it somewhere (possibly encrypted), so that whoever has used that IP won't be able to answer again, you won't prevent me from answering again.
Have you considered enabling login with social networks, such as facbook and twitter or google+? It's an alternative to login.
If you're happy with the IP only, you can simply create a database table with IP entries. Let's say you have it already and that the table has three columns (id, ip, survey).
<?
//take the ip
$user = $_SERVER['REMOTE_ADDR'];
//sanitize the ip (is it valid?). This can probably skipped with prepared statements but one never knows
if (filter_var($user, FILTER_VALIDATE_IP)) {
echo "This (ip_a) IP address is considered valid."; //for debug
$user = md5( $user); //or whatever else if not md5
$query = $mysqli->prepare("insert into iptable values (NULL, ?, ?)");
//etc.
}else{
//return error
}
?>
I hope this helps. I'd prefer a login though.

- 845
- 1
- 7
- 14