首页 后端开发 php教程 简单的php+mysql聊天室实现方法(附源码)_PHP

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

May 28, 2016 am 11:49 AM
mysql php 聊天室

本文实例讲述了简单的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">
    <&#63;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'");
    }
    &#63;>
    <&#63;php if($_COOKIE["nick"]){echo "欢迎您 ".$_COOKIE["nick"]." <a href=&#63;tj=out>退出房间</a>";}else{echo "请输入您的昵称";}&#63;></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="/static/imghw/default1.png"  data-src="face/PIC1.GIF"  class="lazy"    style="max-width:90%"  style="max-width:90%" border="0" / alt="简单的php+mysql聊天室实现方法(附源码)_PHP" >
<input type="radio" value="2" name="face" />
<img  src="/static/imghw/default1.png"  data-src="face/PIC2.GIF"  class="lazy"    style="max-width:90%"  style="max-width:90%" border="0" / alt="简单的php+mysql聊天室实现方法(附源码)_PHP" >
<input type="radio" value="3" name="face" />
<img  src="/static/imghw/default1.png"  data-src="face/PIC3.GIF"  class="lazy"    style="max-width:90%"  style="max-width:90%" border="0" / alt="简单的php+mysql聊天室实现方法(附源码)_PHP" >
<input type="radio" value="4" name="face" />
<img  src="/static/imghw/default1.png"  data-src="face/PIC4.GIF"  class="lazy"    style="max-width:90%"  style="max-width:90%" border="0" / alt="简单的php+mysql聊天室实现方法(附源码)_PHP" >
<input type="radio" value="5" name="face" />
<img  src="/static/imghw/default1.png"  data-src="face/PIC5.GIF"  class="lazy"    style="max-width:90%"  style="max-width:90%" border="0" / alt="简单的php+mysql聊天室实现方法(附源码)_PHP" >
<input type="radio" value="6" name="face" />
<img  src="/static/imghw/default1.png"  data-src="face/PIC6.GIF"  class="lazy"    style="max-width:90%"  style="max-width:90%" border="0" / alt="简单的php+mysql聊天室实现方法(附源码)_PHP" >
<input type="radio" value="7" name="face" />
<img  src="/static/imghw/default1.png"  data-src="face/PIC7.GIF"  class="lazy"    style="max-width:90%"  style="max-width:90%" border="0" / alt="简单的php+mysql聊天室实现方法(附源码)_PHP" > 
<input type="text" name="words" cols="20">
<input type="submit" value="发言">
</form>
</body>
</html>

登录后复制

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

<&#63;php require_once('config.php'); &#63;>
<&#63;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'"); }
&#63;>
<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>
<&#63;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)){
&#63;>
<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"><&#63;php if($row[nick] == ""){echo "游客";}else{echo $row[nick];}&#63;></td>
  <td width="42" align="center" bgcolor="#F5E6C1" class="font"><img  src="face/PIC<&#63;php echo $row[face];&#63; alt="简单的php+mysql聊天室实现方法(附源码)_PHP" >.GIF" width="20"    style="max-width:90%"></td>
  <td width="56" align="left" bgcolor="#F5E6C1" class="font">发言内容:</td>
  <td width="160" align="left" bgcolor="#F5E6C1" class="font"><&#63;php echo $row[words];&#63;></td>
  <td width="56" align="left" bgcolor="#F5E6C1" class="font">发言时间:</td>
  <td width="244" align="left" bgcolor="#F5E6C1" class="font"><&#63;php echo $row[chtime];&#63;></td>
 </tr>
</table>
<table width="100" border="0" align="center" cellpadding="0" cellspacing="0">
 <tr>
  <td height="5"></td>
 </tr>
</table>
<&#63;php } &#63;>
</body>
</html>

登录后复制

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

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

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1666
14
CakePHP 教程
1425
52
Laravel 教程
1327
25
PHP教程
1273
29
C# 教程
1252
24
在MySQL中解释外键的目的。 在MySQL中解释外键的目的。 Apr 25, 2025 am 12:17 AM

在MySQL中,外键的作用是建立表与表之间的关系,确保数据的一致性和完整性。外键通过引用完整性检查和级联操作维护数据的有效性,使用时需注意性能优化和避免常见错误。

比较和对比Mysql和Mariadb。 比较和对比Mysql和Mariadb。 Apr 26, 2025 am 12:08 AM

MySQL和MariaDB的主要区别在于性能、功能和许可证:1.MySQL由Oracle开发,MariaDB是其分支。2.MariaDB在高负载环境中性能可能更好。3.MariaDB提供了更多的存储引擎和功能。4.MySQL采用双重许可证,MariaDB完全开源。选择时应考虑现有基础设施、性能需求、功能需求和许可证成本。

如果session_start()被多次调用会发生什么? 如果session_start()被多次调用会发生什么? Apr 25, 2025 am 12:06 AM

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

MySQL:数据库,PHPMYADMIN:管理接口 MySQL:数据库,PHPMYADMIN:管理接口 Apr 29, 2025 am 12:44 AM

MySQL和phpMyAdmin可以通过以下步骤进行有效管理:1.创建和删除数据库:在phpMyAdmin中点击几下即可完成。2.管理表:可以创建表、修改结构、添加索引。3.数据操作:支持插入、更新、删除数据和执行SQL查询。4.导入导出数据:支持SQL、CSV、XML等格式。5.优化和监控:使用OPTIMIZETABLE命令优化表,并利用查询分析器和监控工具解决性能问题。

作曲家:通过AI的帮助开发PHP 作曲家:通过AI的帮助开发PHP Apr 29, 2025 am 12:27 AM

AI可以帮助优化Composer的使用,具体方法包括:1.依赖管理优化:AI分析依赖关系,建议最佳版本组合,减少冲突。2.自动化代码生成:AI生成符合最佳实践的composer.json文件。3.代码质量提升:AI检测潜在问题,提供优化建议,提高代码质量。这些方法通过机器学习和自然语言处理技术实现,帮助开发者提高效率和代码质量。

给MySQL表添加和删除字段的操作步骤 给MySQL表添加和删除字段的操作步骤 Apr 29, 2025 pm 04:15 PM

在MySQL中,添加字段使用ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column,删除字段使用ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop。添加字段时,需指定位置以优化查询性能和数据结构;删除字段前需确认操作不可逆;使用在线DDL、备份数据、测试环境和低负载时间段修改表结构是性能优化和最佳实践。

session_start()函数的意义是什么? session_start()函数的意义是什么? May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

怎样卸载MySQL并清理残留文件 怎样卸载MySQL并清理残留文件 Apr 29, 2025 pm 04:03 PM

要安全、彻底地卸载MySQL并清理所有残留文件,需遵循以下步骤:1.停止MySQL服务;2.卸载MySQL软件包;3.清理配置文件和数据目录;4.验证卸载是否彻底。

See all articles