Home > Backend Development > PHP Tutorial > Online group chat with iPad-like interface

Online group chat with iPad-like interface

WBOY
Release: 2016-07-25 09:01:36
Original
1409 people have browsed it
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/
Online group chat with iPad-like interface
  1. /*Create a session, determine whether to fill in the nickname, if not, it will be a visitor*/
  2. session_start();
  3. if(isset($_SESSION['views']))
  4. $_SESSION['views ']=$_SESSION['views']+1;
  5. else
  6. $_SESSION['views']=1;
  7. if($_SESSION['views']==1)$_SESSION['username']=$_POST ['user'];
  8. if(!$_SESSION['username'])$_SESSION['username']="Guest";
  9. if($_POST['user'])$_SESSION['username']=$ _POST['user'];
  10. $user=$_SESSION['username'];
  11. $words=$_POST['words'];//Assignment of chat content
  12. if(empty($words))exit;
  13. savechat( $words,$_SESSION['username']);//Save chat content
  14. /*The following is the function to save chat content*/
  15. function savechat($msg, $user)
  16. {
  17. $date=date('H:i :s',time());
  18. $DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
  19. if (!$fp=fopen("$DOCUMENT_ROOT/chat.txt",'a+')) {
  20. die('Create Chat history file failed, please check if you have permission.');
  21. }
  22. $msg = htmlspecialchars($msg);
  23. $msg = preg_replace('/([httpftp://])*([a-zA-] )+.([a-zA-Z0-9_-])+.([a-zA-Z0-9_-])+(a-zA-Z0-9_)*/', '\0', $msg);
  24. $msg = preg_replace('/([a-zA-Z0-9_.])+@([a-zA-Z0-9-] )+.([a-zA-Z0-9-]{2,4})+/', '\0', $msg);
  25. $msg ='['.$date.']'."t".$user.":".$msg."n";
  26. if (!fwrite($fp, $msg)) {
  27. die('write Chat record failed.');
  28. }
  29. fclose($fp);
  30. }
  31. ?>
Copy code
  1. $DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT'];
  2. $fp=fopen("$DOCUMENT_ROOT/chat.txt",'a+');
  3. if(!$fp){
  4. echo "

    Didn't write chat log in chat.txt.Please try say again.

    ";
  5. exit;
  6. }
  7. $handle=$fp;
  8. $temp_arr=array();
  9. do
  10. {
  11. $file=fgets($handle,1024);
  12. $temp_arr[]=$file;
  13. }
  14. while(!feof($handle));
  15. fclose($handle );
  16. krsort($temp_arr);//Reverse order
  17. foreach($temp_arr as $value){
  18. echo "".$value." "."
    ";
  19. }
  20. ?>
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template