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 );
}
}
}