现要只是实现了聊天室的基本聊天,ajax无刷新显示功能,会员功能 等正在制作中。
下载地址:
数据库sql:
- CREATE TABLE `message` (
- `mid` int(20) NOT NULL auto_increment,
- `msg` varchar(255) NOT NULL,
- `user` varchar(50) NOT NULL,
- `time` int(10) NOT NULL,
- PRIMARY KEY (`mid`),
- KEY `user` (`user`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=161 ;
- CREATE TABLE `session` (
- `id` varchar(32) NOT NULL,
- `time` int(10) NOT NULL,
- `timenow` int(10) NOT NULL,
- `data` text NOT NULL,
- `ip` varchar(15) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
现在程序开源,其它的像字体,字色,会员,在线列表等功能将陆续完成。
主要的三个页面的代码
1、 index.php
- <?php
- //程序设计:℃冻番茄 QQ:7279915 E-mail:web@ye55.com www.phpd.cn
- require_once("config.php");
- ?>
- <!DOCTYPE htm PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <HEAD>
- <TITLE> chat </TITLE>
- <META NAME="Generator" CONTENT="EditPlus">
- <META NAME="Author" CONTENT="">
- <META NAME="Keywords" CONTENT="">
- <META NAME="Description" CONTENT="">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link href="css/global.css" rel="stylesheet" type="text/css" />
- <SCRIPT LANGUAGE="JavaScript" src="js/jquery.js"></SCRIPT>
- </HEAD>
- <BODY>
- <div id="main">
- <div id="main_left" style="float:none;margin:0px auto;">
- <div id="chat_m" style="width:500px;height:500px;border:#cccccc 1px solid;overflow:auto;">Loading…</div>
- <div id="main_l_from">
- <FORM METHOD=POST ACTION="javascript:void(0)">
- 昵称:<INPUT TYPE="text" NAME="user" id="user" size="8" value="<?=$_SESSION[‘user’]?>"> 内容:
- <INPUT TYPE="text" NAME="message" id="message" style="width:250px;"> <INPUT TYPE="submit" name="sub" value="发言" id="sub">
- </div></FORM>
- </div>
- <!–<div id="main_right">
- <h2>在线会员</h2>
- </div>—>
- <SCRIPT LANGUAGE="JavaScript">
- <!–
- $("#chat_m").load("chat.php");
- $("#sub").click(function(){
- if($("#user").val()==”){
- alert("昵称不能为空!");
- }else if($("#message").val()==”){
- alert("聊天内容不能为空!");
- }else{
- $.post("ajax.php?act=send",{user:$("#user").val(),msg:$("#message").val()},function(data){
- if(data.msg===true){
- showmsg();
- $("#message").val("");
- }else{
- alert(data.msg);
- }
- },"json")
- }
- })
- function sc()
- {
- var e=document.getElementById("chat_m")
- ee.scrollTop=e.scrollHeight;
- }
- //var s=setInterval("sc()",200)
- //–>
- </SCRIPT>
- <hr size=1>
- <center>程序设计:℃冻番茄 QQ:7279915 E-mail:web@ye55.com <a href="http://www.phpd.cn">www.phpd.cn</a> </center>
2、chat.php
- <?php
- require_once("config.php");
- //程序设计:℃冻番茄 QQ:7279915 E-mail:web@ye55.com www.phpd.cn
- $fist=$db->getfirst("select `mid` from `message` order by `mid` desc ");
- $mid=$fist[‘mid’]-40; //初始化聊天记录条数
- $mid=$mid<=0?0:$mid;
- $mid=0;
- ?>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <div id="contents"></div>
- <INPUT TYPE="text" style="display:none" NAME="mid" id="hide_mid" value="<?=$mid?>">
- <div id="end" style="width:400px;display:none;clear:both;"></div>
- <SCRIPT LANGUAGE="JavaScript">
- <!–
- //alert($("#hide_mid").val());
- function showmsg(){
- mids=$("#hide_mid").val();
- $.post("ajax.php?act=display",
- {mid:mids},
- function(data){
- //alert(data.list[2].user);
- $("#hide_mid").val(data.mid);
- if(data.list!=0){
- for(i=0;i<data.list.length;i++){
- $("#contents").append("<br />"+data.list[i].user+"说: "+data.list[i].msg+" ["+data.list[i].time1+"]");
- sc();
- }
- }
- },"json");
- }
- function scrollWindow() {scroll(0, 100000); }
- showmsg();
- setInterval("showmsg()", 4000);
- //–>
- </SCRIPT>
3、ajax.php
- <?php
- require_once("config.php");
- ////程序设计:℃冻番茄 QQ:7279915 E-mail:web@ye55.com www.phpd.cn
- //发信息
- if($_GET[‘act’]==‘send’){
- $msg=$_POST[‘msg’];
- $user=$_POST[‘user’];
- if(emptyempty($msg) || emptyempty($user)){
- $return[‘msg’]=‘聊天内容或昵称不能为空’;
- }else{
- $time=time();
- if($db->insert("insert into `message` (`msg`,`user`,`time`) values (‘$msg’,’$user’,’$time’)")){
- $return[‘msg’]=true;
- }else{
- $return[‘msg’]="err";
- }
- }
- echo json_encode($return);
- }elseif($_GET[‘act’]==‘display’){
- if(isset($_POST[‘mid’])){
- $s=" where `mid`>’".$_POST[‘mid‘]."’ ";
- }else{
- $s=”;
- }
- $sql="select * from `message` $s order by `mid`";
- $total=$db->getcount($sql);
- if($total==0){
- $a=0;
- }else{
- $result=$db->query($sql);
- while($row=$db->getarray($result)){
- $row[‘time1’]=date(‘Y-m-d H:i:s’,$row[‘time’]);
- $a[]=$row;
- $b=$row[‘mid’];
- }
- }
- $ccc=array(‘list’=>$a,‘mid’=>$b);
- echo json_encode($ccc);
- }
- ?>