PHP enables active and passive updates in chat rooms

Source: Internet
Author: User
Tags chop home screen
How to display the chat content on the screen. one is to refresh the page at intervals, read all the chat content, and then display it. here we use the js document. write method to achieve non-refresh chat page! How to display the ghost chat content on the screen. one is to refresh the page and read all the chats at intervals.
Content, and then display, here is the js document. write method to achieve non-refresh chat page
!

1. Generate the home page, specify the CSS type, and display the welcome word
Function write2 (){
If(this.u.doc ument = null) return;
This.u.doc ument. writeln ("");
This.u.doc ument. writeln (" Ml; charset = gb2312> ");
This.u.doc ument. writeln (" ");

This.u.doc ument. writeln ("");
This.u.doc ument. writeln (" // .................. Insert the program section to generate the online person array here


This.u.doc ument. writeln ("script");
This.u.doc ument. writeln ("

");
This.u.doc ument. writeln ("

Welcome to the PlayBoy chat room.
Testing phase in progress. please contact us if you have any questions

");
}

2. initialize the information and enter the chat room for the first time.

If ($ action = "enter ")
{

//////////////// Call the js program that displays the home screen ////////////// //////
Print ("parent. write2 (); n ");

// The Speech Content. xx entered the chat room
$ Message = "$ name
Go to the chat room ". $ message." ". date (" m Month d h: I ")." script parent. add ('$ name ',
'$ Photo'); parent. write1 (); script
";
}
// Update the speech content
While (file_exists ($ lockfile) {$ pppp ++ ;}

// Lock the speech
Fclose (fopen ($ lockfile, "w "));

// Read the total number of speeches, that is, the total number of speeches made by everyone! We can save every speech,
Yes, this will occupy a lot of disk space. we adopt a modulo method to reduce the number of files in a loop.
File operations!
$ Talkmessage = file ($ filename );
$ Number = chop ($ talkmessage [0]);

// Add one to the number of speeches and save the statement.
$ Talkhandle = fopen ($ filename, "w ");
$ Number ++;
Fputs ($ talkhandle, $ number );
Fclose ($ talkhandle );

/Remove lock
Unlink ($ lockfile );

// Modulo 10 for the total number of speeches and save the speech content as the file name. that is to say, 11th sentences and 1st sentences use the same
A file name, because it is impossible to have 10 words not Updated at the same time, so this is not a lot of people
Very good! Of course, you can set it to 100 considering the large number of people.
$ Filehandle = fopen ("messageonline". ($ number % 10). ". php", "w ");
Fputs ($ filehandle, $ message );
Fclose ($ filehandle );

// Display the entry information
Print ("parent.u.doc ument. writeln (" $ message "); n ");

// Call to actively refresh the js program and pass the displayed number of speeches
Print ("parent. flushwin ($ number) n ");

// Save the Last displayed speech
$ Last = $ number;
}


3. process the request for sending a form

// Do not handle blank and more than a certain number of speeches
If ($ message! = "") & (Strlen ($ message) <150 ))
{

// Check whether the speaker is online to prevent exceptions
$ Onlineperson = file ("useronline. dbf ");
$ Personsign = 0;
For ($ I = 0; $ I {
$ Person = split ($ split, $ onlineperson [$ I], 99 );
If ($ person [0] = $ name)
{
$ Personsign = 1;
$ Person [3] = date ("U ");
Break;
}
}

// Online processing program
If ($ personsign = 1)
{

// Add the part of the speech time
$ Message = $ message. "". date ("m Month D day H: I ")."
";


// Lock the total number of speeches file
While (file_exists ($ lockfile) {$ pppp ++ ;}
Fclose (fopen ($ lockfile, "w "));

// Total number of speeches read
$ Talkmessage = file ($ filename );
$ Number = chop ($ talkmessage [0]);

// Add 1 to the total number and save it.
$ Talkhandle = fopen ($ filename, "w ");
$ Number ++;
Fputs ($ talkhandle, $ number );
Fclose ($ talkhandle );
Unlink ($ lockfile );

// Save the speech content as a file after modulo 10 of the total number
$ Filehandle = fopen ("messageonline". ($ number % 10). ". php", "w ");
Fputs ($ filehandle, $ message );
Fclose ($ filehandle );
}
}

//////////////////////////////////////// //////////////////////////
In this way, the form processing is complete, and the following active update program will display the new speech content on the screen.
Upper
//////////////////////////////////////// //////////////////////////

4. automatically update the automatic cyclic call method

Available & Pass = ?> & Amp; last = "+ winnumber;
Flush. location = url
Flushflag = false
}

// Otherwise, wait for a loop
FlushtimerID = setTimeout ("flushstart ()", 2000 );
FlushtimerRunning = true;
}

This method ensures that the background update program will continue to run if the main program does not die!


5. actively update the program

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.