How can unique IDs be generated and used to prevent users from participating in a survey more than once in PHP?

To prevent users from participating in a survey more than once, unique IDs can be generated for each user and stored in a database. When a user tries to access the survey, their unique ID can be checked against the database to see if they have already participated. If they have, they can be redirected away from the survey. Here is an example PHP code snippet to demonstrate this:

<?php
// Connect to database (replace with your database credentials)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "survey_db";

$conn = new mysqli($servername, $username, $password, $dbname);

// Generate a unique ID for the user
$unique_id = uniqid();

// Check if the user's unique ID already exists in the database
$sql = "SELECT * FROM survey_responses WHERE unique_id = '$unique_id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // User has already participated, redirect them away from the survey
    header("Location: survey_already_taken.php");
    exit();
} else {
    // User has not participated, proceed to the survey
    // Display survey form here
}
?>