Php+jquery+ajax implements live Chat feature instances, Jqueryajax
In this paper, we describe the method of Php+jquery+ajax to realize instant chatting. Share to everyone for your reference. Specific as follows:
This is a simple use of jquery and PHP to do a chat room source code, we regularly use AJAX to read the database and refresh, the following direct reference to the source code, the example is as follows:
The Index.html page is as follows:
Copy the Code code as follows:
Untitled Document
Speeches
The data.php page is as follows:
Copy CodeThe code is as follows: <?php
Header ("Expires:mon, Jul 1997 05:00:00 GMT");
Header ("last-modified:". Gmdate ("D, D M Y h:i:s"). " GMT ");
Header ("Cache-control:no-cache, must-revalidate");
Header ("Pramga:no-cache");
Set_time_limit (0);
$get = $_get[' action '];
$type = $_get[' type '];
$file = $type. '. TXT ';
if (Isset ($get) && isset ($type) && file_exists ($file)) {
Switch ($get) {
Case ' first ':
$chat = file_get_contents ($file);
$json =array (
' Status ' = 200,
' Time ' = Gmdate ("s"),
' Chat ' = $chat,
);
echo Json_encode ($json);
Break
Case ' while ':
$oldsize = FileSize ($file);
$newsize = FileSize ($file);
while (true) {
if ($oldsize! = $newsize) {
$chat = file_get_contents ($file);
$json =array (
' Status ' = 200,
' Time ' = Gmdate ("s"),
' Chat ' = $chat,
);
echo Json_encode ($json);
Exit
}
Clearstatcache ();
$newsize = FileSize ($file);
Usleep (10000);
}
Break
}
}
?>
The send.php page is as follows:
Copy the Code code as follows: <?php
$json = Array ();
$txt = isset ($_get[' txt '])? $_get[' txt ']: ';
$type = isset ($_get[' type ')? $_get[' type ']: ';
if ($txt! = ") {
$file = $type. ". TXT ";
if (file_exists ($file)) {
$fp = fopen ($file, "a");
$STR = "rn". ' Admin: '. $txt;
$str = $txt. " n "//linux;
Fwrite ($fp, $STR);
Fclose ($FP);
$json [' Status ']=200;
echo Json_encode ($json);
Exit
}
}
?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/932074.html www.bkjia.com true http://www.bkjia.com/PHPjc/932074.html techarticle Php+jquery+ajax realizes the real-time chat function instance, Jqueryajax This article describes Php+jquery+ajax realizes the instant chatting function the method. Share to everyone for your reference. As follows: this ...