<?php
require_once 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = htmlspecialchars(trim($_POST['username']));
$email = htmlspecialchars(trim($_POST['email']));
$password = htmlspecialchars(trim($_POST['password']));
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$token = bin2hex(random_bytes(16));
// Validation simple
if (empty($username) || empty($email) || empty($password)) {
die('Tous les champs sont requis.');
}
// Insertion dans la base de données
$stmt = $pdo->prepare('INSERT INTO user_session (username, email, password, token) VALUES (?, ?, ?, ?)');
if ($stmt->execute([$username, $email, $hashed_password, $token])) {
echo "Inscription réussie !";
} else {
echo "Erreur lors de l'inscription.";
}
}
?>
##db_conect##
<?php
$host = 'localhost';
$dbname = 'dbname';
$user = 'username';
$pass = 'password';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Erreur de connexion : " . $e->getMessage());
}
?>
##table##
CREATE TABLE user_session (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
token VARCHAR(32) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
JavaScript
document.getElementById('signup-form').addEventListener('submit', function (e) {
const password = document.getElementById('password').value;
const repassword = document.getElementById('repassword').value;
if (password !== repassword) {
e.preventDefault();
alert("Les mots de passe ne correspondent pas !");
}
});