69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
// --- Input Validation ---
|
|
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
|
header('Location: register.php');
|
|
exit();
|
|
}
|
|
|
|
$name = trim($_POST['name'] ?? '');
|
|
$usn = trim($_POST['usn'] ?? '');
|
|
$mobile = trim($_POST['mobile'] ?? '');
|
|
|
|
if (empty($name) || empty($usn) || empty($mobile)) {
|
|
// In a real app, show a proper error message
|
|
die('All fields are required.');
|
|
}
|
|
|
|
// --- Database Interaction ---
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Check if USN already exists
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE usn = ?");
|
|
$stmt->execute([$usn]);
|
|
if ($stmt->fetch()) {
|
|
// For simplicity, we'll just log them in and send to the game.
|
|
// A real app might show an error "USN already registered".
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE usn = ?");
|
|
$stmt->execute([$usn]);
|
|
$user = $stmt->fetch();
|
|
$_SESSION['user_id'] = $user['id'];
|
|
header('Location: level1.php'); // Redirect to level 1
|
|
exit();
|
|
}
|
|
|
|
// 1. Create a new User (without referral link first)
|
|
$sql = "INSERT INTO users (name, usn, mobile, referralCount, hasWonReward, rewardClaimed, gameLevel) VALUES (?, ?, ?, 0, 'no', 'no', 0)";
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([$name, $usn, $mobile]);
|
|
|
|
// 2. Get the new User's ID
|
|
$userId = $pdo->lastInsertId();
|
|
|
|
// 3. Generate and update the referralLink
|
|
// Note: This assumes the domain is http:// and uses the current host.
|
|
// In a production app, this should be a configurable value.
|
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
|
|
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
|
|
$referralLink = $protocol . '://' . $host . '?ref=' . $userId;
|
|
|
|
$updateSql = "UPDATE users SET referralLink = ? WHERE id = ?";
|
|
$updateStmt = $pdo->prepare($updateSql);
|
|
$updateStmt->execute([$referralLink, $userId]);
|
|
|
|
// 4. Log this user in by storing their ID in the session
|
|
$_SESSION['user_id'] = $userId;
|
|
|
|
// 5. Navigate to Level 1 (placeholder)
|
|
// We will create level1.php in a future step.
|
|
header('Location: level1.php');
|
|
exit();
|
|
|
|
} catch (PDOException $e) {
|
|
// In a real app, log this error and show a generic error page.
|
|
die("Database error: " . $e->getMessage());
|
|
}
|