-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
103 lines (88 loc) · 2.75 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
session_start();
// Define the chat file
$chatFile = 'chat.txt';
$setupFile = 'setup.php';
// Run setup if necessary
if (!file_exists($setupFile)) {
header('Location: setup.php');
exit();
}
// Check if the user has submitted their name
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name'])) {
$_SESSION['name'] = htmlspecialchars($_POST['name']);
header('Location: index.php');
exit();
}
// Check if the user is already logged in
if (!isset($_SESSION['name'])) {
echo '<form method="POST">
<label>Enter your name:</label><br>
<input type="text" name="name" required><br>
<button type="submit">Join Chat</button>
</form>';
exit();
}
// Load the chat messages
function loadChat() {
global $chatFile;
if (file_exists($chatFile)) {
$lines = file($chatFile, FILE_IGNORE_NEW_LINES);
$messages = array_slice($lines, -10); // Show last 10 messages only
return json_encode($messages);
} else {
return json_encode([]);
}
}
// Handle chat submission
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['message'])) {
$message = htmlspecialchars($_SESSION['name'] . ': ' . $_POST['message']);
file_put_contents($chatFile, $message . PHP_EOL, FILE_APPEND);
// Keep only the last 10 messages
$lines = file($chatFile, FILE_IGNORE_NEW_LINES);
if (count($lines) > 10) {
file_put_contents($chatFile, implode(PHP_EOL, array_slice($lines, -10)) . PHP_EOL);
}
header('Location: index.php');
exit();
}
// Handle AJAX loader
if (isset($_GET['loader'])) {
header('Content-Type: application/json');
echo loadChat();
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Chat</title>
<script>
function loadChat() {
fetch('index.php?loader=1')
.then(response => response.json())
.then(data => {
const chatBox = document.getElementById('chatBox');
chatBox.innerHTML = '';
data.forEach(line => {
const p = document.createElement('p');
p.textContent = line;
chatBox.appendChild(p);
});
});
}
setInterval(loadChat, 300000); // Reload chat every 5 minutes
window.onload = loadChat;
</script>
</head>
<body>
<h1>Welcome to the Chat</h1>
<div id="chatBox" style="height: 300px; overflow-y: scroll; border: 1px solid #000;"></div>
<form method="POST">
<label>Your message:</label><br>
<input type="text" name="message" required><br>
<button type="submit">Send</button>
</form>
</body>
</html>