当前位置:首页 > PHP源码 > 列表

PHP+mysql+ajax轻量级聊天室

发布:smiling 来源: PHP粉丝网  添加日期:2018-05-31 10:49:37 浏览: 评论:0 

本文实例讲述了PHP+mysql+ajax轻量级聊天室实现方法。分享给大家供大家参考,具体如下:做了一个QQ聊天交友网站,想加个聊天的功能,于是做完用PHP做了简单又强大的聊天室

1. 创建mysql数据库表:

  1. create table chat( id bigint AUTO_INCREMENT,username varchar(20), chatdate datetime,msg varchar(500), primary key(id)); 

2.编写建议连接数据库函数:dbconnect.php

  1. <?php 
  2. function db_connect() 
  3. { 
  4.  date_default_timezone_set("Asia/Shanghai"); 
  5.  $link = mysql_connect("xxx.xxx.xxx.xxx", "databasename", "password") 
  6.       or die('无法连接: ' . mysql_error()); 
  7.  mysql_select_db("databasename") or die('没有你找到指定数据库'); 
  8.  return true; 
  9. } 
  10. function quote($strText) 
  11. { 
  12.   $Mstr = addslashes($strText); 
  13.   return "'" . $Mstr . "'"; 
  14. } 
  15. function isdate($d) 
  16. { 
  17.   $ret = true; 
  18.   try 
  19.   { 
  20.     $x = date("d",$d); 
  21.   } 
  22.   catch (Exception $e) 
  23.   { 
  24.     $ret = false; 
  25.   } 
  26.   echo $x; 
  27.   return $ret; 
  28. } 
  29. ?> 

3. 编写ajax发送和接收函数:ajax发送函数chat_send_ajax.php

  1. <?php 
  2.    require_once('dbconnect.php'); 
  3.    db_connect(); 
  4.    $msg = iconv("UTF-8","GB2312",$_GET["msg"]); 
  5.    $dt = date("Y-m-d H:i:s"); 
  6.    $user = iconv("UTF-8","GB2312",$_GET["name"]); 
  7.    $sql="INSERT INTO chat(USERNAME,CHATDATE,MSG) " . 
  8.      "values(" . quote($user) . "," . quote($dt) . "," . quote($msg) . ");"; 
  9.      echo $sql; 
  10.    $result = mysql_query($sql); 
  11.    if(!$result) 
  12.    { 
  13.     throw new Exception('Query failed: ' . mysql_error()); 
  14.     exit(); 
  15.    } 
  16. ?> 

ajax接收函数chat_recv_ajax.php

  1. <?php 
  2. header("Content-Type:text/html;charset=gb2312"); 
  3. header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 
  4.   header("Cache-Control: no-cache, must-revalidate"); 
  5.   header("Pragma: no-cache"); 
  6.    require_once('dbconnect.php'); 
  7.    db_connect(); 
  8.    $sql = "SELECT *, date_format(chatdate,'%Y年%m月%d日 %r') as cdt from chat order by ID desc limit 200"; 
  9.    $sql = "SELECT * FROM (" . $sql . ") as ch order by ID"; 
  10.    $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); 
  11.    // Update Row Information 
  12.    $msg="<table border='0' style='font-size: 10pt; color: white; font-family: verdana, arial;'>"; 
  13.    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
  14.    { 
  15.       $msg = $msg . "<tr><td>" . $line["cdt"] . " </td>" . 
  16.         "<td>" . $line["username"] . ": </td>" . 
  17.         "<td>" . $line["msg"] . "</td></tr>"; 
  18.    } 
  19.    $msg=$msg . "</table>"; 
  20.    echo $msg; 
  21. ?> 

