Editing: /home/goacom/public_html/scratch_extract/login.php
<?php session_start(); // --- Database Configuration --- $host = 'localhost'; $db = 'tishanw1_melbets'; $user = 'tishanw1_melbets'; $pass = 'tishanw1_melbets'; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); } catch (\PDOException $e) { die("Database connection failed: " . $e->getMessage()); } // --- Redirect if already logged in --- if (isset($_SESSION['user_id'])) { header("Location: dashboard.php"); exit; } $error_message = ""; // Check for referral in URL (e.g., login.php?r=123456) $url_referral = isset($_GET['r']) ? htmlspecialchars($_GET['r']) : ''; // --- Helper Function: Get Real IP Address --- function get_client_ip() { $ipaddress = ''; if (isset($_SERVER['HTTP_CLIENT_IP'])) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_X_FORWARDED'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_FORWARDED'])) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if(isset($_SERVER['REMOTE_ADDR'])) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; } // --- Log Referral Click --- if (!empty($url_referral)) { try { $ip = get_client_ip(); // Check if a click was logged from this IP for this code in the last 1 hour $chk = $pdo->prepare("SELECT id FROM referral_clicks WHERE referrer_code = ? AND ip_address = ? AND created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)"); $chk->execute([$url_referral, $ip]); if ($chk->rowCount() == 0) { $ins = $pdo->prepare("INSERT INTO referral_clicks (referrer_code, ip_address, created_at) VALUES (?, ?, NOW())"); $ins->execute([$url_referral, $ip]); } } catch (\PDOException $e) { // ignore errors } } // --- Helper Function: Generate Unique Referral Code --- function generateUniqueReferral($pdo) { $unique = false; while (!$unique) { // Generate a 6-digit random number (e.g., 919098) $code = rand(100000, 999999); $stmt = $pdo->prepare("SELECT id FROM users WHERE referral_code = ?"); $stmt->execute([$code]); if ($stmt->rowCount() == 0) { $unique = true; return $code; } } } // --- Handle Login POST --- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'login') { $phone = trim($_POST['phone']); $password = $_POST['password']; $stmt = $pdo->prepare("SELECT id, password FROM users WHERE phone = ?"); $stmt->execute([$phone]); $user = $stmt->fetch(); if ($user && password_verify($password, $user['password'])) { // 1. Get User IP $user_ip = get_client_ip(); // 2. Update Last Login IP and Time in Database $updateStmt = $pdo->prepare("UPDATE users SET last_login_ip = ?, last_login_time = NOW() WHERE id = ?"); $updateStmt->execute([$user_ip, $user['id']]); // 3. Set Session $_SESSION['user_id'] = $user['id']; $_SESSION['phone'] = $phone; header("Location: dashboard.php"); exit; } else { $error_message = "Invalid phone number or PIN."; } } // --- Handle Registration POST --- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'register') { $phone = trim($_POST['phone']); $password = $_POST['password']; $confirm = $_POST['confirm_password']; // This is the code the user entered (The "Refered By" code) $referral_input = isset($_POST['referral']) ? trim($_POST['referral']) : null; // Server-side Validation if ($password !== $confirm) { $error_message = "Passwords do not match."; } else { // Check if phone exists $stmt = $pdo->prepare("SELECT id FROM users WHERE phone = ?"); $stmt->execute([$phone]); if ($stmt->rowCount() > 0) { $error_message = "Phone number already registered."; } else { // 1. Generate a unique referral code for THIS NEW user (Their "My Code") $my_referral_code = generateUniqueReferral($pdo); // Hash password $hashedPassword = password_hash($password, PASSWORD_DEFAULT); // 2. Insert into Database // 'referral_code' = My generated code // 'referred_by' = The code I entered during registration (Who invited me) $stmt = $pdo->prepare("INSERT INTO users (phone, password, referral_code, referred_by, created_at) VALUES (?, ?, ?, ?, NOW())"); if ($stmt->execute([$phone, $hashedPassword, $my_referral_code, $referral_input])) { // Auto-login after registration $_SESSION['user_id'] = $pdo->lastInsertId(); $_SESSION['phone'] = $phone; header("Location: dashboard.php"); exit; } else { $error_message = "Registration failed. Please try again."; } } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Melbete Login</title> <!-- Tailwind CSS CDN --> <script src="https://cdn.tailwindcss.com"></script> <!-- Google Fonts --> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet"> <style> body { font-family: 'Inter', sans-serif; background-color: #000; } /* Toast Notification */ #toast { visibility: hidden; min-width: 250px; background-color: #e53e3e; /* Red for error */ color: #fff; text-align: center; border-radius: 50px; padding: 16px; position: fixed; z-index: 100; left: 50%; bottom: 30px; transform: translateX(-50%); box-shadow: 0 4px 12px rgba(0,0,0,0.5); opacity: 0; transition: opacity 0.3s, bottom 0.3s; } #toast.show { visibility: visible; opacity: 1; bottom: 50px; } #toast.success { background-color: #38a169; /* Green for success */ } </style> </head> <body class="bg-black text-white"> <!-- Main Modal Container --> <div class="fixed inset-0 bg-black text-white z-50 overflow-auto transition-opacity duration-300 opacity-100"> <div class="min-h-screen px-6 py-8"> <!-- Header --> <div class="flex items-center justify-between mb-12"> <button class="text-white p-2 hover:bg-white/10 rounded-full transition-colors" onclick="history.back()"> <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path> </svg> </button> <div class="flex-1 flex justify-center"> <div class="text-3xl font-bold tracking-wider text-white">MELB<span class="text-[#f06827]">ETE</span></div> </div> <div class="w-10"></div> </div> <!-- Tabs --> <div class="flex mb-8 bg-[#f06827] rounded-lg p-1 max-w-md mx-auto"> <button id="tab-login" onclick="switchTab('login')" class="flex-1 py-3 px-4 rounded-md text-sm font-medium transition-colors bg-[#d04f11] text-white shadow-sm"> Log in </button> <button id="tab-register" onclick="switchTab('register')" class="flex-1 py-3 px-4 rounded-md text-sm font-medium transition-colors text-white/80 hover:text-white"> Registration </button> </div> <!-- LOGIN SECTION --> <div id="view-login" class="max-w-md mx-auto transition-all duration-500 opacity-100 translate-y-0"> <form id="form-login" method="POST" action="login.php" onsubmit="return validateLogin(event)"> <input type="hidden" name="action" value="login"> <div class="space-y-6"> <!-- Phone Input Area --> <div class="relative"> <div class="flex items-baseline border-b-2 border-[#f06827] focus-within:border-[#f06827] transition-colors pb-1"> <div class="flex items-center px-2 cursor-pointer bg-transparent"> <img src="https://flagcdn.com/w20/bd.png" alt="BD" class="w-6 h-4 mr-1 object-cover"> <span class="text-sm font-bold text-[#f06827]">+880</span> <svg class="w-3 h-3 ml-1 text-[#f06827]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg> </div> <input type="tel" name="phone" placeholder="Mobile number" required class="flex-1 px-2 py-4 bg-transparent border-0 text-white placeholder-gray-500 focus:outline-none focus:text-white transition-colors text-base"> </div> </div> <!-- Pin Input --> <div class="relative"> <input type="password" id="login-pass" name="password" placeholder="Pin" required class="w-full px-0 py-4 bg-transparent border-0 border-b-2 border-[#f06827] text-white placeholder-gray-500 focus:outline-none focus:border-[#f06827] focus:text-white transition-colors text-base pr-10"> <button type="button" onclick="togglePassword('login-pass', this)" class="absolute right-3 top-1/2 transform -translate-y-1/2 text-[#f06827]"> <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg> </button> </div> <!-- Options --> <div class="flex items-center justify-between"> <label class="flex items-center space-x-3 cursor-pointer"> <div class="relative"> <input type="checkbox" class="sr-only peer"> <div class="w-5 h-5 border-2 rounded border-[#f06827] peer-checked:bg-[#f06827] flex items-center justify-center transition-colors"></div> </div> <span class="text-white text-sm">Remember</span> </label> <a href="#" class="text-[#f06827] hover:underline text-sm">Forgot password?</a> </div> <!-- Submit Button --> <button type="submit" class="w-full py-4 bg-[#f06827] rounded-full text-white font-semibold transition-all text-base shadow-lg hover:bg-[#d04f11] active:scale-95"> Log in </button> </div> </form> <p class="text-center text-gray-400 mt-8"> Don't have an account yet? <button onclick="switchTab('register')" class="text-[#f06827] hover:underline font-medium">Registration</button> </p> </div> <!-- REGISTRATION SECTION (Hidden by default) --> <div id="view-register" class="max-w-md mx-auto hidden opacity-0 translate-y-4"> <form id="form-register" method="POST" action="login.php" onsubmit="return validateRegister(event)"> <input type="hidden" name="action" value="register"> <div class="space-y-5"> <!-- Phone --> <div class="relative"> <div class="flex items-baseline border-b-2 border-[#f06827] focus-within:border-[#f06827] transition-colors pb-1"> <div class="flex items-center px-2 cursor-pointer bg-transparent"> <img src="https://flagcdn.com/w20/bd.png" alt="BD" class="w-6 h-4 mr-1 object-cover"> <span class="text-sm font-bold text-[#f06827]">+880</span> </div> <input type="tel" id="reg-phone" name="phone" placeholder="Mobile number" required class="flex-1 px-2 py-4 bg-transparent border-0 text-white placeholder-gray-500 focus:outline-none focus:text-white transition-colors text-base"> </div> </div> <!-- Password --> <div class="relative"> <input type="password" id="reg-pass" name="password" placeholder="Password" required class="w-full px-0 py-4 bg-transparent border-0 border-b-2 border-[#f06827] text-white placeholder-gray-500 focus:outline-none focus:border-[#f06827] focus:text-white transition-colors text-base pr-10"> <button type="button" onclick="togglePassword('reg-pass', this)" class="absolute right-3 top-1/2 transform -translate-y-1/2 text-[#f06827]"> <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg> </button> </div> <!-- Confirm Password --> <div class="relative"> <input type="password" id="reg-confirm" name="confirm_password" placeholder="Confirm Password" required class="w-full px-0 py-4 bg-transparent border-0 border-b-2 border-[#f06827] text-white placeholder-gray-500 focus:outline-none focus:border-[#f06827] focus:text-white transition-colors text-base"> </div> <!-- Referral Code (Auto-filled from URL if present) --> <div class="relative"> <input type="text" id="reg-referral" name="referral" placeholder="Referral code (optional)" value="<?php echo $url_referral; ?>" class="w-full px-0 py-4 bg-transparent border-0 border-b-2 border-gray-700 text-white placeholder-gray-500 focus:outline-none focus:border-[#f06827] focus:text-white transition-colors text-base"> </div> <!-- Submit Button --> <button type="submit" class="w-full py-4 bg-[#f06827] rounded-full text-white font-semibold transition-all text-base shadow-lg hover:bg-[#d04f11] active:scale-95 mt-4"> Registration </button> </div> </form> <p class="text-center text-gray-400 mt-8"> Already have an account? <button onclick="switchTab('login')" class="text-[#f06827] hover:underline font-medium">Log in</button> </p> </div> </div> </div> <!-- Toast Component --> <div id="toast">Notification</div> <script> // --- Tab Switching Logic --- function switchTab(tab) { const loginView = document.getElementById('view-login'); const registerView = document.getElementById('view-register'); const loginTab = document.getElementById('tab-login'); const registerTab = document.getElementById('tab-register'); if (tab === 'login') { // UI State for Login loginTab.classList.remove('text-white/80', 'hover:text-white'); loginTab.classList.add('bg-[#d04f11]', 'text-white', 'shadow-sm'); registerTab.classList.remove('bg-[#d04f11]', 'text-white', 'shadow-sm'); registerTab.classList.add('text-white/80', 'hover:text-white'); // Transition Views registerView.classList.add('opacity-0', 'translate-y-4'); setTimeout(() => { registerView.classList.add('hidden'); loginView.classList.remove('hidden'); setTimeout(() => { loginView.classList.remove('opacity-0', 'translate-y-4'); }, 10); }, 300); } else { // UI State for Register registerTab.classList.remove('text-white/80', 'hover:text-white'); registerTab.classList.add('bg-[#d04f11]', 'text-white', 'shadow-sm'); loginTab.classList.remove('bg-[#d04f11]', 'text-white', 'shadow-sm'); loginTab.classList.add('text-white/80', 'hover:text-white'); // Transition Views loginView.classList.add('opacity-0', 'translate-y-4'); setTimeout(() => { loginView.classList.add('hidden'); registerView.classList.remove('hidden'); setTimeout(() => { registerView.classList.remove('opacity-0', 'translate-y-4'); }, 10); }, 300); } } // --- Password Visibility Toggle --- function togglePassword(inputId, btn) { const input = document.getElementById(inputId); const isPassword = input.type === 'password'; input.type = isPassword ? 'text' : 'password'; if(isPassword) { btn.classList.add('text-white'); btn.classList.remove('text-[#f06827]'); } else { btn.classList.add('text-[#f06827]'); btn.classList.remove('text-white'); } } // --- Toast Notification --- function showToast(message, type = 'error') { const toast = document.getElementById("toast"); toast.textContent = message; toast.className = "show " + type; setTimeout(function(){ toast.className = toast.className.replace("show", ""); }, 3000); } // --- Login Handler (Client-side Validation) --- function validateLogin(e) { const phone = e.target.phone.value; const pass = e.target.password.value; if(!phone || !pass) { e.preventDefault(); // Stop submission showToast("Please fill in all fields"); return false; } // If valid, allow form to submit to PHP return true; } // --- Register Handler (Client-side Validation) --- function validateRegister(e) { const pass = e.target.password.value; const confirm = e.target.confirm_password.value; if (pass !== confirm) { e.preventDefault(); // Stop submission showToast("Passwords do not match"); return false; } if (pass.length < 4) { e.preventDefault(); showToast("Password too short"); return false; } // If valid, allow form to submit to PHP return true; } // --- Show PHP Errors if any --- <?php if ($error_message): ?> showToast("<?php echo $error_message; ?>", 'error'); <?php endif; ?> // --- Auto-switch to Register if referral code is present in URL --- <?php if ($url_referral): ?> window.onload = function() { switchTab('register'); }; <?php endif; ?> </script> </body> </html>
💾 Save