简单的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 相关文章推荐
一个简易需要注册的留言版程序
Oct 09 PHP
用PHP生成静态HTML速度快类库
Mar 18 PHP
Wordpress 相册插件 NextGEN-Gallery 添加目录将中文转为拼音的解决办法
Dec 29 PHP
php+js实现异步图片上传实例分享
Jun 02 PHP
php使用cookie保存用户登录的用户名实例
Jan 26 PHP
php实现在服务器端调整图片大小的方法
Jun 16 PHP
5款适合PHP使用的HTML编辑器推荐
Jul 03 PHP
浅谈php+phpStorm+xdebug配置方法
Sep 17 PHP
php实现爬取和分析知乎用户数据
Jan 26 PHP
php封装一个异常的处理类
Jun 08 PHP
基于ThinkPHP5框架使用QueryList爬取并存入mysql数据库操作示例
May 25 PHP
使用Rancher在K8S上部署高性能PHP应用程序的教程
Jul 10 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 获取远程文件内容的函数代码
2010/03/24 PHP
php 计划任务 检测用户连接状态
2012/03/29 PHP
PHP实现的MongoDB数据库操作类分享
2014/05/12 PHP
PHP程序员的技术成长规划
2016/03/25 PHP
apache php mysql开发环境安装教程
2016/07/28 PHP
PHP Callable强制指定回调类型的方法
2016/08/30 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
2017/11/14 PHP
laravel-admin 中列表筛选方法
2019/10/03 PHP
javascript检测页面是否缩放的小例子
2013/05/16 Javascript
js判断变量初始化的三种形式及推荐用的形式
2014/07/22 Javascript
javascript实现相同事件名称,不同命名空间的调用方法
2015/06/26 Javascript
jQuery获取字符串中出现最多的数
2016/02/22 Javascript
javascript动态获取登录时间和在线时长
2016/02/25 Javascript
react性能优化达到最大化的方法 immutable.js使用的必要性
2017/03/09 Javascript
详解如何构建一个Angular6的第三方npm包
2018/09/07 Javascript
vue+iview动态渲染表格详解
2019/03/19 Javascript
基于node+vue实现简单的WebSocket聊天功能
2020/02/01 Javascript
基于原生JS封装的Modal对话框插件的示例代码
2020/09/09 Javascript
在vue项目中封装echarts的步骤
2020/12/25 Vue.js
[02:20]DOTA2英雄基础教程 黑暗贤者
2013/12/19 DOTA
Python使用matplotlib绘制动画的方法
2015/05/20 Python
python中的for循环
2018/09/28 Python
python查看模块安装位置的方法
2018/10/16 Python
详解用Python实现自动化监控远程服务器
2019/05/18 Python
python爬虫开发之使用Python爬虫库requests多线程抓取猫眼电影TOP100实例
2020/03/10 Python
python将unicode和str互相转化的实现
2020/05/11 Python
django 数据库返回queryset实现封装为字典
2020/05/19 Python
Python list和str互转的实现示例
2020/11/16 Python
CSS3之2D与3D变换的实现方法
2019/01/28 HTML / CSS
优秀辅导员事迹材料
2014/02/16 职场文书
师范毕业生求职信
2014/07/11 职场文书
学校领导班子四风对照检查材料
2014/09/27 职场文书
会议开幕词
2015/01/28 职场文书
试用期工作表现自我评价
2015/03/06 职场文书
2015年社区环境卫生工作总结
2015/04/21 职场文书
律政俏佳人观后感
2015/06/09 职场文书