Créez un formulaire PHP fonctionnel avec validation
filter_var()
pour valider l'emailpassword_hash()
htmlspecialchars()
<?php session_start(); ?> <!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <title>Inscription</title> </head> <body> <?php if (isset($_SESSION['errors'])): ?> <div class="errors"> <?php foreach($_SESSION['errors'] as $error): ?> <p><?= htmlspecialchars($error) ?></p> <?php endforeach; ?> </div> <?php unset($_SESSION['errors']); endif; ?> <form action="traitement.php" method="post"> <input type="text" name="prenom" placeholder="Prénom" required value="<?= htmlspecialchars($_SESSION['form_data']['prenom'] ?? '') ?>"> <input type="text" name="nom" placeholder="Nom" required value="<?= htmlspecialchars($_SESSION['form_data']['nom'] ?? '') ?>"> <input type="email" name="email" placeholder="Email" required value="<?= htmlspecialchars($_SESSION['form_data']['email'] ?? '') ?>"> <input type="password" name="password" placeholder="Mot de passe" required minlength="8"> <input type="password" name="confirm_password" placeholder="Confirmation" required> <input type="checkbox" name="cgu" id="cgu" required> <label for="cgu">J'accepte les CGU</label> <button type="submit">S'inscrire</button> </form> </body> </html>
<?php session_start(); // Réinitialiser les données de formulaire et erreurs $_SESSION['form_data'] = $_POST; $_SESSION['errors'] = []; // Validation if (empty($_POST['prenom'])) { $_SESSION['errors'][] = "Le prénom est obligatoire"; } if (empty($_POST['nom'])) { $_SESSION['errors'][] = "Le nom est obligatoire"; } if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $_SESSION['errors'][] = "Email invalide"; } if (strlen($_POST['password']) < 8) { $_SESSION['errors'][] = "Le mot de passe doit faire 8 caractères minimum"; } if ($_POST['password'] !== $_POST['confirm_password']) { $_SESSION['errors'][] = "Les mots de passe ne correspondent pas"; } if (!isset($_POST['cgu'])) { $_SESSION['errors'][] = "Vous devez accepter les CGU"; } // Si erreurs, rediriger vers le formulaire if (!empty($_SESSION['errors'])) { header('Location: inscription.php'); exit; } // Si validation OK require 'config.php'; try { $pdo = new PDO( "mysql:host={$config['db']['host']};dbname={$config['db']['name']};charset=utf8", $config['db']['user'], $config['db']['pass'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] ); // Vérifier si l'email existe déjà $stmt = $pdo->prepare("SELECT id FROM utilisateurs WHERE email = ?"); $stmt->execute([$_POST['email']]); if ($stmt->fetch()) { $_SESSION['errors'][] = "Cet email est déjà utilisé"; header('Location: inscription.php'); exit; } // Hachage du mot de passe $passwordHash = password_hash($_POST['password'], PASSWORD_DEFAULT); // Insertion en base $stmt = $pdo->prepare("INSERT INTO utilisateurs (prenom, nom, email, password) VALUES (?, ?, ?, ?)"); $stmt->execute([ htmlspecialchars($_POST['prenom']), htmlspecialchars($_POST['nom']), filter_var($_POST['email'], FILTER_SANITIZE_EMAIL), $passwordHash ]); // Redirection vers confirmation $_SESSION['inscription_success'] = true; unset($_SESSION['form_data']); header('Location: confirmation.php'); exit; } catch (PDOException $e) { $_SESSION['errors'][] = "Erreur technique : " . $e->getMessage(); header('Location: inscription.php'); exit; }
<?php session_start(); if (!isset($_SESSION['inscription_success'])) { header('Location: inscription.php'); exit; } unset($_SESSION['inscription_success']); ?> <!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <title>Confirmation</title> </head> <body> <h1>Inscription réussie !</h1> <p>Merci pour votre inscription.</p> <a href="connexion.php">Se connecter</a> </body> </html>