Web online group chat (iPad interface) source code is all in the ichat.zip compressed package
Mainly includes three files:
index.php: The form value gets the nickname file and passes the value to
chat.php file;
chat
.php
:Main file, after getting the value passed by index.php, determine whether to fill in the nickname, if it is empty, it is the visitor. This page contains a chat content input text box, which is submitted to this page for processing, and the chat record is saved in the automatically created chat. txt file, use iframe to call view.php to display the chat content;
view
.php
:
Read the contents of the chat.txt file line by line, output in reverse order, and automatically refresh every 20s Areas to be improved: (I am a newbie and am learning) 1. Anti-refresh mechanism 2. To read the content, use ajax Demo address: http://qhbbs.tk/
- /*Create a session, determine whether to fill in the nickname, if not, it will be a visitor*/
- 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']="Guest";
- if($_POST['user'])$_SESSION['username']=$ _POST['user'];
- $user=$_SESSION['username'];
- $words=$_POST['words'];//Assignment of chat content
- if(empty($words))exit;
- savechat( $words,$_SESSION['username']);//Save chat content
- /*The following is the function to save 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('Create Chat history file failed, please check if you have 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
|