59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$fullName = $_POST['fullName'] ?? '';
|
|
$birthDate = $_POST['birthDate'] ?? '';
|
|
$email = $_POST['email'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
$confirmPassword = $_POST['confirmPassword'] ?? '';
|
|
|
|
// Validation
|
|
if (empty($fullName) || empty($birthDate) || empty($email) || empty($password) || empty($confirmPassword)) {
|
|
$_SESSION['error_message'] = 'Todos os campos são obrigatórios.';
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
if ($password !== $confirmPassword) {
|
|
$_SESSION['error_message'] = 'As senhas não correspondem.';
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$_SESSION['error_message'] = 'Formato de e-mail inválido.';
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Check if user already exists
|
|
$stmt = $pdo->prepare('SELECT id FROM users WHERE email = ?');
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$_SESSION['error_message'] = 'Este e-mail já está cadastrado.';
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
// Insert new user
|
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare('INSERT INTO users (username, email, password, role, birth_date) VALUES (?, ?, ?, ?, ?)');
|
|
$stmt->execute([$fullName, $email, $hashedPassword, 'atleta', $birthDate]);
|
|
|
|
$_SESSION['success_message'] = 'Cadastro realizado com sucesso! Você já pode fazer o login.';
|
|
header('Location: index.php');
|
|
exit;
|
|
|
|
} catch (PDOException $e) {
|
|
$_SESSION['error_message'] = 'Erro no banco de dados. Tente novamente mais tarde.';
|
|
// In a real app, you would log this error: error_log($e->getMessage());
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
}
|