-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.php
93 lines (91 loc) · 2.72 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
<style type="text/css">
#sent_msg{
text-align: right;
font-size: 18px;
margin-right: 30px;
margin-left: 60%;
font-style: initial;
font-family: sans-serif;
color:black;
margin-top:20px;
padding-left: 0px;
background-color: #e6ffb4;
border-radius: 50px;
border-bottom-right-radius: 0px;
}
#received_msg{
margin-right: 65%;
font-size: 18px;
font-style: initial;
font-family: sans-serif;
color: black;
background-color: #eee;
margin-top:20px;
border-radius: 50px;
border-top-left-radius: 0px;
}
.jumbotron{
padding: 10px;
}
#time{
font-size: 12px;
}
</style>
<?php
include 'pdo.php';
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(empty($_SESSION['user'])){
echo 'error occured';
}
else{
$from=$_SESSION['user'];
$to=$_SESSION['to'];
if(isset($_POST['msg'])&&!empty($_POST['msg'])){
$stmt=$db->prepare("INSERT INTO chats(sender,receiver,message,time1,date1)
VALUES (:sender,:receiver,:message,:time1,:date1)");
$stmt->bindParam(':sender',$from);
$stmt->bindParam(':receiver',$to);
$stmt->bindParam(':message',$_POST['msg']);
date_default_timezone_set('Asia/Kolkata');
$stmt->bindParam(':time1',date('h:i a'));
$stmt->bindParam(':date1',date("d:M"));
$stmt->execute();
$stmt2=$db->prepare("UPDATE followings set talking=:talking WHERE username=:username AND following=:following");
$stmt2->bindValue(':talking','1');
$stmt2->bindParam(':username',$from);
$stmt2->bindParam(':following',$to);
$stmt2->execute();
$stmt3=$db->prepare("UPDATE followers set talking=:talking WHERE username=:username AND follower=:follower");
$stmt3->bindValue(':talking','1');
$stmt3->bindParam(':username',$to);
$stmt3->bindParam(':follower',$from);
$stmt3->execute();
}
}
$to=$_SESSION['to'];
$text="SELECT * FROM chats WHERE (sender='$from' AND receiver='$to')
OR (receiver='$from' AND sender='$to')";
//echo "<embed loop='false' src='tasty.mp3' hidden='true' autoplay='true'/>";
foreach($db->query($text) as $row){
if($row['sender']==$_SESSION['user']){
echo '<div class="jumbotron" id="sent_msg">'.$row['message'].' <sub id="time">';
if(date("d:M")==$row['date1']){
echo $row['time1'].'</sub></div>';
}
if(date("d:M")!=$row['date1']){
echo $row['date1'].'</sub></div>';
}
}
else{
echo '<div class="jumbotron" id="received_msg">'.$row['message'].' <sub id="time">';
if(date("d:M")==$row['date1']){
echo $row['time1'].'</sub></div>';
}
if(date("d:M")!=$row['date1']){
echo $row['date1'].'</sub></div>';
}
}
}
?>