简单的php+mysql聊天室实现方法(附源码)
更新时间:2016年1月7日 13:52 点击:1791
本文实例讲述了简单的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程序设计有所帮助。
上一篇: weiphp微信公众平台授权设置
下一篇: Yii中表单用法实例详解
相关文章
- 这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
- 这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
- PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
- index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04- 这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
- 这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
- 这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31- 这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
- 今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
- 这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
- 这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
- 这篇文章主要为大家详细介绍了linux下源码安装mysql5.6.20教程的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-15
- 经常看到有人踩在了PHP路径的坑上面了,感觉有必要来说说PHP中相对路径的一些坑,以及PHP中绝对路径的使用,下面一起来看看。 ...2016-08-24
- 这篇文章主要介绍了thinkPHP中多维数组的遍历方法,以简单实例形式分析了thinkPHP中foreach语句的使用技巧,需要的朋友可以参考下...2016-01-12
- 这篇文章主要介绍了PHP正则表达式过滤html标签属性的相关内容,实用性非常,感兴趣的朋友参考下吧...2016-05-06
- 这篇文章主要为大家详细介绍了php构造方法中析构方法在继承中的表现,感兴趣的小伙伴们可以参考一下...2016-04-15
- 这篇文章主要介绍了jQuery+PHP+MySQL实现无限级联效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-02-21
通过node-mysql搭建Windows+Node.js+MySQL环境的教程
这篇文章主要介绍了通过node-mysql搭建Windows+Node.js+MySQL环境的教程,node-mysql是JavaScript编写的一个Node的MySQL驱动,需要的朋友可以参考下...2016-03-03