转载自:http://www.jb51.cc/article/p-ndyouxzi-g.html
思路:sqlite:数据库数据,用来保存聊天记录;PHP链接数据库,接受ajax传送数据,返回处理结果;ajax异步获取聊天数据,异步更新聊天记录;
所用的数据库类,请参照前面blog。
一.登录
- <?PHPsession_start();?>
- <!DOCTYPEhtml>
- <htmllang="zh-cn">
- headMetacharset='utf-8'linkhref="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.min.css"rel="stylesheet">
- <!--HTML5shimandRespond.jsforIE8supportofHTML5elementsandmediaqueries-->
- <!--[ifltIE9]scriptsrc="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></scriptscriptsrc="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js">
- <![endif]-->
- style>
- .main{
- width:500px;
- margin:50pxauto;
- }
- .info-list{
- border:1pxsolid#dedede;
- height:200px;
- overflow-y:scroll;
- }
- .info-listli{
- border:0px;
- }
- bodyonload="cycle();">
- varxmlhttp;
- vara;
- //生成xmlhttp对象
- functioncreate(){
- if(window.XMLHttpRequest)
- {
- xmlhttp=newXMLHttpRequest();
- }else{
- xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
- }
- }
- //回调方法
- functioncallback(){
- if(xmlhttp.readyState==4&&xmlhttp.status==200)
- {
- document.getElementById("info_list").innerHTML=xmlhttp.responseText;//获取PHP获取到的聊天记录
- }
- }
- //启动
- functionrun(){
- create();
- xmlhttp.open("GET","/message/first.PHP",true);
- xmlhttp.onreadystatechange=callback;
- xmlhttp.send();
- }
- 每隔500毫秒,便更新一次表中数据。
- functioncycle(){
- setInterval("run()",500);
- }
- >
- divclass="main"divclass="panelpanel-default"divclass="panel-heading">登录divdivclass="panel-body"formmethod="POST"action=""divclass="form-group"labelfor="exampleInputEmail1">账号labelinputtype="text"name="username"class="form-control"id="exampleInputEmail1"placeholder="Enteremail"labelfor="exampleInputPassword1">密码inputtype="password"name="password"class="form-control"id="exampleInputPassword1"placeholder="Password"inputtype="submit"name="submit"class="btnbtn-default"value="登录"atype="button"class="btnbtn-default"href="/message/register.PHP">注册aform>
- PHP
- if($_POST['submit']){//登录,并且获取session
- include'../sqlite/sqlite_db.PHP';
- $db=newsqliteDb();
- $res=$db->query('user','whereusername="'.$_POST['username'].'"andpassword="'.$_POST['password'].'"');
- if($res){
- foreach($resas$key=>$row){
- $_SESSION['user']['username']=$row['username'];
- $_SESSION['user']['nickname']=$row['nickname'];
- }
- echo'>window.location.href="/message/";>';
- }
- }
- ?>
scriptsrc="my.js"scriptsrc="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"scriptsrc="http://cdn.bootcss.com/bootstrap/3.3.2/js/bootstrap.min.js"bodyhtml>
原文链接:https://www.f2er.com/sqlite/199868.html