Home  >  Article  >  Backend Development  >  简单的php+mysql聊天室实现方法(附源码)_php实例

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

WBOY
WBOYOriginal
2016-06-07 17:09:54619browse

本文实例讲述了简单的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');

二、框架页面如下:





简单的php+mysql聊天室--框架页

<body> </body>

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



简单的php+mysql聊天室--登陆页


<?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"]." 退出房间";}else{echo "请输入您的昵称";}?>

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



简单的php+mysql聊天室--发言页


发言表情: 简单的php+mysql聊天室实现方法(附源码)_php实例 简单的php+mysql聊天室实现方法(附源码)_php实例 简单的php+mysql聊天室实现方法(附源码)_php实例 简单的php+mysql聊天室实现方法(附源码)_php实例 简单的php+mysql聊天室实现方法(附源码)_php实例 简单的php+mysql聊天室实现方法(附源码)_php实例 简单的php+mysql聊天室实现方法(附源码)_php实例 

五、显示聊天室内容页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'"); }
?>


简单的php+mysql聊天室--显示留言页



<?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)){ ?>
昵称: <?php if($row[nick] == ""){echo "游客";}else{echo $row[nick];}?> .GIF" width="20" style="max-width:90%"> 发言内容: <?php echo $row[words];?> 发言时间: <?php echo $row[chtime];?>
<?php } ?>

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn