I used tricks (pushing) technology to program chat rooms.

Source: Internet
Author: User

I use the tricks (pushing) technology to compile the chat room program. The interface does not need to be refreshed, nor does it need to ask the server side if it is like ajax.

Set_time_limit (9990 );
Header ('cache-control: private ');
Header ('content-Type: text/html; charset = UTF-8 ');
$ File = "chat.txt ";

$ Action = trim ($ _ GET ['action']);
Switch ($ action ){
Case "say ":
_ Say ();
Break;
Case "chat ":
_ Chat ();
Break;
Default:
_ Main ();
Break;
}
//------------------------------------------------*
Function _ main (){
Echo '<iframe src = "? Action = chat "height =" 300 "width =" 100% "> </iframe> ';

Echo '<form method = "post" id = "f1" target = "say"
Action = "? Action = say "> <input type =" text "name =" wd "> <input
Type = "submit" value = "say"> </form> ';
Echo '<iframe name = "say" style = "display: none"> </iframe> ';
}
//------------------------------------------------*
Function _ say (){
Global $ file;
$ Wd = trim ($ _ POST ['wd ']);
IF (File_Exists ($ file )){
$ Htm = File_Get_Contents ($ file). $ wd. Chr (10 );
} Else {
$ Htm = $ wd. Chr (10 );
}

File_Put_Contents ($ file, $ htm, LOCK_EX );
Clearstatcache ();
}
//------------------------------------------------*
Function _ chat (){
Global $ file;
IF (! File_Exists ($ file )){
File_Put_Contents ($ file, '', LOCK_EX );
}

// This line is for IE bug
Echo str_repeat (', 256 );
Ob_flush ();
Flush ();

$ Total = Count (File ($ file ));
$ I = $ total-1;

// If you want to publish this article, please indicate the exit, no outgoing copyright notice, I do not like to see this kind of website. My work website does not indicate the person who is serving Seven {See7di # Gmail.com}
While (true ){
$ N = Count (File ($ file ));
IF ($ N! = $ Total ){
$ Total = $ N;
$ HTM = File ($ file );
Echo '<div>', Htmlspecialchars (End ($ HTM), '</div> ';
Ob_flush ();
Flush ();
} Else {
Usleep (1500 );
}
}
}

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.