Online chat on an iPad-like interface
The source code of the web online chat (iPad interface) is included in the ichat.zip package.
It consists of three files:
Index. php: obtain the nickname file from the form value and pass the value to the chat. php file;
Chat. php: In the chat file, use iframe to call view. php to display the chat content;
View. php: reads the content of the chat.txt file by row and outputs it in reverse order. the content is automatically refreshed every 20 seconds.
To be improved: (new to me, learning)
1. anti-refresh mechanism
2. ajax should be used to read the content
Demo address: http://qhbbs.tk/
- /* Create a session and determine whether to enter a nickname. if not, it is a visitor */
- Session_start ();
- If (isset ($ _ SESSION ['View'])
- $ _ SESSION ['View'] = $ _ SESSION ['View'] + 1;
- Else
- $ _ SESSION ['View'] = 1;
- If ($ _ SESSION ['View'] = 1) $ _ SESSION ['username'] = $ _ POST ['user'];
- If (! $ _ SESSION ['username']) $ _ SESSION ['username'] = "tourist ";
- If ($ _ POST ['user']) $ _ SESSION ['username'] = $ _ POST ['user'];
- $ User = $ _ SESSION ['username'];
- $ Words = $ _ POST ['word']; // chat content assignment
- If (empty ($ words) exit;
- Savechat ($ words, $ _ SESSION ['username']); // Save the chat content
- /* The following is the function for saving chat content */
- Function savechat ($ msg, $ user)
- {
- $ Date = date ('H: I: S', time ());
- $ DOCUMENT_ROOT = $ _ SERVER ['document _ root'];
- If (! $ Fp = fopen ("$ DOCUMENT_ROOT/chat.txt", 'A + ')){
- Die ('failed to create the chat record file. Please check if you have the permission .');
- }
- $ Msg = htmlspecialchars ($ msg );
- $ Msg = preg_replace ('/([httpftp: \ //]) * ([a-zA-]) + \. ([a-zA-Z0-9 _-]) + \. ([a-zA-Z0-9 _-]) + (a-zA-Z0-9 _) */',' \ 0', $ msg );
- $ Msg = preg_replace ('/([a-zA-Z0-9 _ \.]) + @ ([a-zA-Z0-9-]) + \. ([a-zA-Z0-9-] {2, 4}) +/',' \ 0', $ msg );
- $ Msg = '['. $ date. ']'. "\ t". $ user. ":". $ msg. "\ n ";
- If (! Fwrite ($ fp, $ msg )){
- Die ('failed to write the chat record .');
- }
- Fclose ($ fp );
- }
- ?>
- $ DOCUMENT_ROOT = $ _ SERVER ['document _ root'];
- $ Fp = fopen ("$ DOCUMENT_ROOT/chat.txt", 'A + ');
- If (! $ Fp ){
- Echo"
Didn't write chat log in chat.txt. Please try say again. ";
- Exit;
- }
- $ Handle = $ fp;
- $ Temp_arr = array ();
- Do
- {
- $ File = fgets ($ handle, 1024 );
- $ Temp_arr [] = $ file;
- }
- While (! Feof ($ handle ));
- Fclose ($ handle );
- Krsort ($ temp_arr); // reverse
- Foreach ($ temp_arr as $ value ){
- Echo "". $ value .""."
";
- }
- ?>
|