forked from codingWithElias/php-chat-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.php
194 lines (169 loc) · 5.29 KB
/
chat.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
session_start();
if (isset($_SESSION['username'])) {
# database connection file
include 'app/db.conn.php';
include 'app/helpers/user.php';
include 'app/helpers/chat.php';
include 'app/helpers/opened.php';
include 'app/helpers/timeAgo.php';
if (!isset($_GET['user'])) {
header("Location: home.php");
exit;
}
# Getting User data data
$chatWith = getUser($_GET['user'], $conn);
if (empty($chatWith)) {
header("Location: home.php");
exit;
}
$chats = getChats($_SESSION['user_id'], $chatWith['user_id'], $conn);
opened($chatWith['user_id'], $conn, $chats);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat App</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link rel="stylesheet"
href="css/style.css">
<link rel="icon" href="img/logo.png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body class="d-flex
justify-content-center
align-items-center
vh-100">
<div class="w-400 shadow p-4 rounded">
<a href="home.php"
class="fs-4 link-dark">←</a>
<div class="d-flex align-items-center">
<img src="uploads/<?=$chatWith['p_p']?>"
class="w-15 rounded-circle">
<h3 class="display-4 fs-sm m-2">
<?=$chatWith['name']?> <br>
<div class="d-flex
align-items-center"
title="online">
<?php
if (last_seen($chatWith['last_seen']) == "Active") {
?>
<div class="online"></div>
<small class="d-block p-1">Online</small>
<?php }else{ ?>
<small class="d-block p-1">
Last seen:
<?=last_seen($chatWith['last_seen'])?>
</small>
<?php } ?>
</div>
</h3>
</div>
<div class="shadow p-4 rounded
d-flex flex-column
mt-2 chat-box"
id="chatBox">
<?php
if (!empty($chats)) {
foreach($chats as $chat){
if($chat['from_id'] == $_SESSION['user_id'])
{ ?>
<p class="rtext align-self-end
border rounded p-2 mb-1">
<?=$chat['message']?>
<small class="d-block">
<?=$chat['created_at']?>
</small>
</p>
<?php }else{ ?>
<p class="ltext border
rounded p-2 mb-1">
<?=$chat['message']?>
<small class="d-block">
<?=$chat['created_at']?>
</small>
</p>
<?php }
}
}else{ ?>
<div class="alert alert-info
text-center">
<i class="fa fa-comments d-block fs-big"></i>
No messages yet, Start the conversation
</div>
<?php } ?>
</div>
<div class="input-group mb-3">
<textarea cols="3"
id="message"
class="form-control"></textarea>
<button class="btn btn-primary"
id="sendBtn">
<i class="fa fa-paper-plane"></i>
</button>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var scrollDown = function(){
let chatBox = document.getElementById('chatBox');
chatBox.scrollTop = chatBox.scrollHeight;
}
scrollDown();
$(document).ready(function(){
$("#sendBtn").on('click', function(){
message = $("#message").val();
if (message == "") return;
$.post("app/ajax/insert.php",
{
message: message,
to_id: <?=$chatWith['user_id']?>
},
function(data, status){
$("#message").val("");
$("#chatBox").append(data);
scrollDown();
});
});
/**
auto update last seen
for logged in user
**/
let lastSeenUpdate = function(){
$.get("app/ajax/update_last_seen.php");
}
lastSeenUpdate();
/**
auto update last seen
every 10 sec
**/
setInterval(lastSeenUpdate, 10000);
// auto refresh / reload
let fechData = function(){
$.post("app/ajax/getMessage.php",
{
id_2: <?=$chatWith['user_id']?>
},
function(data, status){
$("#chatBox").append(data);
if (data != "") scrollDown();
});
}
fechData();
/**
auto update last seen
every 0.5 sec
**/
setInterval(fechData, 500);
});
</script>
</body>
</html>
<?php
}else{
header("Location: index.php");
exit;
}
?>