-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.php
198 lines (158 loc) · 5.94 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
195
196
197
198
<?php
require_once('common.php');
checkUser();
function createForm(){
?>
<form action="<?php echo $_SESSION['userName']; ?><" method="post">
<table align="center">
<tr><td colspan="2">Please eneter a nickname to login!</td></tr>
<tr><td>Your name: </td>
<td><input class="text" type="text" name="name" /></td></tr>
<tr><td colspan="2" align="center">
<input class="text" type="submit" name="submitBtn" value="Login" />
</td></tr>
</table>
</form>
<?php
}
if (isset($_GET['u'])){
unset($_SESSION['userName']);
}
// Process login info
if (isset($_POST['submitBtn'])){
$name = isset($_POST['name']) ? $_POST['name'] : $_SESSION['userName'];;
$_SESSION['userName'] = $name;
}
$nickname = isset($_SESSION['userName']) ? $_SESSION['userName'] : "Hidden";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Micro Chat</title>
<script language="javascript" type="text/javascript">
<!--
var httpObject = null;
var link = "";
var timerID = 0;
var nickName = "<img src='img/<?php echo $_SESSION['userName']; ?>.jpg' class='w3-circle' style='height:30px;width:30px' alt='Avatar'><?php echo $_SESSION['userName']; ?>";
// Get the HTTP Object
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}
// Change the value of the outputText field
function setOutput(){
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("result");
objDiv.innerHTML += response;
objDiv.scrollTop = objDiv.scrollHeight;
var inpObj = document.getElementById("msg");
inpObj.value = "";
inpObj.focus();
}
}
// Change the value of the outputText field
function setAll(){
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("result");
objDiv.innerHTML = response;
objDiv.scrollTop = objDiv.scrollHeight;
}
}
// Implement business logic
function doWork(){
httpObject = getHTTPObject();
if (httpObject != null) {
link = "message.php?nick="+nickName+"&msg="+document.getElementById('msg').value;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = setOutput;
httpObject.send(null);
}
}
// Implement business logic
function doReload(){
httpObject = getHTTPObject();
var randomnumber=Math.floor(Math.random()*10000);
if (httpObject != null) {
link = "message.php?all=1&rnd="+randomnumber;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = setAll;
httpObject.send(null);
}
}
function UpdateTimer() {
doReload();
timerID = setTimeout("UpdateTimer()", 5000);
}
function keypressed(e){
if(e.keyCode=='13'){
doWork();
}
}
//-->
</script>
<link rel="stylesheet" href="w3css/w3.css">
<link href="style/style1.css" rel="stylesheet" type="text/css" />
</head>
<body onload="UpdateTimer();">
<body class="w3-theme-l5">
<h2 class="w3-tag w3-xlarge w3-padding w3-red" style="transform:rotate(-5deg)">chat</h2>
<hr>
<div class="w3-dropdown-hover w3-hide-small">
<button class="w3-button w3-padding-large" title="Notifications"><i class="fa fa-bell"></i><span class="w3-badge w3-right w3-small w3-green">admin</span></button>
<div class="w3-dropdown-content w3-card-4 w3-bar-block" style="width:300px">
<a href="index.php" class="w3-bar-item w3-button"><i class="fa fa-cog fa-spin" style="font-size:24px"></i>sprinfielface</a>
<a href="chat.php" class="w3-bar-item w3-button">chat</a>
<a href="entradas.php" class="w3-bar-item w3-button">entradas</a>
<a href="lista.php" class="w3-bar-item w3-button">profile</a>
<a href="blog.php" class="w3-bar-item w3-button">blog</a>
<a href="logout.php" class="w3-bar-item w3-button">salir</a>
</div>
</div>
<br>
</div>
</div>
<hr>
<?php
if (!isset($_SESSION['userName']) ){
createForm();
} else {
$name = isset($_POST['name']) ? $_POST['name'] : $_SESSION['userName'];
$_SESSION['userName'] = $name;
?>
<div class="w3-row-padding">
<div class="w3-col m12">
<div class="w3-card-4 w3-round w3-white ">
<div class="w3-container w3-padding">
<div id="result" class="w3-container w3-padding">
<?php
$data = file("msg.html");
foreach ($data as $line) {
echo $line;
}
?>
</div></div></div>
<hr>
<div class="w3-row-padding">
<div class="w3-col m12">
<div class="w3-card-2 w3-round w3-white">
<div class="w3-container w3-padding">
<h6 class="w3-opacity">Social friend chat
<div id="sender" onkeyup="keypressed(event);">
Your message:</h6> <input type="text" name="msg" size="30" id="msg" class="w3-input w3-border w3-round" /><br>
<button onclick="doWork();"class="w3-button w3-green">Send</button>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</body>