首页 > 作文

php实现简单聊天功能

更新时间:2023-04-08 13:57:57 阅读: 评论:0

1、创建聊天消息表,其表的字段有消息内容,发送时间和发送者的名称;

sql:

create table `guanhui`.`message` (     `id` i名人名事nt(10) not null auto_increment comment '消息id' ,     `content` varchar(255) not null comment '消息内容' ,     `nder` varchar(60) not null comment '发送者' ,     `nd_time` int(10) not null comment '发送时间' ,     primary key (`id`) ) engine = myisam;

2、创建php文件将聊天消息查询出来,并以json格式输出;

$con = mysql_connect("localhost","","");if (!$con) {    die('could not connect: ' . mysql_error());}mysql_lect_db("chat", $con);mysql_query("t names 'utf8'");            $sql = "lect * from  `message`";$result = mysql_query($sql);if(ist($result))雮{    json_encode(array(        'code' => 0,        'msg' => 'ok',       'data' => mysql_fetch_array($result, mysql_assoc)    ))} el {    json_encode(array(        'code' => 0,        'msg' => '聊天信息为空!'    ))}

3、在前端使用ajax轮询请求php文件,并将消息展示出来。

tinterval(function(){     $.get('/get_message_list.php', function(data){         $(data).each(function(value, item){            $('#message_list').append('来自:' + data.专科考研究生nder + '的消息:' + data.message + '发送时间:', data.nd_time);        });     }); }, 600);

4、创建nd_message.php用来接收发送的消息,并将数据储存到数据库。

$nd_time = date('y-m-d h:i:s',time());;$nd = $_post['nder'];$content = $_post['content'];$con = mysql_connect("localhost","","");if (!$con) {  die('could not connect: ' . mysql_error());}mysql_lect_db("ing", $con);mysql_query("t names 'utf8'");$inrt="inrt into  `chat` (`id` ,`nder` ,`content` ,`nd_time`) values (null ,  '$nder',  '$content',  '$nd_time')";$result = mysql_query($inrt);

5、在前端获取要发送的消息,并请求nd_message.php。

function ndcontent()  {    var xmlhttp;    if (window.xmlhttprequest)    {// code for ie7+, firefox, chrome, opera, safari      xmlhttp=new xmlhttprequest();    }    el    {// code for ie6, ie5      xmlhttp=new activexobject("microsoft.xmlhttp");    }    店面平面图xmlhttp.onreadystatechange=function() {      if (xmlhttp.readystate==4 && xmlhttp.status==200)      // 服务端返回了正确数据,开始响应处理      {           document.getelementbyid("input").innerhtml="";      }     }    xmlhttp.open("post","/nd_m巴黎圣母院赏析essage.php",true);    var f=document.chat;    var content = f.content.value;    var nder = f.nder.value;    //发送请求    //这里使用post方法传递参数;    //将要构造的参数连接起来,接收的时候:$_post['nd'];    var post_str= "content="+ content+"&nder="+nder;    //使用post的时候必须在发送请求之前加上下面这句    xmlhttp.trequestheader("content-type","application/x-www-form-urlencoded");    xmlhttp.nd(post_str);  }

更多学习内容请访问:

腾讯t3-t4标准精品php架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)

本文发布于:2023-04-08 13:57:55,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/ab12e328c03218f903fcbb9a21e2e22c.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php实现简单聊天功能.doc

本文 PDF 下载地址:php实现简单聊天功能.pdf

标签:消息   并将   时间   内容
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图