How can you prevent a user from seeing the same questionnaire twice in PHP?

To prevent a user from seeing the same questionnaire twice in PHP, you can store the questionnaire IDs that the user has already seen in a session variable. When generating a new questionnaire for the user, you can check if the ID is in the session variable and skip it if it is. This way, the user will not see the same questionnaire again.

session_start();

// List of questionnaire IDs
$questionnaireIds = [1, 2, 3, 4, 5];

// Check if user has seen any questionnaires
if (!isset($_SESSION['seen_questionnaires'])) {
    $_SESSION['seen_questionnaires'] = [];
}

// Get a random questionnaire for the user
$randomQuestionnaireId = array_diff($questionnaireIds, $_SESSION['seen_questionnaires']);
$randomQuestionnaireId = array_values($randomQuestionnaireId)[array_rand($randomQuestionnaireId)];

// Mark the questionnaire as seen
$_SESSION['seen_questionnaires'][] = $randomQuestionnaireId;

// Display the questionnaire
echo "Display questionnaire with ID: " . $randomQuestionnaireId;