Skip to content

Commit b51537d

Browse files
authored
Prevent duplicate names in submissions
2 parents 9f3ddb7 + eedcc2a commit b51537d

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

index.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
$alreadySubmitted = isset($_COOKIE['already_submitted']);
3+
$errorMessage = '';
34

45
// Configuration settings
56
$maxDates = 6; // Maximum number of dates selectable - change this value to adjust the limit
@@ -19,16 +20,32 @@
1920
}
2021

2122
$data = json_decode(file_get_contents('db.json'), true) ?? [];
22-
foreach ($datesArray as $date) {
23-
$data[] = ['date' => trim($date), 'name' => $nome];
23+
24+
// Controlla se il nome esiste già
25+
$existingNames = array_unique(array_column($data, 'name'));
26+
$nomeNormalized = mb_strtolower($nome);
27+
$nameExists = false;
28+
foreach ($existingNames as $existingName) {
29+
if (mb_strtolower($existingName) === $nomeNormalized) {
30+
$nameExists = true;
31+
break;
32+
}
2433
}
25-
file_put_contents('db.json', json_encode($data));
2634

27-
// Imposta il cookie per 1 settimana
28-
setcookie('already_submitted', 'true', time() + (7 * 24 * 60 * 60));
35+
if ($nameExists) {
36+
$errorMessage = 'Questo nome è già stato utilizzato. Inserisci un nome diverso.';
37+
} else {
38+
foreach ($datesArray as $date) {
39+
$data[] = ['date' => trim($date), 'name' => $nome];
40+
}
41+
file_put_contents('db.json', json_encode($data));
42+
43+
// Imposta il cookie per 1 settimana
44+
setcookie('already_submitted', 'true', time() + (7 * 24 * 60 * 60));
2945

30-
header('Location: report.php');
31-
exit;
46+
header('Location: report.php');
47+
exit;
48+
}
3249
}
3350
}
3451
?>
@@ -141,10 +158,17 @@
141158
</h1>
142159

143160
<?php if (!$alreadySubmitted): ?>
161+
<?php if ($errorMessage): ?>
162+
<div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md mb-4 text-sm flex items-center">
163+
<i data-lucide="alert-circle" class="w-4 h-4 mr-2 flex-shrink-0"></i>
164+
<?= htmlspecialchars($errorMessage) ?>
165+
</div>
166+
<?php endif; ?>
167+
144168
<form method="POST" class="space-y-4">
145169
<div>
146170
<label for="nome" class="block text-sm font-medium text-slate-700 mb-1">Il tuo nome</label>
147-
<input type="text" id="nome" name="nome" required placeholder="Inserisci il tuo nome" class="w-full px-3 py-2.5 bg-slate-50 border border-slate-200 rounded-md text-sm text-slate-700 placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-slate-300 focus:border-slate-300">
171+
<input type="text" id="nome" name="nome" required placeholder="Inserisci il tuo nome" value="<?= htmlspecialchars($_POST['nome'] ?? '') ?>" class="w-full px-3 py-2.5 bg-slate-50 border border-slate-200 rounded-md text-sm text-slate-700 placeholder-slate-400 focus:outline-none focus:ring-2 focus:ring-slate-300 focus:border-slate-300<?= $errorMessage ? ' border-red-300' : '' ?>">
148172
</div>
149173

150174
<div class="flex items-center justify-end mb-1">

0 commit comments

Comments
 (0)