说点废话:今天lol的时候 碰到一个坑货,技术确实不错,到后面,被我说了一句,那家伙居然说让我求他,他就玩。我骂了他一句傻逼。我就挂机,看着他们输了。有时候,看到别人要坑,我就先下手为强!
思路:sqlite:数据库数据,用来保存聊天记录;php链接数据库,接受ajax传送数据,返回处理结果;ajax异步获取聊天数据,异步更新聊天记录;
所用的数据库类,请参照前面blog。
一.登录
<?php session_start(); ?> <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset='utf-8'> <link href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!--[if lt IE 9]> <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> <![endif]--> </head> <style> .main{ width: 500px; margin: 50px auto; } .info-list{ border: 1px solid #dedede; height: 200px; overflow-y: scroll; } .info-list li{ border: 0px; } </style> <body onload="cycle();"> <script> var xmlhttp; var a; //生成xmlhttp对象 function create(){ if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } } //回调方法 function callback(){ if(xmlhttp.readyState==4&&xmlhttp.status==200) { document.getElementById("info_list").innerHTML=xmlhttp.responseText;//获取php获取到的聊天记录 } } //启动 function run(){ create(); xmlhttp.open("GET","/message/first.php",true); xmlhttp.onreadystatechange=callback; xmlhttp.send(); } 每隔500毫秒,便更新一次表中数据。 function cycle(){ setInterval("run()",500); } </script> <div class="main"> <div class="panel panel-default"> <div class="panel-heading">登录</div> <div class="panel-body"> <form method="POST" action=""> <div class="form-group"> <label for="exampleInputEmail1">账号</label> <input type="text" name="username" class="form-control" id="exampleInputEmail1" placeholder="Enter email"> </div> <div class="form-group"> <label for="exampleInputPassword1">密码</label> <input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> </div> <input type="submit" name="submit" class="btn btn-default" value="登录"> <a type="button" class="btn btn-default" href="/message/register.php">注册</a> </form> </div> </div> </div> <?php if($_POST['submit']){//登录,并且获取session include '../sqlite/sqlite_db.php'; $db=new SqliteDb(); $res=$db->query('user',' where username="'.$_POST['username'].'" and password="'.$_POST['password'].'"'); if ($res) { foreach ($res as $key => $row) { $_SESSION['user']['username']=$row['username']; $_SESSION['user']['nickname']=$row['nickname']; } echo '<script>window.location.href="/message/";</script>'; } } ?>
<script src="my.js"></script> <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script> <script src="http://cdn.bootcss.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> </body> </html>
sqlite+php+ajax 即时聊天系统(ajax 长连接)一
原文地址:http://blog.csdn.net/youmypig/article/details/43653981