ajax 实现一个网页版山寨QQ
思路:用户登录后,可以点击好友列表对好友进行聊天,那么所发送的数据都会保存到数据库,而聊天窗口每隔一定时间向数据库索要数据(条件是 where getter='$getter' and sender='$sender' and isGet=0" )
该程序包含以下几个页面
login.php //登录页面
fiendList.php //登录好友页面
chatRoom.php //聊天室页面,要发送数据和接收数据都要通过ajax调用相应的controller页面,下面两个就是了
sendMessageController //处理发送过来的数据
getMessageController //返回相应的聊天数据
MessageService.php //用于服务上面的两个控制器 (就是model)
下面是源码
//====================login.php====================
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<h1>欢迎登录聊天室</h1>
<form action="LoginController.php" method="post">
用户名:<input type="text" name="username" /><br/><br/>7
密 码:<input type="text" name="passwd" /><br />
<input type="submit" value="登录" /><br />
</form>
</html>
//==================fridendList.php=======================
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function openChatRoom(obj){
window.open("chatRoom.php?username="+obj.innerText,"_blank");
}
</script>
</head>
<body>
<h1>好友列表</h1>
<ul>
<li onclick="openChatRoom(this)">songjiang</li>
<li onclick="openChatRoom(this)">zhangfei</li>
<li onclick="openChatRoom(this)">xiaoqian</li>
<li onclick="openChatRoom(this)">linzhouzhi</li>
</ul>
</body>
</html>
//=========================chatRoom.php===============
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php
$username=$_GET['username'];
//取出session中登陆名
session_start();
$loginUser = $_SESSION['loginuser'];
?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function getXmlHttpObject(){
var xmlHttpRequest;
//不同的浏览器获取对象xmlhttprequest 对象方法不一样
if(window.ActiveXObject){
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest=new XMLHttpRequest();
}
return xmlHttpRequest;
}
var myXmlHttpRequest=null;
//没歌两秒调用getmessage()
window.setInterval("getMessage()",5000);
function getMessage(){
myXmlHttpRequest = getXmlHttpObject();
if(myXmlHttpRequest){
var url="getMessageController.php";
var data = "getter=<?php echo $loginUser;?>&sender=<?php echo $username?>";
myXmlHttpRequest.open("post",url,true);
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myXmlHttpRequest.onreadystatechange=function(){
if(myXmlHttpRequest.readyState==4){
if(myXmlHttpRequest.status==200){
var mesRes = myXmlHttpRequest.responseXML;
//取出content内容和时间
var cons = mesRes.getElementsByTagName("con");
if(cons.length!=0){
//有多少条记录就显示多少条记录
for(var i=0;i<cons.length;i++){
$('mycons').value += "<?php echo $username;?> -->><?php echo $loginUser;?> say:"+cons[i].childNodes[0].nodeValue+"\r\n ";
}
}
}
}
}
myXmlHttpRequest.send(data);
}
}
function sendMessage(){
myXmlHttpRequest = getXmlHttpObject();
if(myXmlHttpRequest){
var url = "sendMessageController.php";
//js中如何使用php数据
var data = "con="+$('con').value+"& getter=<?php echo $username; ?>& sender=<?php echo $loginUser; ?>";
myXmlHttpRequest.open("post",url,true);
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myXmlHttpRequest.onreadystatechange=function(){
if(myXmlHttpRequest.readyState==4){
if(myXmlHttpRequest.status==200){
//这里要返回信息,这里我们不需要
}
}
}
myXmlHttpRequest.send(data);
$('mycons').value += "you --><?php echo $username;?> say: "+$('con').value + new Date()+"\r\n";
}
}
function $(id){
return document.getElementById(id);
}
</script>
</head>
<center>
<h1>You(<?php echo $loginUser;?>with <font color='red'><?php echo $username; ?></font>chatting..)</h1>
<textarea cols="40" rows="20" id="mycons" value=""></textarea><br/>
<input type="text" style="width:200px" id="con" />
<input type="button" value="sendmessage" onclick="sendMessage()" />
</center>
</html>
//======================sendMessageController.php============================
<?php
require_once 'MessageService.class.php';
$sender = $_POST['sender'];
$getter = $_POST['getter'];
$con = $_POST['con'];
//file_put_contents("mylog.log",$sender."__".$getter."__".$con."\r\n",FILE_APPEND);
$messageService = new MessageService();
if($messageService->addMessage($sender, $getter, $con)==0){
echo "you operate is faile";
}else{
echo "you operate is ok";
}
//========================getMessageController.php==========================
<?php
//这里两句话很重要,第一讲话告诉浏览器返回的数据是xml格式
header("Content-Type: text/xml;charset=utf-8");
//告诉浏览器不要缓存数据
header("Cache-Control: no-cache");
require_once 'MessageService.class.php';
$getter = $_POST['getter'];
$sender= $_PO
补充:Web开发 , php ,
- 更多php疑问解答:
- wordpress问题<?php if(have_posts()) : ?>
- 建设一个搜索类网站php还是jsp,数据库那个好
- 我是一个学PHP的,我现在已经学会了PHP,HTML,CSS,JS,JQ,AJAX,XML,thinkPHP,smarty模板
- 没理由啊 php代码无法执行,貌似有语法错误。。。
- 关于PHP 和API 的一段代码不懂啊不懂,请高手指点! 这是淘宝API的
- php语言中,序列化到底在那里使用?它的优势是什么?劣势是什么?
- PHP函数等于或等于应该怎么表达
- 请教php高手,解决basename函数和mb_substr函数处理中文文件名称的解决方法,在上传文件时,总是出现乱码
- .NET,PHP,JAVA,JS优秀点分别是?
- 织梦cms 在环境监测的时候 wamp5 gd不支持 是为什么。;extension=php_gd2.dll这一句我删除了还是不显示?
- 我想学PHP。1.应安装什么编程工具? 2. 装LIUNX系统是装简易的还是?什么版本的?3.还应安装什么?
- <?php 和 <? 有什么区别
- PHP网页如何接收按钮的值?请问
- 买来书的代码运行好是错误。。帮帮忙啊。php
- 求,一个php代码,用来修改 mysql数据库中的商品名字的大小写