自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP實現(xiàn)最簡單的聊天室應(yīng)用

開發(fā) 后端
這篇文章,以及任何相關(guān)的源代碼和文件,都獲得了The Code Project Open License (CPOL)的許可。

介紹

聊天應(yīng)用程序在網(wǎng)上非常常見。開發(fā)人員在構(gòu)建這類應(yīng)用程序時的選擇也很多。這篇文章介紹了如何實現(xiàn)基于PHP-AJAX的聊天應(yīng)用程序,并且不需要刷新頁面就可以發(fā)送和接收消息。

核心邏輯

在定義應(yīng)用程序的核心功能之前,先來看一看聊天應(yīng)用程序的基本外觀,如以下截圖所示:

通過聊天窗口底部的輸入框輸入聊天文本。點擊Send按鈕,就開始執(zhí)行函數(shù)set_chat_msg。這是一個基于Ajax的函數(shù),因此無需刷新頁面就可以將聊天文本發(fā)送到服務(wù)器。程序在服務(wù)器中執(zhí)行chat_send_ajax.php以及用戶名和聊天文本。

 

  1. // 
  2. // Set Chat Message 
  3. // 
  4.  
  5. function set_chat_msg() 
  6.     if(typeof XMLHttpRequest != "undefined"
  7.     { 
  8.         oxmlHttpSend = new XMLHttpRequest(); 
  9.     } 
  10.     else if (window.ActiveXObject) 
  11.     { 
  12.        oxmlHttpSend = new ActiveXObject("Microsoft.XMLHttp"); 
  13.     } 
  14.     if(oxmlHttpSend == null
  15.     { 
  16.        alert("Browser does not support XML Http Request"); 
  17.        return
  18.     } 
  19.  
  20.     var url = "chat_send_ajax.php"
  21.     var strname="noname"
  22.     var strmsg=""
  23.     if (document.getElementById("txtname") != null
  24.     { 
  25.         strname = document.getElementById("txtname").value; 
  26.         document.getElementById("txtname").readOnly=true
  27.     } 
  28.     if (document.getElementById("txtmsg") != null
  29.     { 
  30.         strmsg = document.getElementById("txtmsg").value; 
  31.         document.getElementById("txtmsg").value = ""
  32.     } 
  33.  
  34.     url += "?name=" + strname + "&msg=" + strmsg; 
  35.     oxmlHttpSend.open("GET",url,true); 
  36.     oxmlHttpSend.send(null); 

PHP模塊從Query String(查詢字符串)中接收表單數(shù)據(jù),更新到命名為chat的數(shù)據(jù)庫表中。chat數(shù)據(jù)庫表有命名為ID、USERNAMECHATDATEMSG的列。ID字段是自動遞增字段,所以這個ID字段的賦值將自動遞增。當(dāng)前的日期和時間,會更新到CHATDATE列。

 

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

為了接收來自數(shù)據(jù)庫表中所有用戶的聊天消息,timer函數(shù)被設(shè)置為循環(huán)5秒調(diào)用以下的JavaScript命令,即每隔5秒時間執(zhí)行g(shù)et_chat_msg函數(shù)。

var t = setInterval(function(){get_chat_msg()},5000);

get_chat_msg是一個基于Ajax的函數(shù)。它執(zhí)行chat_recv_ajax.php程序以獲得來自于數(shù)據(jù)庫表的聊天信息。在 onreadystatechange屬性中,另一個JavaScript 函數(shù)get_chat_msg_result被連接起來。在返回來自于數(shù)據(jù)庫表中的聊天消息的同時,程序控制進(jìn)入到 get_chat_msg_result函數(shù)。

  1. // 
  2. // General Ajax Call 
  3. // 
  4.  
  5. var oxmlHttp; 
  6. var oxmlHttpSend; 
  7.  
  8. function get_chat_msg() 
  9.     if(typeof XMLHttpRequest != "undefined"
  10.     { 
  11.         oxmlHttp = new XMLHttpRequest(); 
  12.     } 
  13.     else if (window.ActiveXObject) 
  14.     { 
  15.        oxmlHttp = new ActiveXObject("Microsoft.XMLHttp"); 
  16.     } 
  17.     if(oxmlHttp == null
  18.     { 
  19.         alert("Browser does not support XML Http Request"); 
  20.        return
  21.     } 
  22.  
  23.     oxmlHttp.onreadystatechange = get_chat_msg_result; 
  24.     oxmlHttp.open("GET","chat_recv_ajax.php",true); 
  25.     oxmlHttp.send(null); 

在chat_recv_ajax.php程序中,來自于用戶的聊天消息會通過SQL select命令進(jìn)行收集。為了限制行數(shù),在SQL查詢中還給出了限制子句(limit 200),即要求聊天數(shù)據(jù)庫表中的***200行。所獲得的消息再返回給Ajax函數(shù),用于在聊天窗口中顯示內(nèi)容。

 

  1. require_once('dbconnect.php'); 
  2.  
  3. db_connect(); 
  4.  
  5. $sql = "SELECT *, date_format(chatdate,'%d-%m-%Y %r'
  6. as cdt from chat order by ID desc limit 200"; 
  7. $sql = "SELECT * FROM (" . $sql . ") as ch order by ID"
  8. $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); 
  9.  
  10. // Update Row Information 
  11. $msg=""
  12. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
  13.    $msg = $msg . "" . 
  14.         "" . 
  15.         ""
  16. $msg=$msg . "<table style="color: blue; font-family: verdana, arial; " . 
  17.   "font-size: 10pt;" border="0"
  18.   <tbody><tr><td>" . $line["cdt"] . 
  19.   " </td><td>" . $line["username"] . 
  20.   ": </td><td>" . $line["msg"] . 
  21.   "</td></tr></tbody></table>"
  22.  
  23. echo $msg; 
  24.  
  25. 數(shù)據(jù)準(zhǔn)備就緒的同時,JavaScript函數(shù)會收集來自于PHP接收到的數(shù)據(jù)。這些數(shù)據(jù)將被安排置于DIV標(biāo)簽內(nèi)。oxmlHttp.responseText會保留從PHP程序接收到的聊天消息,并復(fù)制到DIV標(biāo)簽的document.getElementById(“DIV_CHAT”).innerHTML屬性。 
  26.  
  27. function get_chat_msg_result(t) 
  28.     if(oxmlHttp.readyState==4 || oxmlHttp.readyState=="complete"
  29.     { 
  30.         if (document.getElementById("DIV_CHAT") != null
  31.         { 
  32.             document.getElementById("DIV_CHAT").innerHTML =  oxmlHttp.responseText; 
  33.             oxmlHttp = null
  34.         } 
  35.         var scrollDiv = document.getElementById("DIV_CHAT"); 
  36.         scrollDiv.scrollTop = scrollDiv.scrollHeight; 
  37.     } 

下面的SQL CREATE TABLE命令可用于創(chuàng)建名為chat的數(shù)據(jù)庫表。所有由用戶輸入的信息都會進(jìn)入到數(shù)據(jù)庫表中。

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

興趣點

這段用于實現(xiàn)聊天應(yīng)用程序的代碼非常有意思。它可以改進(jìn)成為一個完全成熟的HTTP聊天應(yīng)用程序。創(chuàng)建該應(yīng)用程序的邏輯也非常簡單。即使是初學(xué)者理解起來也不會有任何困難。

許可證

這篇文章,以及任何相關(guān)的源代碼和文件,都獲得了The Code Project Open License (CPOL)的許可。

譯文鏈接:http://www.codeceo.com/article/php-chart-app.html
英文原文:Chat Application in PHP

責(zé)任編輯:王雪燕 來源: 碼農(nóng)網(wǎng)
相關(guān)推薦

2011-12-15 11:11:51

JavaNIO

2021-11-16 09:38:10

鴻蒙HarmonyOS應(yīng)用

2023-02-10 08:16:48

WebSocket簡易聊天室

2022-07-26 14:53:10

WebSocket網(wǎng)絡(luò)通信協(xié)議

2023-01-13 00:02:41

2023-01-05 09:17:58

2015-08-06 17:17:33

swoole聊天室

2011-06-09 15:44:29

Spring

2022-11-14 08:01:48

2022-12-01 08:25:23

eTsTCP聊天室

2024-10-07 10:45:12

2022-04-18 10:36:48

社交軟件聊天平臺rocket.cha

2021-10-14 18:46:29

Websocket瀏覽器API

2021-11-15 11:32:12

Linux Linux sockeLinux 系統(tǒng)

2024-01-18 11:15:46

Pythonsocket聊天室

2021-02-06 23:26:25

聊天室開發(fā)WebSocket

2021-12-09 16:48:25

鴻蒙HarmonyOS應(yīng)用

2015-08-06 16:23:04

iosxmpp聊天

2013-11-27 10:46:31

JavaEEWebsockets
點贊
收藏

51CTO技術(shù)棧公眾號