4.聊天室页面:chat.html

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml" > 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  6.   <title>聊天页面</title> 
  7. <script type="text/javascript"> 
  8. var t = setInterval(function(){get_chat_msg()},5000); 
  9. // 
  10. // General Ajax Call 
  11. // 
  12. var oxmlHttp; 
  13. var oxmlHttpSend; 
  14. function get_chat_msg() 
  15. { 
  16.   if(typeof XMLHttpRequest != "undefined") 
  17.   { 
  18.     oxmlHttp = new XMLHttpRequest(); 
  19.   } 
  20.   else if (window.ActiveXObject) 
  21.   { 
  22.     oxmlHttp = new ActiveXObject("Microsoft.XMLHttp"); 
  23.   } 
  24.   if(oxmlHttp == null) 
  25.   { 
  26.     alert("浏览器不支持XML Http Request!"); 
  27.     return; 
  28.   } 
  29.   oxmlHttp.onreadystatechange = get_chat_msg_result; 
  30.   oxmlHttp.open("GET",encodeURI("chat_recv_ajax.php"),true); 
  31.   oxmlHttp.send(null); 
  32. } 
  33. function get_chat_msg_result() 
  34. { 
  35.   if(oxmlHttp.readyState==4 || oxmlHttp.readyState=="complete") 
  36.   { 
  37.     if (document.getElementById("DIV_CHAT") != null) 
  38.     { 
  39.       document.getElementById("DIV_CHAT").innerHTML = oxmlHttp.responseText; 
  40.       oxmlHttp = null; 
  41.     } 
  42.     var scrollDiv = document.getElementById("DIV_CHAT"); 
  43.     scrollDivscrollDiv.scrollTop = scrollDiv.scrollHeight; 
  44.   } 
  45. } 
  46. function set_chat_msg() 
  47. { 
  48.   if(typeof XMLHttpRequest != "undefined") 
  49.   { 
  50.     oxmlHttpSend = new XMLHttpRequest(); 
  51.   } 
  52.   else if (window.ActiveXObject) 
  53.   { 
  54.     oxmlHttpSend = new ActiveXObject("Microsoft.XMLHttp"); 
  55.   } 
  56.   if(oxmlHttpSend == null) 
  57.   { 
  58.     alert("浏览器不支持XML Http Request!"); 
  59.     return; 
  60.   } 
  61.   var url = "chat_send_ajax.php"; 
  62.   var strname="noname"; 
  63.   var strmsg=""; 
  64.   if (document.getElementById("txtname") != null) 
  65.   { 
  66.     strname = document.getElementById("txtname").value; 
  67.     document.getElementById("txtname").readOnly=true; 
  68.   } 
  69.   if (document.getElementById("txtmsg") != null) 
  70.   { 
  71.     strmsg = document.getElementById("txtmsg").value; 
  72.     document.getElementById("txtmsg").value = ""; 
  73.   } 
  74.   url += "?name=" + strname + "&msg=" + strmsg; 
  75.   oxmlHttpSend.open("GET",encodeURI(url),true); 
  76.   oxmlHttpSend.send(null); 
  77. } 
  78. function clickBtn(e) 
  79.  { 
  80.   if(window.event.keyCode==13) 
  81.   { 
  82.   var id=e.id; 
  83.   switch(id) 
  84.   { 
  85.    case "txtmsg": 
  86.    document.getElementById("Submit2").click(); 
  87.    window.event.returnValue=false; 
  88.    break; 
  89.    } 
  90.   } 
  91. } 
  92. function fRandomBy(under, over){ 
  93. switch(arguments.length){ 
  94. case 1: return parseInt(Math.random()*under+1); 
  95. case 2: return parseInt(Math.random()*(over-under+1) + under); 
  96. default: return 0; 
  97. } 
  98. } 
  99. function SetTxtName(){ 
  100. var i=fRandomBy(10); 
  101. if(i==0)document.getElementById('txtname').value='无敌战神'; 
  102. if(i==1)document.getElementById('txtname').value='令狐冲'; 
  103. if(i==2)document.getElementById('txtname').value='西门吹雪'; 
  104. if(i==3)document.getElementById('txtname').value='超级玛丽'; 
  105. if(i==4)document.getElementById('txtname').value='奥巴马'; 
  106. if(i==5)document.getElementById('txtname').value='恐怖分子'; 
  107. if(i==6)document.getElementById('txtname').value='聊斋奇女子'; 
  108. if(i==7)document.getElementById('txtname').value='天朝?潘?; 
  109. if(i==8)document.getElementById('txtname').value='中500万了'; 
  110. if(i==9)document.getElementById('txtname').value='神级奇葩'; 
  111. if(i==10)document.getElementById('txtname').value='爱你不是两三天'; 
  112. } 
  113. </script> 
  114. </head> 
  115. <body onload="SetTxtName();"> 
  116.   <div style="border-right: black thin solid; border-top: black thin solid; 
  117.     border-left: black thin solid; border-bottom: black thin solid; 
  118.     background:#fff url('http://www.ihaonet.com/chat/blue.jpg') repeat-x left top; 
  119.     height: 450px;width: 500px; "> 
  120.     <table style="width:100%; height:100%"> 
  121.       <tr> 
  122.         <td colspan="2" style="font-weight: bold; font-size: 16pt; color: white; font-family: verdana, arial; 
  123.           text-align: center"> 
  124.           聊天窗口--全球最大QQ聊天交友网站</td> 
  125.       </tr> 
  126.       <tr> 
  127.         <td colspan="2" style="font-weight: bold; font-size: 16pt; color: white; font-family: verdana, arial; 
  128.           text-align: left"> 
  129.           <table style="font-size: 12pt; color: white; font-family: Verdana, Arial;border: white thin solid; "> 
  130.             <tr> 
  131.               <td style="width: 100px"> 
  132.                 名字:</td> 
  133.               <td style="width: 100px"><input id="txtname" style="width: 150px" type="text" name="name" maxlength="15" value="匿名" /></td> 
  134.             </tr> 
  135.           </table> 
  136.         </td> 
  137.       </tr> 
  138.       <tr> 
  139.         <td style="vertical-align: middle;" valign="middle" colspan="2"> 
  140.           <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"> 
  141.           </div> 
  142.         </td> 
  143.       </tr> 
  144.       <tr> 
  145.         <td style="width: 310px"> 
  146.           <input id="txtmsg" style="width: 350px" type="text" name="msg" onkeydown="return clickBtn(this)"/></td> 
  147.         <td style="width: 85px"> 
  148.           <input id="Submit2" style="font-family: verdana, arial" type="button" value="发送" onclick="set_chat_msg()"/></td> 
  149.       </tr> 
  150.       <tr> 
  151.         <td colspan="1" style="font-family: verdana, arial; text-align: center; width: 350px;"> 
  152.           </td> 
  153.         <td colspan="1" style="width: 85px; font-family: verdana, arial; text-align: center"> 
  154.         </td> 
  155.       </tr> 
  156.     </table> 
  157.   </div> 
  158. </body> 
  159. </html>

Tags: 轻量级 聊天室

分享到:

相关文章