转载自:http://www.jb51.cc/article/p-ndyouxzi-g.html
思路:sqlite:数据库数据,用来保存聊天记录;PHP链接数据库,接受ajax传送数据,返回处理结果;ajax异步获取聊天数据,异步更新聊天记录;
所用的数据库类,请参照前面blog。
一.登录
- <?PHP@H_403_21@session_start();?>@H_403_21@
- <!DOCTYPEhtml>@H_403_21@
- <html@H_403_21@lang@H_403_21@="zh-cn">@H_403_21@
- 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@
- <!--HTML5shimandRespond.jsforIE8supportofHTML5elementsandmediaqueries-->@H_403_21@
- <!--[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@
- <![endif]-->@H_403_21@
- style>@H_403_21@
- .main{
- width:500px;
- margin:50pxauto;
- }
- .info-list{
- border:1pxsolid#dedede;
- height:200px;
- overflow-y:scroll;
- }
- .info-listli{
- border:0px;
- }
- body@H_403_21@onload@H_403_21@="cycle();">@H_403_21@
- varxmlhttp;
- vara;
- //生成xmlhttp对象
- functioncreate(){
- if(window.XMLHttpRequest)
- {
- xmlhttp@H_403_21@=new@H_403_21@XMLHttpRequest();
- }else{
- xmlhttp@H_403_21@=new@H_403_21@ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- //回调方法
- functioncallback(){
- if(xmlhttp.readyState@H_403_21@==4&&xmlhttp.status@H_403_21@==200)
- {
- document.getElementById("info_list").innerHTML@H_403_21@=xmlhttp@H_403_21@.responseText;//获取PHP获取到的聊天记录
- }
- }
- //启动
- functionrun(){
- create();
- xmlhttp.open("GET","/message/first.PHP",true);
- xmlhttp.onreadystatechange@H_403_21@=callback@H_403_21@;
- xmlhttp.send();
- }
- 每隔500毫秒,便更新一次表中数据。
- functioncycle(){
- setInterval("run()",500);
- }
- >@H_403_21@
- 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@
- PHP@H_403_21@
- if($_POST['submit']){//登录,并且获取session
- include'../sqlite/sqlite_db.PHP';
- $db@H_403_21@=new@H_403_21@sqliteDb();
- $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@');
- if($res){
- foreach($resas$key@H_403_21@=>@H_403_21@$row){
- $_SESSION['user']['username']=$row['username'];
- $_SESSION['user']['nickname']=$row['nickname'];
- }
- echo'>window.location.href@H_403_21@="/message/"@H_403_21@;>@H_403_21@';
- }
- }
- ?>@H_403_21@