<?php

$fichier = "messages.json";

// Charger les données existantes
if (file_exists($fichier)) {
    $data = json_decode(file_get_contents($fichier), true);
    if (!is_array($data)) {
        $data = [];
    }
} else {
    $data = [];
}

// Ajouter la nouvelle entrée (vulnérable XSS volontaire pour TP)
$data[] = [
    "nom" => $_POST['nom'],
    "comment" => $_POST['comment']
];

// Sauvegarder
file_put_contents($fichier, json_encode($data, JSON_PRETTY_PRINT));

// Redirection automatique vers le formulaire
header("Location: form2.php");
exit;