php-chatbox
--chatbox.html-----------------------------------------------------------------------------
Code (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
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
<script>
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
var timer = 6000; //iedere 5 seconden refreshen
function say(value)
{
name = document.getElementById('name').value;
xmlhttp.open("GET", "chat.php?text="+value+"&name="+name+"&random="+Math.round(9000*Math.random()),true);//method, target, async (set always true!)
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById('chatwindow').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
timer = 6000;
counter();
}
function update()
{
xmlhttp.open("GET", "chat.php?random="+Math.round(9000*Math.random()),true);//method, target, async (set always true!)
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById('chatwindow').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
timer = 6000;
counter();
}
function counter()
{
if(timer == 0)
{
update();
}
else
{
timer = timer - 1000;
setTimeout("counter()", 1000);
}
}
function check_enter(eventobjekt)
{
//Bedankt Cees!
var var_key;
if (navigator.appName == "Microsoft Internet Explorer") {
var_key = window.event.keyCode;
} else {
var_key = eventobjekt.which;
}
if (var_key==13) {//{alert(var_key);
say(value);
document.getElementById('chattext').value = "";
}
}
function hasvalue(value)
{
if(value != "")
{
document.getElementById('submitbutton').disabled = false;
}
else
{
document.getElementById('submitbutton').disabled = true;
}
}
</script>
<input type="text" id="name" name="name" style="width: 600px;" maxlength="256" value="Annoniem"/>
<div id="chatwindow" style="display:block; width: 600px; height: 400px; overflow: scroll;">
Welcome to the chat. Please type a message.
</div><br/>
<input type="text" id="chattext" name="chattext" onKeyPress="check_enter(event);hasvalue(this.value);" onChange="hasvalue(this.value);" maxlength="256" style="width: 450px; "/>
<input type="submit" id="submitbutton" onMouseDown="javascript: say(document.getElementById('chattext').value);document.getElementById('chattext').value = '';" value="Say!" style="width:150px; " disabled />
<script>
update()
</script>
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
var timer = 6000; //iedere 5 seconden refreshen
function say(value)
{
name = document.getElementById('name').value;
xmlhttp.open("GET", "chat.php?text="+value+"&name="+name+"&random="+Math.round(9000*Math.random()),true);//method, target, async (set always true!)
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById('chatwindow').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
timer = 6000;
counter();
}
function update()
{
xmlhttp.open("GET", "chat.php?random="+Math.round(9000*Math.random()),true);//method, target, async (set always true!)
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
document.getElementById('chatwindow').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
timer = 6000;
counter();
}
function counter()
{
if(timer == 0)
{
update();
}
else
{
timer = timer - 1000;
setTimeout("counter()", 1000);
}
}
function check_enter(eventobjekt)
{
//Bedankt Cees!
var var_key;
if (navigator.appName == "Microsoft Internet Explorer") {
var_key = window.event.keyCode;
} else {
var_key = eventobjekt.which;
}
if (var_key==13) {//{alert(var_key);
say(value);
document.getElementById('chattext').value = "";
}
}
function hasvalue(value)
{
if(value != "")
{
document.getElementById('submitbutton').disabled = false;
}
else
{
document.getElementById('submitbutton').disabled = true;
}
}
</script>
<input type="text" id="name" name="name" style="width: 600px;" maxlength="256" value="Annoniem"/>
<div id="chatwindow" style="display:block; width: 600px; height: 400px; overflow: scroll;">
Welcome to the chat. Please type a message.
</div><br/>
<input type="text" id="chattext" name="chattext" onKeyPress="check_enter(event);hasvalue(this.value);" onChange="hasvalue(this.value);" maxlength="256" style="width: 450px; "/>
<input type="submit" id="submitbutton" onMouseDown="javascript: say(document.getElementById('chattext').value);document.getElementById('chattext').value = '';" value="Say!" style="width:150px; " disabled />
<script>
update()
</script>
--chat.php----------------------------------------------------------------------------------
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<pre>
<?php
if(isset($_GET['text']))
{
$already = file("text.txt");
$already[] = htmlentities($_GET['name'])." says:\t".htmlentities($_GET['text'])."\n";
$handle = fopen("text.txt",'w');
$iwant = array_chunk(array_reverse($already), 20);
if(!fwrite($handle, implode("", array_reverse($iwant[0]))))
{
die("grote error!");
}
fclose($handle);
echo implode("", array_reverse($iwant[0]));
}
else
{
echo implode("", file("text.txt"));
}
?>
</pre>
<?php
if(isset($_GET['text']))
{
$already = file("text.txt");
$already[] = htmlentities($_GET['name'])." says:\t".htmlentities($_GET['text'])."\n";
$handle = fopen("text.txt",'w');
$iwant = array_chunk(array_reverse($already), 20);
if(!fwrite($handle, implode("", array_reverse($iwant[0]))))
{
die("grote error!");
}
fclose($handle);
echo implode("", array_reverse($iwant[0]));
}
else
{
echo implode("", file("text.txt"));
}
?>
</pre>
--text.txt------------------------------------------------------------------------------------