php网页版聊天软件实现代码
本文实例为大家分享了php匿名聊天室的具体实现代码,供大家参考,具体内容如下
1.index.html
<html> <head> <title>聊天室</title> <metacharset="utf-8"/> <linkhref="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css"rel="stylesheet"> <scriptsrc="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <scriptsrc="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> <linkhref="./css/style.css"rel="stylesheet"/> <script> varmaxid=0; functionshowmessage(){ //创建ajax对象 varxhr=newXMLHttpRequest(); //监听 xhr.onreadystatechange=function(){ if(xhr.readyState===4){ eval("varinfo="+xhr.responseText); vartext=""; for(vari=0;i<info.length;i++) { text+="<divclass='alertalert-success'>"+ "<spanclass='name'>"+info[i].send+":</span>"+ "<spanclass='message'>"+info[i].content+"</span>"+ "<span>("+info[i].time+")</span>"+ "</div>"; maxid=info[i].id; } varold=document.getElementById("msg").innerHTML; document.getElementById("msg").innerHTML=old+text; document.getElementById("msg").scrollTop=document.getElementById("msg").scrollHeight; } }; //初始化 xhr.open("get","./action.php?maxid="+maxid); //发送 xhr.send(); } $(document).ready( function() { showmessage() self.setInterval("showmessage()",2000); } ); functionsend(){ varpostData="content="+document.getElementById('content').value; varxhr=newXMLHttpRequest(); xhr.open("POST","./add.php",true); //一定要写头信息不然服务器接收不到 xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.onreadystatechange=function(){ varXMLHttpReq=xhr; if(XMLHttpReq.readyState==4){ if(XMLHttpReq.status==200){ document.getElementById('content').value=""; } } }; xhr.send(postData); } </script> </head> <body> <divclass="panelpanel-default"id="main"> <!--聊天室名字--> <divclass="panel-heading"> <h3class="panel-title"> 匿名者聊天室 </h3> </div> <!--聊天室名字--> <!--聊天室消息框--> <divclass="panel-body"> <divclass="wellno-bottom"> <!--消息框样式--> <!-- <divclass="alertalert-success">成功!很好地完成了提交。</div> <divclass="alertalert-info">信息!请注意这个信息。</div> <divclass="alertalert-warning">警告!请不要提交。</div> <divclass="alertalert-danger">错误!请进行一些更改。</div> --> <divid="msg"class="showmessage"> </div> <!--聊天室消息框--> <divclass="well"> <formrole="form"> <divclass="form-group"> <labelfor="name">发送消息</label> <textareaclass="form-control"id="content"name="content" style="resize:none;font-family:MicrosoftYaHei;"rows="3"> </textarea> </div> <divstyle="text-align:right"> <buttontype="button"class="btnbtn-primary"onclick="send()"> <spanclass="glyphiconglyphicon-envelope"></span> 发送 </button> </div> </form> </div> <!--聊天室发送框--> </div> </div> </div> </body> </html>
2.action.php
<?php $link=mysqli_connect('localhost','root','123','test'); mysqli_query($link,'setnamesutf8'); $info=array(); header("Content-type:text/html;charset=utf-8"); $id=$_GET['maxid']; $data=mysqli_query($link,"select*fromtalkwhereid>$id"); while($array=mysqli_fetch_assoc($data)){ $info[]=$array; }; echojson_encode($info);
3.chat.sql
DROPTABLEIFEXISTS`talk`; CREATETABLE`talk`( `id`int(10)unsignedNOTNULLAUTO_INCREMENTCOMMENT'id', `send`varchar(10)CHARACTERSETutf8DEFAULTNULLCOMMENT'发送者昵称', `ip`varchar(12)CHARACTERSETutf8DEFAULTNULL, `content`varchar(500)CHARACTERSETutf8DEFAULTNULL, `time`varchar(50)DEFAULTNULL, PRIMARYKEY(`id`) )ENGINE=InnoDBAUTO_INCREMENT=52DEFAULTCHARSET=utf8;
4.add.php
<?php /** *@功能将表单插入数据库 */ $content=$_POST['content']; print_r($_POST); $time=date("Y-m-dH:i:s",time(0)); $link=mysqli_connect('localhost','root','123','test'); mysqli_query($link,'setnamesutf8'); $ip=$_SERVER["REMOTE_ADDR"]; $sql="INSERTINTOtalkVALUES(NULL,'匿名者','$ip','$content','$time')"; $data=mysqli_query($link,$sql); echo"$content"; if($data) echo"1"; else echo"0";
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。