-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.php
164 lines (146 loc) · 6.39 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
<?php
session_start();
//
//function loginForm(){
// echo'
// <div id="loginform">
// <form action="index.php" method="post">
// <p>Please enter your name to continue:</p>
// <label for="name">Name:</label>
// <input type="text" name="name" id="name" />
// <input type="submit" name="enter" id="enter" value="Enter" />
// </form>
// </div>
// ';
//}
//
//if(isset($_POST['enter'])){
// if($_POST['name'] != ""){
// $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
//
// //Clear file
// $fp = fopen("log.html", 'w');
// fclose($fp);
//
// $fp = fopen("log.html", 'a');
// fwrite($fp, "<div class='msgln' id=" . time() ."><i>User ". $_SESSION['name'] ." has entered the chat session.</i><br></div>");
// fclose($fp);
// }
// else{
// echo '<span class="error">Please type in a name</span>';
// }
//}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat - Customer Module</title>
<link type="text/css" rel="stylesheet" href="style.css" />
<link href="phpProject9.css" rel="stylesheet" type="text/css"/>
<link href="bootstrap-3.2.0-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script src="Javascript-project.js" type="text/javascript"></script>
</head>
<body>
<?php include 'header.inc.php' ?>
<?php include 'sidenav.inc.php' ?>
<?php
$_SESSION['name'] = $_SESSION['USERNAME'];
if (!isset($_SESSION['name'])) {
//loginForm();
} else {
if (isset($_GET['logout'])) {
//Clear file
$fp = fopen("log.html", 'w');
fclose($fp);
//Simple exit message
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'><i>User " . $_SESSION['name'] . " has left the chat session.</i><br></div>");
fclose($fp);
session_destroy();
echo '<script>location.reload(true);</script>'; //Show login form again
}
?>
<div class="container">
<div class="col-md-9">
<div>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
<!-- <p class="logout"><a id="exit" href="#">Exit Chat</a></p>-->
<div style="clear:both"></div>
</div>
<div id="chatbox"><?php
if (file_exists("log.html") && filesize("log.html") > 0) {
$handle = fopen("log.html", "r");
$contents = fread($handle, filesize("log.html"));
fclose($handle);
echo $contents;
}
?></div>
<form name="message" method="post">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
</div>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function () {
//If user submits the form (Enter key or submit)
$("#usermsg").keydown(function (e) {
//Optional "user is typing" message (for small group chats)
//$.post("post.php", {text: "user is typing"});
if (e.keyCode == 13) {
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").val("");
return false;
}
});
$("#submitmsg").click(function () {
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").val("");
return false;
});
$("#usermsg").focus();
//Load the file containing the chat log
function loadLog() {
var oldscrollHeight = $("#chatbox").prop("scrollHeight") - 20;
$.ajax({
url: "log.html",
cache: false,
success: function (html) {
var scrolltop = $("#chatbox").scrollTop();//Get current vertical position of scroll bar
//Id's used to load only new messages (timewise) into the #chatbox
var prevmsgid = $("#chatbox div").last().attr('id');
var newmsgid = $(html).attr('id');
if (prevmsgid != newmsgid) {
$("#chatbox").append(html);
}
//Autoscroll to new bottom of #chatbox if previously at bottom of #chatbox
var newscrollHeight = $("#chatbox").prop("scrollHeight") - 20;
if (scrolltop == (oldscrollHeight - 270) && newscrollHeight > oldscrollHeight) {
$("#chatbox").animate({scrollTop: newscrollHeight}, 'normal');
}
},
});
}
setInterval(loadLog, 70); //Reload file every 70ms
//If user wants to end session
$("#exit").click(function () {
var exit = confirm("Are you sure you want to end the session?");
if (exit == true) {
window.location = 'index.php?logout=true';
}
});
});
</script>
<?php
}
?>
</body>
</html>