How can we prevent multiple votes in a voting script using cookies in PHP?

To prevent multiple votes in a voting script using cookies in PHP, you can set a cookie when a user casts a vote and check for the presence of this cookie before allowing another vote. If the cookie is present, you can prevent the user from voting again.

// Check if the user has already voted by checking for a specific cookie
if(isset($_COOKIE['voted'])){
    echo "You have already voted.";
} else {
    // Process the vote
    // Set a cookie to prevent multiple votes
    setcookie('voted', 'true', time() + (86400 * 30)); // Cookie expires in 30 days
    echo "Thank you for voting!";
}