34109-vm/login.php
2025-09-19 12:49:02 +00:00

181 lines
8.6 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$error = null;
$phone_number_for_registration = null;
// Handle Registration Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['register'])) {
$phone_number = $_POST['phone_number'] ?? null;
$name = $_POST['name'] ?? null;
$nickname = $_POST['nickname'] ?? null;
$positions = $_POST['positions'] ?? ['Sub'];
$photo_path = null;
if (isset($_FILES['photo']) && $_FILES['photo']['error'] == 0) {
$target_dir = "assets/images/users/";
if (!is_dir($target_dir)) {
mkdir($target_dir, 0755, true);
}
$file_extension = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
$safe_filename = preg_replace('/[^a-zA-Z0-9_.-]/', '_', basename($_FILES['photo']['name']));
$target_file = $target_dir . uniqid() . '-' . $safe_filename;
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target_file)) {
$photo_path = $target_file;
}
}
if ($phone_number && $name && $positions) {
try {
$pdoconn = db();
$pdoconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$position_str = implode(', ', $positions);
$sql = "INSERT INTO users (phone_number, name, nickname, position, photo, joined_date, role) VALUES (:phone_number, :name, :nickname, :position, :photo, CURDATE(), 'player')";
$stmt = $pdoconn->prepare($sql);
$stmt->bindParam(':phone_number', $phone_number);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':nickname', $nickname);
$stmt->bindParam(':position', $position_str);
$stmt->bindParam(':photo', $photo_path);
$stmt->execute();
$user_id = $pdoconn->lastInsertId();
$_SESSION['user_id'] = $user_id;
header("Location: index.php");
exit();
} catch (PDOException $e) {
$error = "Database error during registration: " . $e->getMessage();
}
} else {
$error = "Please fill out all required fields.";
$phone_number_for_registration = $phone_number; // Keep phone number for the form
}
}
// Handle Phone Number Lookup
else if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['phone'])) {
$phone_number = $_POST['phone'];
try {
$pdoconn = db();
$pdoconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM users WHERE phone_number = :phone_number";
$stmt = $pdoconn->prepare($sql);
$stmt->bindParam(':phone_number', $phone_number);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
$_SESSION['user_id'] = $user['id'];
header("Location: index.php");
exit();
} else {
// User not found, set phone number to show registration form
$phone_number_for_registration = $phone_number;
}
} catch (PDOException $e) {
$error = "Database error: " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login / Register</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h3 class="card-title text-center mb-4">Login or Register</h3>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if (!$phone_number_for_registration): ?>
<!-- Step 1: Phone Number Form -->
<form action="login.php" method="POST">
<div class="mb-3">
<label for="phone" class="form-label">Phone Number</label>
<input type="tel" class="form-control" id="phone" name="phone" required placeholder="Enter your phone number">
</div>
<button type="submit" class="btn btn-primary w-100">Continue</button>
</form>
<?php else: ?>
<!-- Step 2: Registration Form -->
<h4 class="text-center mb-3">Welcome! Let's get you set up.</h4>
<form action="login.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="register" value="1">
<input type="hidden" name="phone_number" value="<?php echo htmlspecialchars($phone_number_for_registration); ?>">
<div class="mb-3">
<label class="form-label">Phone Number</label>
<input type="text" class="form-control" value="<?php echo htmlspecialchars($phone_number_for_registration); ?>" disabled>
</div>
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="nickname" class="form-label">Nickname (Optional)</label>
<input type="text" class="form-control" id="nickname" name="nickname">
</div>
<div class="mb-3">
<label class="form-label">Position(s)</label>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="positions[]" value="GK" id="pos_gk">
<label class="form-check-label" for="pos_gk">GK</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="positions[]" value="Defender" id="pos_def">
<label class="form-check-label" for="pos_def">Defender</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="positions[]" value="Midfield" id="pos_mid">
<label class="form-check-label" for="pos_mid">Midfield</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="positions[]" value="Forward" id="pos_fwd">
<label class="form-check-label" for="pos_fwd">Forward</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="positions[]" value="Sub" id="pos_sub" checked>
<label class="form-check-label" for="pos_sub">Sub</label>
</div>
</div>
<div class="mb-3">
<label for="photo" class="form-label">Photo (Optional)</label>
<input type="file" class="form-control" id="photo" name="photo" accept="image/*">
</div>
<button type="submit" class="btn btn-success w-100">Register</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>