What best practices should be followed when designing PHP scripts to handle user voting processes, considering security and reliability?

When designing PHP scripts to handle user voting processes, it is important to implement security measures to prevent manipulation of votes and ensure reliability of the voting system. This can be achieved by using techniques such as input validation, CSRF protection, and proper error handling.

// Example PHP code snippet implementing security measures for user voting processes

// Validate input to ensure only legitimate values are accepted
if(isset($_POST['vote']) && ($_POST['vote'] == 'up' || $_POST['vote'] == 'down')) {
    // CSRF protection - generate and validate a token
    if (isset($_POST['token']) && $_POST['token'] === $_SESSION['token']) {
        // Process the vote
        $vote = $_POST['vote'];
        // Code to handle the voting process goes here
    } else {
        // Invalid token - handle error
        echo "Invalid token";
    }
} else {
    // Invalid input - handle error
    echo "Invalid vote";
}