PHP在线聊天室
来源:互联网 发布:windows查看tcp连接数 编辑:程序博客网 时间:2024/05/01 18:28
数据库链接文件:conn.php
<?php
$conn=mysql_connect('localhost','root','123456') or die(mysql_error());
mysql_select_db('dev_chat',$conn) or die(mysql_error());
mysql_query('set names \'utf8\'');
?>
显示页面:index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body bgcolor="yellow">
<h1>PHP在线聊天室</h1>
<!--<textarea rows="10" cols="60" name="one" id="one"></textarea>-->
<iframe src="msgs.php" width="600"height="400" scrolling="no"> </iframe>
<br/>
<br/>
<form method="post" action="write.php"target="five">
你的昵称:<input type="text" name="two"id="two" value="Guest">
你的内容:<input type="text" name="three"id="three">
<input type="submit" id="four" value="发送">
</form>
<iframe width="0"height="0"frameborder="0"name="five"> </iframe>
</body>
</html>
将聊天内容写入数据为文件:write.php
<?php
include("conn.php");
if($_POST['three'] && $_POST['two'])
{$msg = $_POST['three'];
$name = $_POST['two'];
$dateline = date('Y-m-d H:i:s');
$sql='INSERT INTO `msgs` (`id` ,`msg` ,`name` ,`dateline`)VALUES (NULL , \''.$msg.'\', \''.$name.'\', \''.$dateline.'\')'or die(mysql_error());
mysql_query($sql);
print_r($_POST);
}
?>
从数据库中查询聊天内容并输出页面:msgs.php
<?php
include("conn.php");
$sql = "SELECT * FROM `msgs` ORDER BY id DESC LIMIT 10 ";
$query = mysql_query($sql);
$rows=array();
while($row = mysql_fetch_array($query,MYSQL_ASSOC)){
$rows[]=$row;
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;URL=msgs.php" />
<?php
echo"<font color=red>系统消息</font>:欢迎光临PHP在线聊天系统<br><br>";
$rows = array_reverse($rows);
foreach($rows as $k=>$row){
echo '<font color=red>'.$row['name'].'</font>',':',$row['msg'],':','<font color=bule>'.$row['dateline'].'</font>',"\r\n<br>";
}
echo date('Y-m-d H:i:s');
?>
_________________________________________________________
_________________________________________________________
另外用ajax写显示首页内容为:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script>
var request;
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if (!request)
alert("Error initializing XMLHttpRequest!");
}
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
document.getElementById("msgs").innerHTML = response;
} else
alert("status is " + request.status);
}
}
function update_msgs(){
var url = 'msgs2.php';
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
setTimeout('update_msgs()',1000);
}
</script>
</head>
<body>
<h1>PHP在线聊天系统(AJAX更新消息内容)</h1>
<div id="msgs">
</div>
<br />
<br />
<form action="write.php" method="post" name="text" target="write_frame" >
<!--<p> <textarea cols="80" rows="10" id="msgs"></textarea></p>-->
你的昵称:<input type="text" id="name" name="name" value="Guest">
说话内容:<input type="text" id="msg" name="msg">
<input type="submit" value="发送">
</form>
<iframe src="" name="write_frame" id="write_frame" width="0" height="0" frameborder="0"></iframe>
<script>
createRequest();
setTimeout('update_msgs()',1000);
//setInterval
</script>
</body>
</html>
- PHP在线聊天室
- php基于websocket实现的在线聊天室
- php+Ajax+Mysql 创建简单的一对一在线聊天室
- 【php】基于Xajax的在线聊天室、直播间
- Ajax PHP JavaScript MySQL实现简易无刷新在线聊天室
- php聊天室
- 在线文字聊天室技术
- ajax在线聊天室(一)
- ajax在线聊天室(二)
- ajax在线聊天室(三)
- ajax在线聊天室(四)
- Socket.io在线聊天室
- asp.net 在线聊天室
- Socket.io在线聊天室
- Ajax实现在线聊天室
- 在线聊天室小程序
- 搭建简易在线聊天室
- Socket.io在线聊天室
- 内核中随处可见的等待队列详解1
- 为什么要使用NoSQL
- Linux Wget使用
- 一点一滴培养你的领袖气质
- 论 NoSQL 的数据模型
- PHP在线聊天室
- QQ聊天记录备份BAK文件的修复方法
- xcode4.2 ARC错误处理
- 交互式图形图像软件
- 转 Python3中文教程
- DLNA的介绍
- 内核中随处可见的等待队列详解2
- 内核中随处可见的等待队列详解3
- 【搜索】核反应