Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php to realize chat room

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to use php to achieve chat room ideas, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Php to achieve chat room ideas are: 1, create tables; 2, set up "connect.php" to connect to the database; 3, create a user chat interface file "client.php"; 4, query whether there is a customer service reply; 5, set up a customer service chat page "server.php"; 6, send information to the user.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

The idea of realizing chat Room with php

A detailed explanation of the simple implementation method of PHP chat room

This paper gives an example to describe the simple implementation method of PHP chat room. Share with you for your reference, the details are as follows:

User = > customer service (first put the information into the database, then continuously query the data from the database and send it to the customer service through the ob+ persistent connection)

Customer service = > user (first receives the user's information, then stores the reply message, and finally requests the data continuously through ajax polling, which is displayed to the user chat interface)

[note:] if all the pages are set up, first link the customer service chat page (server.php), and then link the user page (client.php)

Illustrated with the following drawings:

Step 1: build a table

Description: rec: receiving message, sender: sender, content: sending content, is_new: as tag, 1 is new message, 2 is read message (default is 1)

CREATE TABLE `chat_ log` (`log_ id` int (11) NOT NULL AUTO_INCREMENT, `rec` varchar (10) NOT NULL COMMENT 'recipient', `sender` varchar (10) NOT NULL COMMENT 'sender', `content` text NOT NULL COMMENT 'send content', `is_ new` tinyint (4) NOT NULL DEFAULT'1' COMMENT 'Information 1 New message 0 read message, PRIMARY KEY (`Read`, `rec`) ENGINE=MyISAM AUTO_INCREMENT=105 DEFAULT CHARSET=utf8 COMMENT=' customer Service chat polling form'

Step 2: link the database: connect.php

$link = mysql_connect ('localhost',' root',''); mysql_query ("set names utf8"); mysql_select_db ("chat")

Step 3: user chat interface: client.php

User window # user {width: 440px; height: 300px; border: 1px solid blue;} $(function () {$("# btn") .click (function () {var content = $("textarea"). Val (); if (content = =') {alert ('send content cannot be empty'); return } / / send to customer service $.post ("toServer.php", {'msg':content}, function (res) {var obj = JSON.parse (res); $("# user") .append ("you send to customer service:" + obj + "

"); $(" textarea ") .val (");});}) / / use ajax polling to obtain whether the customer service has sent a message to the user var polling = {"url": 'fromServer.php', "dataType":' json', success: function (res) {/ / the data returned by the ajax request var obj = res / / append to the User chat page $("# user") .append ("customer service reply:" + obj.content + ")

"); $.ajax (polling);}}; $.ajax (polling); / / polling sends ajax request}) chat window with customer service

Send..

Fourth: users send messages to storage + ajax polling to inquire whether there is a customer service reply message.

ToServer.php

Require ('connect.php'); $msg = htmlspecialchars ($_ POST [' msg'], ENT_QUOTES); $sql = "INSERT INTO `chat_ log` (rec, sender, content) VALUES ('admin',' user','$msg')"; mysql_query ($sql, $link); echo json_encode ($msg)

FromServer.php

Require ('connect.php'); set_time_limit (0); / never time out while (true) {$sql = "SELECT * FROM `chat_ log` WHERE rec='user' AND is_new=1 ORDER BY log_id DESC LIMIT 1"; $res = mysql_query ($sql,$link); if ($row = mysql_fetch_assoc ($res)) {$sql = "UPDATE `chat_ log` SET is_new=0 WHERE log_id=". $row [' log_id']; mysql_query ($sql,$link) Die (json_encode ($row));}}

Step 5: customer service chat page server.php

Customer Service window # server {width: 440px; height: 300px; border: 1px solid blue;} function showMsg (res) {var obj = eval (res); $("# server") .append ("User sends you:" + obj.content + ") } / / reply to User messages $(function () {$("# btn") .click (function () {var content = $("textarea"). Val (); / / messages sent by customer service are stored in the database $.post via toClient.php ("toClient.php", {'msg':content}, function (res) {var obj = JSON.parse (res)) $("# server"). Append ("you send to User:" + obj+ "

"); $(" textarea ") .val (";})});}) chat window with User

Send..

Step 6: customer service query whether the user sends information to the database + sends information to the user

FromClient.php

Require ('connect.php'); ob_start (); / / Open an output buffer so that all output information is no longer sent directly to the browser, but is saved in the output buffer echo str_repeat (', 4096); ob_end_flush (); / / sends the internal buffer to the browser, deletes the buffer contents, and closes the buffer ob_flush () / / send the contents of the internal buffer to the browser, delete the contents of the buffer, and do not close the buffer set_time_limit (0); / / never time out while (true) {$sql = "select * from `chat_ log` where rec= 'admin' and is_new= 1 ORDER BY log_id DESC LIMIT 1"; $res = mysql_query ($sql, $link) If ($row = mysql_fetch_assoc ($res)) {$sql = "UPDATE `chat_ log` SET is_new=0 where log_id=". $row ['log_id']; mysql_query ($sql, $link); echo "parent.showMsg (" .json _ encode ($row). "); ob_flush (); flush () / / output all the contents released by ob_flush and those not in the PHP buffer to the browser Flush the contents of the internal buffer and output sleep (1);}}

ToClient.php

Require ('connect.php'); $msg = htmlspecialchars ($_ POST [' msg'], ENT_QUOTES); if (! empty ($msg)) {$sql = "insert into chat_log (rec, sender, content) values ('user',' admin','$msg')"; mysql_query ($sql); echo json_encode ($msg);} the above is how to use php to implement chat room ideas. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report