Home  >  Article  >  Backend Development  >  Writing a chat room with PHP+MySql_PHP tutorial

Writing a chat room with PHP+MySql_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:02:48659browse

Hello everyone! I haven’t seen you all for a long time. Last time I wrote about a chat room that uses PHP to operate through files. Of course it is full of loopholes, and every time I refresh the screen it is terrible! I have been thinking about it for a long time. Is there a way to get the speech information without refreshing? This effect can be achieved by using one more framework. That is, using one more framework is called getmsg. We let getmeg obtain information and ensure that the information obtained each time is the latest. That is, the speech obtained last time is not needed. Last time I edited The problem is that every time the information is obtained, it is repeated, which is not effective.
My design idea is: for every speech, submit it to the server and then save it to a database. Getmsg will go to the server to obtain the latest speech data every few seconds, and ensure the last speech obtained. Don't remove it, and then superimpose the removed speech into a window (listmsg) dedicated to displaying speeches. In this way, we feel that the listmsg window will not have to be refreshed. Moreover, we can only see the speech information being added continuously, which has a very good effect.
How to add information from one frame to another frame? This idea can be realized using JavaScript:

Of course you have to build two tables in mysql, one table is used to save speech information, and the other table is used to save several online ones. When counting the number of people, we let it refresh every 60 seconds, delete the speeches made one minute ago, and see if a certain speaker has not spoken for a long time. If so, delete him.
Due to The subsections of the table are very simple, so there is no need to write them out here. You will know them by looking at the program.This program works very well both online and offline!
表的名字是:chat和chat_getmsg

if(!isset($username))  
    $username="guest";
  $conid=mysql_connect("localhost","yourcounter","password");
  mysql_select_db("yourdadabase",$conid);
$dstr=date("YmdHis");
$sql="insert chat_getmsg (username,shijian) values ('$username','$dstr')";
mysql_query($sql,$conid);
$sql="update chat_getmsg set shijian='$dstr' where username='$username'";
mysql_query($sql,$conid);
mysql_close($conid);
?>


Untitled Document


  
    
    
    
  
    
   n"; ?>
   n"; ?>
  

<body bgcolor="#FFFFFF"> <br><br></body>


if(!isset($username))
     $username="guest";
if(!isset($yanse))
      $yanse="blue";
if(!isset($objectname))
      $objectname="大家";
?>


Untitled Document





class="normalfont">
  
      
      
      
      
    
      
      
      
      
    
  
颜色:  
        
      
发言:  
        

         >

        
      
对象:
         >
      
回去





if(isset($username)&&isset($fayan)){
$conid=mysql_connect("localhost","yourcount","yourpassword");
mysql_select_db("database",$conid);
$sql="insert chat (username,objectname,action,msg,color) values ("$username","$objectname","NO","$fayan","$yanse")";
mysql_query($sql,$conid);
mysql_close($conid);
}
?>



Untitled Document

$conid=mysql_pconnect("localhost","yourcount","password");
mysql_select_db("database",$conid);

if(!isset($username))
     $username="getmsg";  
$dt=time();   
$newdate=date("YmdHis",$dt);
$fromdate=date("YmdHis",$dt-6);;

$sql="select username as name,msg,action,objectname ,shijian,color from chat where shijian>="$fromdate"";
$res=mysql_query($sql,$conid);

echo "n";
echo "n";

echo "n";
?>







Untitled Document

if(!isset($username))
$username="guest";
echo "n";
echo "n";

echo "n";
?>






Untitled Document







欢迎光临












Untitled Document







  在线人数



if(!isset($username))
    $username="guest";
$dt=time();   
$newdate=date("YmdHis",$dt);
$fromdate=date("YmdHis",$dt-200);
$linkid=mysql_connect("localhost","yourcount","password");
mysql_select_db("yourdatabase",$linkid);
$sql="update chat_getmsg set shijian="$newdate" where username="$username"";
mysql_query($sql,$linkid);
$sql="select username from chat_getmsg where shijian>="$fromdate"";
$res=mysql_query($sql,$linkid);  
while(list($username)=mysql_fetch_row($res)){
     echo "$username
n";
$fromdate=date("YmdHis",$dt-100);
$sql="delete from chat where shijian<="$fromdate"";
mysql_query($sql,$linkid);
mysql_close($linkid);
}
?>






【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】    

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/316487.htmlTechArticle大家好!好久没有看到大家了,上次写了用PHP的通过文件操作的聊天室,当然是漏洞百出的,而且每一次刷新屏幕都很糟糕的!我想了好久...
Statement:
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