105 lines
4.3 KiB
PHP
105 lines
4.3 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($username) || empty($email) || empty($password)) {
|
|
$error = 'Tutti i campi sono obbligatori.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$error = 'Formato email non valido.';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Check for existing user
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
|
|
$stmt->execute([$username, $email]);
|
|
if ($stmt->fetch()) {
|
|
$error = 'Username o email già esistenti.';
|
|
} else {
|
|
// Insert new user
|
|
$password_hash = password_hash($password, PASSWORD_BCRYPT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
|
|
if ($stmt->execute([$username, $email, $password_hash])) {
|
|
$success = 'Registrazione completata! Ora puoi effettuare il login.';
|
|
} else {
|
|
$error = 'Errore durante la registrazione. Riprova.';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error = "Errore del database: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="it">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Registrazione</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<style>
|
|
body {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
height: 100vh;
|
|
background-color: #f8f9fa;
|
|
}
|
|
.card {
|
|
width: 100%;
|
|
max-width: 400px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h3 class="card-title text-center">Registrazione</h3>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success"><?php echo $success; ?></div>
|
|
<?php endif; ?>
|
|
<a href="google-auth.php" class="btn btn-danger w-100 mb-3">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-google" viewBox="0 0 16 16">
|
|
<path d="M15.545 6.558a9.42 9.42 0 0 1 .139 1.626c0 2.434-.87 4.492-2.384 5.885h.002C11.978 15.292 10.158 16 8 16A8 8 0 1 1 8 0a7.689 7.689 0 0 1 5.352 2.082l-2.284 2.284A4.347 4.347 0 0 0 8 3.166c-2.087 0-3.86 1.408-4.492 3.25C2.806 7.48 2.5 8.522 2.5 9.5s.306 2.02.812 2.834c.632 1.842 2.405 3.25 4.492 3.25 1.257 0 2.34-.47 3.162-1.224l.064-.064a3.837 3.837 0 0 0 1.15-2.487H8v-2h7.545z"/>
|
|
</svg> Registrati con Google
|
|
</a>
|
|
<div class="d-flex align-items-center my-3">
|
|
<hr class="flex-grow-1">
|
|
<span class="px-2 text-muted">oppure</span>
|
|
<hr class="flex-grow-1">
|
|
</div>
|
|
<form method="POST" action="register.php">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email</label>
|
|
<input type="email" class="form-control" id="email" name="email" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password</label>
|
|
<input type="password" class="form-control" id="password" name="password" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Registrati</button>
|
|
</form>
|
|
<div class="text-center mt-3">
|
|
<p>Hai già un account? <a href="login.php">Accedi</a></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|