-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforums.php
106 lines (99 loc) · 4.01 KB
/
forums.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
104
105
106
<!DOCTYPE html>
<html lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forums - Let's Talk finance</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<?php
if (!isset($_COOKIE["token"])) {
header('location: /Hackathon%202023/login.php');
}
$token = $_COOKIE["token"];
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli(
$servername,
$username,
$password
);
if ($conn->connect_error) {
die("Connection failed: "
. $conn->connect_error);
}
$sql = "use finance";
$result = ($conn->query($sql));
$log = mysqli_query($conn, "SELECT uid FROM session WHERE token=$token");
if (mysqli_num_rows($log) > 0) {
$data = mysqli_fetch_assoc($log);
$myuid = $data['uid'];
$log = mysqli_query($conn, "SELECT * FROM users WHERE id=$myuid");
if (mysqli_num_rows($log) > 0) {
$data = mysqli_fetch_assoc($log);
echo "<h1>Hello, " . $data['fname'] . " " . $data['lname'] . "</h1><br>";
} else {
header('location: /Hackathon%202023/login.php');
}
} else {
header('location: /Hackathon%202023/login.php');
}
?>
<div id="messages">
<div class="px-10 flex flex-col justify-between">
<div class="flex flex-col mt-5" style="overscroll-none">
<?php
$logforum = mysqli_query($conn, "select * from (SELECT * FROM forum order by time desc LIMIT 20) as r order by time asc");
while ($row = $logforum->fetch_assoc()) {
$id = $row['uid'];
if ($id == $myuid) {
echo '<div class="flex justify-end"><div class="mb-2 bg-red-100 text-xl rounded-md p-3 pl-5"><div class="text-blue-800">';
} else {
echo '<div class="flex justify-start"><div class="mb-2 bg-green-100 text-xl rounded-md p-3 pl-5"><div class="text-red-800">';
}
$userlog = mysqli_query($conn, "SELECT * FROM users WHERE id=$id");
if (mysqli_num_rows($userlog) > 0) {
$userdata = mysqli_fetch_assoc($userlog);
if ($userdata['fname'])
echo $userdata['fname'] . " " . $userdata['lname'] . "</div> " . $row["content"] . "</div></div>";
} else {
echo "<hr><b>Deleted User</b>: " . $row["content"];
}
}
?>
</div>
</div>
</div>
<script>
function ref() {
$('#messages').load(document.URL + ' #messages');
document.getElementById("message").focus();
}
$(window).load(function(){document.getElementById("message").focus();});
setInterval("ref()", 500);
</script>
<?php
if (isset($_POST['submit'])) {
$message = $_POST['message'];
if (strlen($message) != 0) {
$sql = "insert into forum (uid,content) values ('$myuid', '$message');";
if (!mysqli_query($conn, $sql)) {
echo '<script> alert("Message cannot be sent")';
}
}
echo "<script>ref();</script>";
}
?>
<form class="sticky w-full flex bottom-0 px-2" action="#error-check" id="error-check" method="post">
<input class="w-5/6 my-2 px-4 border-2 bg-transparent h-12" type="text" id="message" placeholder="Enter Message"
autofocus="autofocus" name="message" />
<button type="submit" name="submit" class="w-1/6 my-2 border-2">
Send
</button>
</form>
</body>
</html>