How can PHP sessions be effectively utilized to prevent multiple voting from the same user?

To prevent multiple voting from the same user, PHP sessions can be utilized to track whether a user has already voted. By storing a flag in the session indicating that the user has voted, you can prevent them from voting again during the same session.

session_start();

if(isset($_SESSION['voted'])){
    // User has already voted, prevent them from voting again
    echo "You have already voted.";
} else {
    // Process the vote
    // Set the session flag to indicate that the user has voted
    $_SESSION['voted'] = true;
    echo "Thank you for voting!";
}