简单的php+mysql聊天室实现方法(附源码)


Posted in PHP onJanuary 05, 2016

本文实例讲述了简单的php+mysql聊天室实现方法。分享给大家供大家参考,具体如下:

这里介绍的程序分为 8 个文件:

frameset框架页面:index.php

显示聊天室内容页:show.php

用户登陆页面:login.php

用户发言页面:speak.php

数据库配置文件:config.php

页面美化样式:style.css

数据库文件:chat.sql

发言表情包:face/

分别介绍如下:

一、数据库文件chat.sql如下:

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `chat`
-- ----------------------------
DROP TABLE IF EXISTS `chat`;
CREATE TABLE `chat` (
 `chtime` datetime default NULL,
 `nick` char(10) NOT NULL,
 `words` char(150) default NULL,
 `face` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
-- ----------------------------
-- Records of chat
-- ----------------------------
INSERT INTO chat VALUES ('2013-03-21 04:15:14', 'smiling', '测试显示发言', '3');
INSERT INTO chat VALUES ('2013-03-21 04:46:26', 'smiling', '时间有问题,', '5');
INSERT INTO chat VALUES ('2013-03-21 04:47:28', 'php新手', '新手来了。', '1');
INSERT INTO chat VALUES ('2013-03-21 04:55:19', 'php新手', '显示正确啦', '6');
INSERT INTO chat VALUES ('2013-03-21 17:12:47', 'php新手', '正确显示时间', '5');
INSERT INTO chat VALUES ('2013-03-21 17:23:19', 'php新手', '时间显示正确。', '7');
INSERT INTO chat VALUES ('2013-03-21 17:23:29', 'php新手', '哈哈', '1');
INSERT INTO chat VALUES ('2013-03-22 08:28:00', '', '今天再来看看。', '3');

二、框架页面如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>简单的php+mysql聊天室--框架页</title>
</head>
<frameset rows="*,80" cols="*" framespacing="0" bordercolor="#E1D1AE">
 <frameset rows="*" cols="*,284">
  <frame src="show.php" name="mainFrame"/>
  <frame src="login.php" name="rightFrame"/>
 </frameset>
 <frame src="speak.php" name="bottomFrame"/>
</frameset>
<noframes><body>
</body>
</noframes>
</html>

三、用户登陆页面login.php如下:

<html>
<head>
<title>简单的php+mysql聊天室--登陆页</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td> </td>
 </tr>
</table>
<table width="250" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#CBB486">
 <tr>
  <td height="30" align="center" bgcolor="#F5E6C1">
    <?php 
    if($_GET["tj"] == "out"){
    setcookie ("nick", "", time() - 3600);
    header("refresh:0; URL='login.php'");
    }
    if($_POST["submit"]){
    setcookie("nick",$nick); //用cookie记录用户昵称,也可以用SESSION
    header("refresh:0; URL='login.php'");
    }
    ?>
    <?php if($_COOKIE["nick"]){echo "欢迎您 ".$_COOKIE["nick"]." <a href=?tj=out>退出房间</a>";}else{echo "请输入您的昵称";}?></td>
 </tr>
 <tr>
  <td bgcolor="#F5E6C1">
<form action="" method="post">
<input type="text" name="nick" cols="20">
<input type="submit" name="submit" value="登录">
</form></td>
 </tr>
</table>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td> </td>
 </tr>
</table>
<table width="250" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#CBB486">
 <tr>
  <td height="70" bgcolor="#F5E6C1" class="login">程序说明:因本聊天室是作者仅花了一天时间而写的程序,所以仅适合新手练习研究,高手可以进行绕行,新手可以在本基础上进行增加发言IP和其它字段功能,最主要的是理解本程序的制作原理。欢迎新手朋友加入夏日源码交流群:<SPAN id="qid">101140934</SPAN></td>
 </tr>
</table>
</body>
</html>

四、用户发言页面speak.php如下:

<html>
<head>
<title>简单的php+mysql聊天室--发言页</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td height="2"></td>
 </tr>
</table>
<form action="show.php" target="mainFrame" method="post">
  发言表情:
<input type="radio" value="1" name="face" checked="checked" />
<img src="face/PIC1.GIF" width="20" height="20" border="0" />
<input type="radio" value="2" name="face" />
<img src="face/PIC2.GIF" width="20" height="20" border="0" />
<input type="radio" value="3" name="face" />
<img src="face/PIC3.GIF" width="20" height="20" border="0" />
<input type="radio" value="4" name="face" />
<img src="face/PIC4.GIF" width="20" height="20" border="0" />
<input type="radio" value="5" name="face" />
<img src="face/PIC5.GIF" width="20" height="20" border="0" />
<input type="radio" value="6" name="face" />
<img src="face/PIC6.GIF" width="20" height="20" border="0" />
<input type="radio" value="7" name="face" />
<img src="face/PIC7.GIF" width="20" height="20" border="0" /> 
<input type="text" name="words" cols="20">
<input type="submit" value="发言">
</form>
</body>
</html>

五、显示聊天室内容页show.php如下:

<?php require_once('config.php'); ?>
<?php
if($words){
$query="insert into chat(chtime,nick,words,face)values(now(),'$nick','$words','$face')";//插入SQL语句
mysql_query($query,$link_ID); //发送留言到数据库
header("refresh:0; URL='show.php'"); }
?>
<html>
<head>
<title>简单的php+mysql聊天室--显示留言页</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="refresh" content="5;url=show.php">
</head>
<body>
<?php 
    //最新发言显示在最下面
    $sql="select * from chat order by chtime asc";
    $result=mysql_query($sql);
    $total=mysql_num_rows($result);
    $info=($total/15-1)*15;
    if($total<15){
    $str="select * from chat order by chtime asc;" ; //查询字符串
    }else{
    $str="select * from chat order by chtime asc limit $info,15;" ; //查询字符串
    }
     $result=mysql_query($str,$link_ID); //送出查询
     while($row=mysql_fetch_array($result)){
?>
<table width="700" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#CBB486">
 <tr>
  <td width="33" align="left" bgcolor="#F5E6C1" class="font">昵称:</td>
  <td width="41" align="center" bgcolor="#F5E6C1" class="font"><?php if($row[nick] == ""){echo "游客";}else{echo $row[nick];}?></td>
  <td width="42" align="center" bgcolor="#F5E6C1" class="font"><img src="face/PIC<?php echo $row[face];?>.GIF" width="20" height="20"></td>
  <td width="56" align="left" bgcolor="#F5E6C1" class="font">发言内容:</td>
  <td width="160" align="left" bgcolor="#F5E6C1" class="font"><?php echo $row[words];?></td>
  <td width="56" align="left" bgcolor="#F5E6C1" class="font">发言时间:</td>
  <td width="244" align="left" bgcolor="#F5E6C1" class="font"><?php echo $row[chtime];?></td>
 </tr>
</table>
<table width="100" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td height="5"></td>
 </tr>
</table>
<?php } ?>
</body>
</html>

完整实例代码点击此处本站下载。

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP5.0正式发布 不完全兼容PHP4 新增多项功能
Oct 09 PHP
PHP与javascript的两种交互方式
Oct 09 PHP
深入解析php中的foreach函数
Aug 31 PHP
PHP 登录完成后如何跳转上一访问页面
Jan 14 PHP
使用Appcan客户端自动更新PHP版本号(全)
Jul 31 PHP
PHP+Mysql+jQuery实现发布微博程序 php篇
Oct 15 PHP
php中this关键字用法分析
Dec 07 PHP
php简单读取.vcf格式文件的方法示例
Sep 02 PHP
PHP实现的服务器一致性hash分布算法示例
Aug 09 PHP
微信推送功能实现方式图文详解
Jul 12 PHP
laravel5.1框架model类查询的实现方法
Oct 08 PHP
Thinkphp 在api开发中异常返回依然是html的解决方式
Oct 16 PHP
基于PHP实现简单的随机抽奖小程序
Jan 05 #PHP
详解WordPress开发中的get_post与get_posts函数使用
Jan 04 #PHP
使用PHP实现微信摇一摇周边红包
Jan 04 #PHP
解析WordPress中的post_class与get_post_class函数
Jan 04 #PHP
WordPress开发中的get_post_custom()函数使用解析
Jan 04 #PHP
在WordPress中安装使用视频播放器插件Hana Flv Player
Jan 04 #PHP
详解WordPress中分类函数wp_list_categories的使用
Jan 04 #PHP
You might like
我用php+mysql写的留言本
2006/10/09 PHP
php实现天干地支计算器示例
2014/03/14 PHP
取得单条网站评论以数组形式进行输出
2014/07/28 PHP
PHP语法自动检查的Vim插件
2014/08/11 PHP
thinkPHP中验证码的简单实现方法
2016/12/05 PHP
PHP文字转图片功能原理与实现方法分析
2017/08/31 PHP
php array 转json及java 转换 json数据格式操作示例
2019/11/13 PHP
jQuery 核心函数以及jQuery对象
2010/03/23 Javascript
JS 图片缩放效果代码
2010/06/09 Javascript
jquery 简单应用示例总结
2013/08/09 Javascript
兼容FF和IE的动态table示例自写
2013/10/21 Javascript
js动态添加删除,后台取数据(示例代码)
2013/11/25 Javascript
javascript放大镜效果的简单实现
2013/12/09 Javascript
js中函数调用的两种常用方法使用介绍
2014/07/17 Javascript
js匿名函数的调用示例(形式多种多样)
2014/08/20 Javascript
JavaScript将当前时间转换成UTC标准时间的方法
2015/04/06 Javascript
基于jquery实现在线选座订座之影院篇
2015/08/24 Javascript
JavaScript学习笔记(三):JavaScript也有入口Main函数
2015/09/12 Javascript
layui当点击文本框时弹出选择框,显示选择内容的例子
2019/09/02 Javascript
VUE项目初建和常见问题总结
2019/09/12 Javascript
antd-mobile ListView长列表的数据更新遇到的坑
2020/04/08 Javascript
js实现QQ邮箱邮件拖拽删除功能
2020/08/27 Javascript
在vue中使用inheritAttrs实现组件的扩展性介绍
2020/12/07 Vue.js
[03:49]辉夜杯现场龙骑士COSER秀情商“我喜欢芬队!”
2015/12/27 DOTA
Python文本相似性计算之编辑距离详解
2016/11/28 Python
使用Python的package机制如何简化utils包设计详解
2017/12/11 Python
dpn网络的pytorch实现方式
2020/01/14 Python
python调用HEG工具批量处理MODIS数据的方法及注意事项
2020/02/18 Python
pytorch 多分类问题,计算百分比操作
2020/07/09 Python
解决tensorflow模型压缩的问题_踩坑无数,总算搞定
2021/03/02 Python
全球度假村:Club Med
2017/11/27 全球购物
Ray-Ban雷朋奥地利官网:全球领先的太阳眼镜品牌
2020/10/12 全球购物
德国玩具商店:Planet Happy DE
2021/01/16 全球购物
考生诚信考试承诺书
2014/05/23 职场文书
学生党员检讨书范文
2014/12/27 职场文书
小程序wx.getUserProfile接口的具体使用
2021/06/02 Javascript