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?
Advertisement
Answer
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 }