sqlite+php+ajax 即时聊天系统(ajax 长连接)一

前端之家收集整理的这篇文章主要介绍了sqlite+php+ajax 即时聊天系统(ajax 长连接)一前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载自:http://www.jb51.cc/article/p-ndyouxzi-g.html

思路:sqlite:数据库数据,用来保存聊天记录;PHP链接数据库,接受ajax传送数据,返回处理结果;ajax异步获取聊天数据,异步更新聊天记录;

所用的数据库类,请参照前面blog。


一.登录

  1. <?PHP@H_403_21@session_start();?>@H_403_21@
  2. <!DOCTYPEhtml>@H_403_21@
  3. <html@H_403_21@lang@H_403_21@="zh-cn">@H_403_21@
  4. headMeta@H_403_21@charset@H_403_21@='utf-8'link@H_403_21@href@H_403_21@="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.min.css"@H_403_21@rel@H_403_21@="stylesheet">@H_403_21@
  5. <!--HTML5shimandRespond.jsforIE8supportofHTML5elementsandmediaqueries-->@H_403_21@
  6. <!--[ifltIE9]script@H_403_21@src@H_403_21@="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></scriptscript@H_403_21@src@H_403_21@="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js">@H_403_21@
  7. <![endif]-->@H_403_21@
  8. style>@H_403_21@
  9. .main{
  10. width:500px;
  11. margin:50pxauto;
  12. }
  13. .info-list{
  14. border:1pxsolid#dedede;
  15. height:200px;
  16. overflow-y:scroll;
  17. }
  18. .info-listli{
  19. border:0px;
  20. }
  21. body@H_403_21@onload@H_403_21@="cycle();">@H_403_21@
  22. varxmlhttp;
  23. vara;
  24. //生成xmlhttp对象
  25. functioncreate(){
  26. if(window.XMLHttpRequest)
  27. {
  28. xmlhttp@H_403_21@=new@H_403_21@XMLHttpRequest();
  29. }else{
  30. xmlhttp@H_403_21@=new@H_403_21@ActiveXObject("Microsoft.XMLHTTP");
  31. }
  32. }
  33. //回调方法
  34. functioncallback(){
  35. if(xmlhttp.readyState@H_403_21@==4&&xmlhttp.status@H_403_21@==200)
  36. {
  37. document.getElementById("info_list").innerHTML@H_403_21@=xmlhttp@H_403_21@.responseText;//获取PHP获取到的聊天记录
  38. }
  39. }
  40. //启动
  41. functionrun(){
  42. create();
  43. xmlhttp.open("GET","/message/first.PHP",true);
  44. xmlhttp.onreadystatechange@H_403_21@=callback@H_403_21@;
  45. xmlhttp.send();
  46. }
  47. 每隔500毫秒,便更新一次表中数据。
  48. functioncycle(){
  49. setInterval("run()",500);
  50. }
  51. >@H_403_21@
  52. div@H_403_21@class@H_403_21@="main"div@H_403_21@class@H_403_21@="panelpanel-default"div@H_403_21@class@H_403_21@="panel-heading">@H_403_21@登录divdiv@H_403_21@class@H_403_21@="panel-body"form@H_403_21@method@H_403_21@="POST"@H_403_21@action@H_403_21@=""div@H_403_21@class@H_403_21@="form-group"label@H_403_21@for@H_403_21@="exampleInputEmail1">@H_403_21@账号labelinput@H_403_21@type@H_403_21@="text"@H_403_21@name@H_403_21@="username"@H_403_21@class@H_403_21@="form-control"@H_403_21@id@H_403_21@="exampleInputEmail1"@H_403_21@placeholder@H_403_21@="Enteremail"label@H_403_21@for@H_403_21@="exampleInputPassword1">@H_403_21@密码input@H_403_21@type@H_403_21@="password"@H_403_21@name@H_403_21@="password"@H_403_21@class@H_403_21@="form-control"@H_403_21@id@H_403_21@="exampleInputPassword1"@H_403_21@placeholder@H_403_21@="Password"input@H_403_21@type@H_403_21@="submit"@H_403_21@name@H_403_21@="submit"@H_403_21@class@H_403_21@="btnbtn-default"@H_403_21@value@H_403_21@="登录"a@H_403_21@type@H_403_21@="button"@H_403_21@class@H_403_21@="btnbtn-default"@H_403_21@href@H_403_21@="/message/register.PHP">@H_403_21@注册aform>@H_403_21@
  53. PHP@H_403_21@
  54. if($_POST['submit']){//登录,并且获取session
  55. include'../sqlite/sqlite_db.PHP';
  56. $db@H_403_21@=new@H_403_21@sqliteDb();
  57. $res@H_403_21@=$db->@H_403_21@query('user','whereusername@H_403_21@="'.$_POST['username'].'"@H_403_21@andpassword@H_403_21@="'.$_POST['password'].'"@H_403_21@');
  58. if($res){
  59. foreach($resas$key@H_403_21@=>@H_403_21@$row){
  60. $_SESSION['user']['username']=$row['username'];
  61. $_SESSION['user']['nickname']=$row['nickname'];
  62. }
  63. echo'>window.location.href@H_403_21@="/message/"@H_403_21@;>@H_403_21@';
  64. }
  65. }
  66. ?>@H_403_21@
  • script@H_403_21@src@H_403_21@="my.js"script@H_403_21@src@H_403_21@="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"script@H_403_21@src@H_403_21@="http://cdn.bootcss.com/bootstrap/3.3.2/js/bootstrap.min.js"bodyhtml>
    原文链接:https://www.f2er.com/sqlite/199868.html

    猜你在找的Sqlite相关文章