Sfoglia il codice sorgente

Upload files to ''

Parv Ashwani 10 mesi fa
parent
commit
8c23d70d13
5 ha cambiato i file con 245 aggiunte e 0 eliminazioni
  1. 16 0
      db.sql
  2. 36 0
      index.php
  3. 61 0
      register.php
  4. 54 0
      script.js
  5. 78 0
      styles.css

+ 16 - 0
db.sql

@@ -0,0 +1,16 @@
+CREATE TABLE users (
+    id INT AUTO_INCREMENT PRIMARY KEY,
+    username VARCHAR(50) UNIQUE NOT NULL,
+    password VARCHAR(255) NOT NULL,  -- Hashed
+    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE TABLE messages (
+    id INT AUTO_INCREMENT PRIMARY KEY,
+    user_id INT NOT NULL,
+    message_text TEXT,
+    attachment_path VARCHAR(255),  -- Path to uploaded file/image
+    is_image TINYINT(1) DEFAULT 0,  -- 1 if image, for display
+    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+    FOREIGN KEY (user_id) REFERENCES users(id)
+);

+ 36 - 0
index.php

@@ -0,0 +1,36 @@
+<?php
+session_start();
+if (!isset($_SESSION['user_id'])) {
+    // Redirect to login/register if not logged in
+    header('Location: register.php');
+    exit;
+}
+
+// Fetch username for display
+$pdo = new PDO('mysql:host=sql101.infinityfree.com;dbname=if0_39567875_nex', 'if0_39567875', 'PIvOR9WViHm');  // Update credentials
+$stmt = $pdo->prepare("SELECT username FROM users WHERE id = ?");
+$stmt->execute([$_SESSION['user_id']]);
+$user = $stmt->fetch();
+?>
+
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Nexus</title>
+    <link rel="stylesheet" href="styles.css">
+</head>
+<body>
+    <div class="container">
+        <h2>Welcome, <?php echo htmlspecialchars($user['username']); ?>! <a href="api/logout.php">Logout</a></h2>
+        <div id="chat-window"></div>
+        <form id="chat-form">
+            <input type="text" name="message" placeholder="Type your message..." required>
+            <input type="file" name="attachment">
+            <button type="submit">Send</button>
+        </form>
+        <button id="refresh-btn">Refresh Chat</button>
+    </div>
+    <script src="script.js"></script>
+</body>
+</html>

+ 61 - 0
register.php

@@ -0,0 +1,61 @@
+<?php
+session_start();
+if (isset($_SESSION['user_id'])) {
+    header('Location: index.php');
+    exit;
+}
+
+$pdo = new PDO('mysql:host=sql101.infinityfree.com;dbname=if0_39567875_nex', 'if0_39567875', 'PIvOR9WViHm');  // Update credentials
+
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+    $username = $_POST['username'];
+    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
+
+    if (isset($_POST['register'])) {
+        // Register
+        $stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
+        if ($stmt->execute([$username, $password])) {
+            $_SESSION['user_id'] = $pdo->lastInsertId();
+            header('Location: index.php');
+        } else {
+            echo "Username taken.";
+        }
+    } elseif (isset($_POST['login'])) {
+        // Login
+        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
+        $stmt->execute([$username]);
+        $user = $stmt->fetch();
+        if ($user && password_verify($_POST['password'], $user['password'])) {
+            $_SESSION['user_id'] = $user['id'];
+            header('Location: index.php');
+        } else {
+            echo "Invalid credentials.";
+        }
+    }
+}
+?>
+
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Register/Login</title>
+    <link rel="stylesheet" href="styles.css">
+</head>
+<body>
+    <div class="container">
+        <h2>Register</h2>
+        <form method="POST">
+            <input type="text" name="username" placeholder="Username" required>
+            <input type="password" name="password" placeholder="Password" required>
+            <button type="submit" name="register">Register</button>
+        </form>
+        <h2>Login</h2>
+        <form method="POST">
+            <input type="text" name="username" placeholder="Username" required>
+            <input type="password" name="password" placeholder="Password" required>
+            <button type="submit" name="login">Login</button>
+        </form>
+    </div>
+</body>
+</html>

