分享简单的聊天室程序的php代码
分类:PHP_Python| 发布:佚名| 查看:292 | 发表时间:2016/1/27
核心逻辑
在定义应用程序的核心功能之前,先来看一看聊天应用程序的基本外观,如以下截图所示:

通过聊天窗口底部的输入框输入聊天文本。点击Send按钮,就开始执行函数set_chat_msg。这是一个基于Ajax的函数,因此无需刷新页面就可以将聊天文本发送到服务器。程序在服务器中执行chat_send_ajax.php以及用户名和聊天文本。
05 | function set_chat_msg() |
07 | if (typeof XMLHttpRequest != "undefined" ) |
09 | oxmlHttpSend = new XMLHttpRequest(); |
11 | else if (window.ActiveXObject) |
13 | oxmlHttpSend = new ActiveXObject( "Microsoft.XMLHttp" ); |
15 | if (oxmlHttpSend == null) |
17 | alert( "Browser does not support XML Http Request" ); |
21 | var url = "chat_send_ajax.php" ; |
24 | if (document.getElementById( "txtname" ) != null) |
26 | strname = document.getElementById( "txtname" ).value; |
27 | document.getElementById( "txtname" ).readOnly=true; |
29 | if (document.getElementById( "txtmsg" ) != null) |
31 | strmsg = document.getElementById( "txtmsg" ).value; |
32 | document.getElementById( "txtmsg" ).value = "" ; |
35 | url += "?name=" + strname + "&msg=" + strmsg; |
36 | oxmlHttpSend.open( "GET" ,url,true); |
37 | oxmlHttpSend.send(null); |
PHP模块从Query String(查询字符串)中接收表单数据,更新到命名为chat的数据库表中。chat数据库表有命名为ID、USERNAME、CHATDATE和MSG的列。ID字段是自动递增字段,所以这个ID字段的赋值将自动递增。当前的日期和时间,会更新到CHATDATE列。
01 | require_once ( 'dbconnect.php' ); |
06 | $dt = date ( "Y-m-d H:i:s" ); |
09 | $sql = "INSERT INTO chat(USERNAME,CHATDATE,MSG) " . |
10 | "values(" . quote( $user ) . "," . |
11 | quote( $dt ) . "," . quote( $msg ) . ");" ; |
15 | $result = mysql_query( $sql ); |
18 | throw new Exception( 'Query failed: ' . mysql_error()); |
为了接收来自数据库表中所有用户的聊天消息,timer函数被设置为循环5秒调用以下的JavaScript命令,即每隔5秒时间执行get_chat_msg函数。
1 | var t = setInterval( function (){get_chat_msg()},5000); |
get_chat_msg是一个基于Ajax的函数。它执行chat_recv_ajax.php程序以获得来自于数据库表的聊天信息。在 onreadystatechange属性中,另一个JavaScript 函数get_chat_msg_result被连接起来。在返回来自于数据库表中的聊天消息的同时,程序控制进入到 get_chat_msg_result函数。
08 | function get_chat_msg() |
10 | if (typeof XMLHttpRequest != "undefined" ) |
12 | oxmlHttp = new XMLHttpRequest(); |
14 | else if (window.ActiveXObject) |
16 | oxmlHttp = new ActiveXObject( "Microsoft.XMLHttp" ); |
20 | alert( "Browser does not support XML Http Request" ); |
24 | oxmlHttp.onreadystatechange = get_chat_msg_result; |
25 | oxmlHttp.open( "GET" , "chat_recv_ajax.php" ,true); |
在chat_recv_ajax.php程序中,来自于用户的聊天消息会通过SQL select命令进行收集。为了限制行数,在SQL查询中还给出了限制子句(limit 200),即要求聊天数据库表中的最后200行。所获得的消息再返回给Ajax函数,用于在聊天窗口中显示内容。
01 | require_once ( 'dbconnect.php' ); |
05 | $sql = "SELECT *, date_format(chatdate, '%d-%m-%Y %r' ) |
06 | as cdt from chat order by ID desc limit 200"; |
07 | $sql = "SELECT * FROM (" . $sql . ") as ch order by ID" ; |
08 | $result = mysql_query( $sql ) or die ( 'Query failed: ' . mysql_error()); |
12 | while ( $line = mysql_fetch_array( $result , MYSQL_ASSOC)) |
18 | $msg = $msg . "<table style=" color: blue; font-family: verdana, arial; " . |
19 | "font-size: 10pt;" border= "0" > |
20 | <tbody><tr><td> " . $line[" cdt"] . |
21 | " </td><td>" . $line [ "username" ] . |
22 | ": </td><td>" . $line [ "msg" ] . |
23 | "</td></tr></tbody></table>" ; |
26 | function get_chat_msg_result(t) |
28 | if (oxmlHttp.readyState==4 || oxmlHttp.readyState== "complete" ) |
30 | if (document.getElementById( "DIV_CHAT" ) != null) |
32 | document.getElementById( "DIV_CHAT" ).innerHTML = oxmlHttp.responseText; |
35 | var scrollDiv = document.getElementById( "DIV_CHAT" ); |
36 | scrollDiv.scrollTop = scrollDiv.scrollHeight; |
下面的SQL CREATE TABLE命令可用于创建名为chat的数据库表。所有由用户输入的信息都会进入到数据库表中。
create table chat( id bigint AUTO_INCREMENT,username varchar(20),
chatdate datetime,msg varchar(500), primary key(id));
这段用于实现聊天应用程序的代码非常有意思,它可以改进成为一个完全成熟的HTTP聊天应用程序,创建该应用程序的逻辑也非常简单,即使是初学者理解起来也不会有任何困难,希望这篇文章对大家的学习有所帮助。