Php+jquery+ajax Live Chat feature instance, jqueryajax_php tutorial

Source: Internet
Author: User
Tags live chat

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.