Error: PHP PDO SQLite extension is not enabled on your hosting.
Please enable it from 'Select PHP Version' or 'PHP Selector' in cPanel. "); } // Connect to SQLite Database try { $db = new PDO("sqlite:" . $db_file); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Create Tables if not exist $db->exec("CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password TEXT )"); $db->exec("CREATE TABLE IF NOT EXISTS messages ( id INTEGER PRIMARY KEY AUTOINCREMENT, sender_id INTEGER, username TEXT, message TEXT, file_path TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP )"); } catch (PDOException $e) { die("
Database Error: " . $e->getMessage() . "
Hint: Ensure folder permission is set to 777.
"); } // Handle Requests if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; // REGISTER if ($action === 'register') { $user = trim($_POST['username']); $pass = password_hash($_POST['password'], PASSWORD_DEFAULT); try { $stmt = $db->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $stmt->execute([$user, $pass]); $_SESSION['msg'] = "Registration successful! Please login."; } catch (Exception $e) { $_SESSION['error'] = "Username already exists!"; } header("Location: index.php"); exit; } // LOGIN if ($action === 'login') { $user = trim($_POST['username']); $pass = $_POST['password']; $stmt = $db->prepare("SELECT * FROM users WHERE username = ?"); $stmt->execute([$user]); $userData = $stmt->fetch(PDO::FETCH_ASSOC); if ($userData && password_verify($pass, $userData['password'])) { $_SESSION['user_id'] = $userData['id']; $_SESSION['username'] = $userData['username']; } else { $_SESSION['error'] = "Invalid credentials!"; } header("Location: index.php"); exit; } // SEND MESSAGE / UPLOAD if ($action === 'send' && isset($_SESSION['user_id'])) { $msg = trim($_POST['message'] ?? ''); $filePath = null; // Handle File Upload if (!empty($_FILES['file']['name'])) { if (!is_dir($upload_dir)) { mkdir($upload_dir, 0777, true); } $fileName = time() . '_' . preg_replace("/[^a-zA-Z0-9.]/", "_", basename($_FILES['file']['name'])); $targetPath = $upload_dir . $fileName; $webPath = 'uploads/' . $fileName; $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'txt', 'zip'])) { if (move_uploaded_file($_FILES['file']['tmp_name'], $targetPath)) { $filePath = $webPath; } } } if (!empty($msg) || $filePath) { $stmt = $db->prepare("INSERT INTO messages (sender_id, username, message, file_path) VALUES (?, ?, ?, ?)"); $stmt->execute([$_SESSION['user_id'], $_SESSION['username'], $msg, $filePath]); } header('Content-Type: application/json'); echo json_encode(['status' => 'success']); exit; } } // API: Fetch Messages if (isset($_GET['fetch_messages'])) { if (!isset($_SESSION['user_id'])) exit; $lastId = isset($_GET['last_id']) ? (int)$_GET['last_id'] : 0; $stmt = $db->prepare("SELECT * FROM messages WHERE id > ? ORDER BY id ASC"); $stmt->execute([$lastId]); $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); header('Content-Type: application/json'); echo json_encode($messages); exit; } // LOGOUT if (isset($_GET['logout'])) { session_destroy(); header("Location: index.php"); exit; } ?> Secure Private Chat

Secure Chat Login

Chat Room ()

Logout
Connecting to server...