Web Online Group chat (ipad interface) source code all in Ichat.zip compressed package The main consists of three files: index.php: Form values get nickname file, pass value tochat.phpFile Chat . PHP : main file, get index.php value, determine whether to fill in the nickname, Empty is a visitor, this page contains chat input text box, submitted to this page processing, Save chat record in auto-created Chat.txt file, use IFRAME to call view.php to display chat content; View . PHP : Read the contents of the Chat.txt file by line, reverse output, automatically refresh every 20s interval To improve: ( I am a novice, is learning ) 1. Anti-refresh mechanism 2. Read the content should use Ajax Demo Address: http://qhbbs.tk/
- /* Create a session to determine whether to fill in the nickname, no visitors */
- Session_Start ();
- if (isset ($_session[' views '))
- $_session[' views ']=$_session[' views ']+1;
- Else
- $_session[' views ']=1;
- if ($_session[' views ']==1) $_session[' username ']=$_post[' user '];
- if (!$_session[' username ') $_session[' username ']= "tourists";
- if ($_post[' user ') $_session[' username ']=$_post[' user '];
- $user =$_session[' username '];
- $words =$_post[' words '];//chat content Assignment
- if (empty ($words)) exit;
- Savechat ($words, $_session[' username ');//Save chat content
- /* The following is the Save Chat content function */
- function Savechat ($msg, $user)
- {
- $date =date (' H:i:s ', Time ());
- $DOCUMENT _root=$_server[' Document_root '];
- if (! $fp =fopen ("$DOCUMENT _root/chat.txt", ' A + ')) {
- Die (' Create chat log file failed, please check if there is 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 (' Write chat record failed. ');
- }
- Fclose ($FP);
- }
- ?>
Copy Code
- $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 order
- foreach ($temp _arr as $value) {
- echo "". $value. "". "
";
- }
- ?>
Copy Code |