本文实例讲述了PHP+mysql+ajax轻量级聊天室实现方法。分享给大家供大家参考,具体如下:
做了一个QQ聊天交友网站,想加个聊天的功能,于是做完用PHP做了简单又强大的聊天室
1. 创建mysql数据库表:
复制代码 代码如下:create table chat( id bigint AUTO_INCREMENT,username varchar(20), chatdate datetime,msg varchar(500), primary key(id));
2.编写建议连接数据库函数:
dbconnect.php
<"Asia/Shanghai"); $link = mysql_connect("xxx.xxx.xxx.xxx", "databasename", "password") or die('无法连接: ' . mysql_error()); mysql_select_db("databasename") or die('没有你找到指定数据库'); return true; } function quote($strText) { $Mstr = addslashes($strText); return "'" . $Mstr . "'"; } function isdate($d) { $ret = true; try { $x = date("d",$d); } catch (Exception $e) { $ret = false; } echo $x; return $ret; } "htmlcode"><"UTF-8","GB2312",$_GET["msg"]); $dt = date("Y-m-d H:i:s"); $user = iconv("UTF-8","GB2312",$_GET["name"]); $sql="INSERT INTO chat(USERNAME,CHATDATE,MSG) " . "values(" . quote($user) . "," . quote($dt) . "," . quote($msg) . ");"; echo $sql; $result = mysql_query($sql); if(!$result) { throw new Exception('Query failed: ' . mysql_error()); exit(); } "htmlcode"><"Content-Type:text/html;charset=gb2312"); header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); require_once('dbconnect.php'); db_connect(); $sql = "SELECT *, date_format(chatdate,'%Y年%m月%d日 %r') as cdt from chat order by ID desc limit 200"; $sql = "SELECT * FROM (" . $sql . ") as ch order by ID"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); // Update Row Information $msg="<table border='0' style='font-size: 10pt; color: white; font-family: verdana, arial;'>"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { $msg = $msg . "<tr><td>" . $line["cdt"] . " </td>" . "<td>" . $line["username"] . ": </td>" . "<td>" . $line["msg"] . "</td></tr>"; } $msg=$msg . "</table>"; echo $msg; "htmlcode"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>聊天页面</title> <script type="text/javascript"> var t = setInterval(function(){get_chat_msg()},5000); // // General Ajax Call // var oxmlHttp; var oxmlHttpSend; function get_chat_msg() { if(typeof XMLHttpRequest != "undefined") { oxmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { oxmlHttp = new ActiveXObject("Microsoft.XMLHttp"); } if(oxmlHttp == null) { alert("浏览器不支持XML Http Request!"); return; } oxmlHttp.onreadystatechange = get_chat_msg_result; oxmlHttp.open("GET",encodeURI("chat_recv_ajax.php"),true); oxmlHttp.send(null); } function get_chat_msg_result() { if(oxmlHttp.readyState==4 || oxmlHttp.readyState=="complete") { if (document.getElementById("DIV_CHAT") != null) { document.getElementById("DIV_CHAT").innerHTML = oxmlHttp.responseText; oxmlHttp = null; } var scrollDiv = document.getElementById("DIV_CHAT"); scrollDiv.scrollTop = scrollDiv.scrollHeight; } } function set_chat_msg() { if(typeof XMLHttpRequest != "undefined") { oxmlHttpSend = new XMLHttpRequest(); } else if (window.ActiveXObject) { oxmlHttpSend = new ActiveXObject("Microsoft.XMLHttp"); } if(oxmlHttpSend == null) { alert("浏览器不支持XML Http Request!"); return; } var url = "chat_send_ajax.php"; var strname="noname"; var strmsg=""; if (document.getElementById("txtname") != null) { strname = document.getElementById("txtname").value; document.getElementById("txtname").readOnly=true; } if (document.getElementById("txtmsg") != null) { strmsg = document.getElementById("txtmsg").value; document.getElementById("txtmsg").value = ""; } url += "" + strname + "&msg=" + strmsg; oxmlHttpSend.open("GET",encodeURI(url),true); oxmlHttpSend.send(null); } function clickBtn(e) { if(window.event.keyCode==13) { var id=e.id; switch(id) { case "txtmsg": document.getElementById("Submit2").click(); window.event.returnValue=false; break; } } } function fRandomBy(under, over){ switch(arguments.length){ case 1: return parseInt(Math.random()*under+1); case 2: return parseInt(Math.random()*(over-under+1) + under); default: return 0; } } function SetTxtName(){ var i=fRandomBy(10); if(i==0)document.getElementById('txtname').value='无敌战神'; if(i==1)document.getElementById('txtname').value='令狐冲'; if(i==2)document.getElementById('txtname').value='西门吹雪'; if(i==3)document.getElementById('txtname').value='超级玛丽'; if(i==4)document.getElementById('txtname').value='奥巴马'; if(i==5)document.getElementById('txtname').value='恐怖分子'; if(i==6)document.getElementById('txtname').value='聊斋奇女子'; if(i==7)document.getElementById('txtname').value='天朝"SetTxtName();"> <div style="border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid; background:#fff url('http://www.ihaonet.com/chat/blue.jpg') repeat-x left top; height: 450px;width: 500px; "> <table style="width:100%; height:100%"> <tr> <td colspan="2" style="font-weight: bold; font-size: 16pt; color: white; font-family: verdana, arial; text-align: center"> 聊天窗口--全球最大QQ聊天交友网站</td> </tr> <tr> <td colspan="2" style="font-weight: bold; font-size: 16pt; color: white; font-family: verdana, arial; text-align: left"> <table style="font-size: 12pt; color: white; font-family: Verdana, Arial;border: white thin solid; "> <tr> <td style="width: 100px"> 名字:</td> <td style="width: 100px"><input id="txtname" style="width: 150px" type="text" name="name" maxlength="15" value="匿名" /></td> </tr> </table> </td> </tr> <tr> <td style="vertical-align: middle;" valign="middle" colspan="2"> <div style="width: 480px; height: 300px; border-right: white thin solid; border-top: white thin solid; font-size: 10pt; border-left: white thin solid; border-bottom: white thin solid; font-family: verdana, arial; overflow:scroll; text-align: left;" id="DIV_CHAT"> </div> </td> </tr> <tr> <td style="width: 310px"> <input id="txtmsg" style="width: 350px" type="text" name="msg" onkeydown="return clickBtn(this)"/></td> <td style="width: 85px"> <input id="Submit2" style="font-family: verdana, arial" type="button" value="发送" onclick="set_chat_msg()"/></td> </tr> <tr> <td colspan="1" style="font-family: verdana, arial; text-align: center; width: 350px;"> </td> <td colspan="1" style="width: 85px; font-family: verdana, arial; text-align: center"> </td> </tr> </table> </div> </body> </html>效果图如下:
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+MySQL留言板开发专题》、《PHP+ajax技巧与应用小结》、《php文件操作总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“PHP+mysql+ajax轻量级聊天室实现方法详解”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。