+ 54 - 0
script.js

@@ -0,0 +1,54 @@
+document.addEventListener('DOMContentLoaded', () => {
+    const form = document.getElementById('chat-form');
+    const refreshBtn = document.getElementById('refresh-btn');
+    const chatWindow = document.getElementById('chat-window');
+
+    // Function to fetch and display messages
+    function fetchMessages() {
+        fetch('api/fetch_messages.php')
+            .then(response => response.json())
+            .then(messages => {
+                chatWindow.innerHTML = '';
+                messages.forEach(msg => {
+                    const div = document.createElement('div');
+                    div.classList.add('message');
+                    div.innerHTML = `
+                        <span class="user">${msg.username}:</span> ${msg.message_text || ''}
+                        ${msg.is_image ? `<img src="${msg.attachment_path}" alt="Image">` : ''}
+                        ${!msg.is_image && msg.attachment_path ? `<a href="${msg.attachment_path}" download>Download Attachment</a>` : ''}
+                        <small>${msg.created_at}</small>
+                    `;
+                    chatWindow.appendChild(div);
+                });
+                chatWindow.scrollTop = chatWindow.scrollHeight;  // Auto-scroll to bottom
+            });
+    }
+
+    // Initial load
+    fetchMessages();
+
+    // Auto-poll every 5 seconds for real-time updates
+    setInterval(fetchMessages, 5000);
+
+    // Manual refresh button (still available)
+    refreshBtn.addEventListener('click', fetchMessages);
+
+    // Send message
+    form.addEventListener('submit', (e) => {
+        e.preventDefault();
+        const formData = new FormData(form);
+        fetch('api/send_message.php', {
+            method: 'POST',
+            body: formData
+        })
+            .then(response => response.json())
+            .then(result => {
+                if (result.success) {
+                    form.reset();
+                    fetchMessages();  // Auto-refresh after send
+                } else {
+                    alert(result.error);
+                }
+            });
+    });
+});

+ 78 - 0
styles.css

@@ -0,0 +1,78 @@
+body {
+    font-family: 'Arial', sans-serif;
+    background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
+    color: #fff;
+    margin: 0;
+    padding: 0;
+    display: flex;
+    justify-content: center;
+    align-items: center;
+    height: 100vh;
+}
+
+.container {
+    background: rgba(255, 255, 255, 0.1);
+    border-radius: 15px;
+    padding: 20px;
+    width: 80%;
+    max-width: 800px;
+    box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
+    backdrop-filter: blur(10px);
+}
+
+#chat-window {
+    height: 400px;
+    overflow-y: auto;
+    margin-bottom: 20px;
+    padding: 10px;
+    border: 1px solid rgba(0, 255, 255, 0.3);
+    border-radius: 10px;
+    background: rgba(0, 0, 0, 0.2);
+}
+
+.message {
+    margin: 10px 0;
+    padding: 10px;
+    border-radius: 8px;
+    background: rgba(255, 255, 255, 0.05);
+    transition: transform 0.2s;
+}
+
+.message:hover {
+    transform: scale(1.02);
+    box-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
+}
+
+.message .user {
+    font-weight: bold;
+    color: #00ffff;  /* Neon cyan */
+}
+
+.message img {
+    max-width: 200px;
+    border-radius: 5px;
+    margin-top: 5px;
+}
+
+input, button {
+    padding: 10px;
+    margin: 5px;
+    border: none;
+    border-radius: 5px;
+    background: rgba(255, 255, 255, 0.1);
+    color: #fff;
+}
+
+button {
+    background: linear-gradient(45deg, #00ffff, #00ff00);
+    cursor: pointer;
+    transition: background 0.3s;
+}
+
+button:hover {
+    background: linear-gradient(45deg, #00ff00, #00ffff);
+}
+
+#refresh-btn {
+    background: linear-gradient(45deg, #ff00ff, #ff9900);
+}