I am doing a poll voting system and I want each device to have only one chance to vote. How can I do that using PHP?
Asked
Active
Viewed 173 times
2 Answers
1
PHP cannot get MAC, only can get client ip with global variable $_SERVER['REMOTE_ADDR']
.

Henry Trần
- 1,286
- 12
- 14
-
What should I do if, from one network are trying to vote multiple users? – Victor Dec 12 '20 at 11:50
0
this will get value from the environment variable
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
use of cookies
//if user vote first time and successfully then set $vote = "success"
then set cookie variable for that device for example
if($vote == "success"){
setcookie("isvote", "true");
}
// to check the user voted or not you can simply check the cookie variable is exist or not. // here is the final code that you can use
if (isset($_COOKIE["isvote"])){
echo "you can vote only one time";
}else{
if($vote == "success"){
setcookie("isvote", "true");
}
}
with ip address and cookies you can analyse that from which network the vote cast..
you can also save the user id or set count for that user to know that how many time user try to vote. you can add statements in this condition ->
if (isset($_COOKIE["isvote"])){
// some statements that add or update counter in database
}

Akshay Nayka
- 336
- 2
- 7
-
Thank you. Also, can you tell me what happens when from one network are trying to vote multiple users? – Victor Dec 12 '20 at 11:52
-
same network gives same ip address. you can use cookies to identify that user. i am updating the answer to use cookies in your page – Akshay Nayka Dec 14 '20 at 05:10
-
This answer does not cover: "*... I want each device to have only one chance to vote ...*". Never trust the client. Cookies are stored on the client side, not server side. The client can delete the cookie and vote again. – Definitely not Rafal Dec 16 '20 at 12:30