?php require_once "config.php"; if (is_logged_in()) { header("Location: index.php"); exit; } $error = ""; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username = trim($_POST['username'] ?? ''); $password = $_POST['password'] ?? ''; if ($username === '' || $password === '') { $error = "Username aur password dono zaroori hain."; } elseif (strlen($password) < 4) { $error = "Password kam se kam 4 characters ka ho."; } else { $stmt = $conn->prepare("SELECT id FROM users WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows > 0) { $error = "Ye username pehle se le liya gaya hai."; } else { $hash = password_hash($password, PASSWORD_DEFAULT); $ins = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $ins->bind_param("ss", $username, $hash); if ($ins->execute()) { $_SESSION['user_id'] = $ins->insert_id; $_SESSION['username'] = $username; header("Location: index.php"); exit; } else { $error = "Kuch galat ho gaya, dobara try karein."; } $ins->close(); } $stmt->close(); } } ?>