简单的php+mysql聊天室实现方法(附源码)
本文实例讲述了简单的php+mysql聊天室实现方法。分享给大家供大家参考,具体如下:
这里介绍的程序分为8个文件:
frameset框架页面:index.php
显示聊天室内容页:show.php
用户登陆页面:login.php
用户发言页面:speak.php
数据库配置文件:config.php
页面美化样式:style.css
数据库文件:chat.sql
发言表情包:face/
分别介绍如下:
一、数据库文件chat.sql如下:
SETFOREIGN_KEY_CHECKS=0;
------------------------------
--Tablestructurefor`chat`
------------------------------
DROPTABLEIFEXISTS`chat`;
CREATETABLE`chat`(
`chtime`datetimedefaultNULL,
`nick`char(10)NOTNULL,
`words`char(150)defaultNULL,
`face`int(11)defaultNULL
)ENGINE=InnoDBDEFAULTCHARSET=gb2312;
------------------------------
--Recordsofchat
------------------------------
INSERTINTOchatVALUES('2013-03-2104:15:14','smiling','测试显示发言','3');
INSERTINTOchatVALUES('2013-03-2104:46:26','smiling','时间有问题,','5');
INSERTINTOchatVALUES('2013-03-2104:47:28','php新手','新手来了。','1');
INSERTINTOchatVALUES('2013-03-2104:55:19','php新手','显示正确啦','6');
INSERTINTOchatVALUES('2013-03-2117:12:47','php新手','正确显示时间','5');
INSERTINTOchatVALUES('2013-03-2117:23:19','php新手','时间显示正确。','7');
INSERTINTOchatVALUES('2013-03-2117:23:29','php新手','哈哈','1');
INSERTINTOchatVALUES('2013-03-2208:28:00','','今天再来看看。','3');
二、框架页面如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Frameset//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/> <title>简单的php+mysql聊天室--框架页</title> </head> <framesetrows="*,80"cols="*"framespacing="0"bordercolor="#E1D1AE"> <framesetrows="*"cols="*,284"> <framesrc="show.php"name="mainFrame"/> <framesrc="login.php"name="rightFrame"/> </frameset> <framesrc="speak.php"name="bottomFrame"/> </frameset> <noframes><body> </body> </noframes> </html>
三、用户登陆页面login.php如下:
<html>
<head>
<title>简单的php+mysql聊天室--登陆页</title>
<linkhref="style.css"rel="stylesheet"type="text/css"/>
</head>
<body>
<tablewidth="80%"border="0"cellspacing="0"cellpadding="0">
<tr>
<td> </td>
</tr>
</table>
<tablewidth="250"border="0"align="center"cellpadding="5"cellspacing="1"bgcolor="#CBB486">
<tr>
<tdheight="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'");
}
?>
<?phpif($_COOKIE["nick"]){echo"欢迎您 ".$_COOKIE["nick"]." <ahref=?tj=out>退出房间</a>";}else{echo"请输入您的昵称";}?></td>
</tr>
<tr>
<tdbgcolor="#F5E6C1">
<formaction=""method="post">
<inputtype="text"name="nick"cols="20">
<inputtype="submit"name="submit"value="登录">
</form></td>
</tr>
</table>
<tablewidth="80%"border="0"cellspacing="0"cellpadding="0">
<tr>
<td> </td>
</tr>
</table>
<tablewidth="250"border="0"align="center"cellpadding="5"cellspacing="1"bgcolor="#CBB486">
<tr>
<tdheight="70"bgcolor="#F5E6C1"class="login">程序说明:因本聊天室是作者仅花了一天时间而写的程序,所以仅适合新手练习研究,高手可以进行绕行,新手可以在本基础上进行增加发言IP和其它字段功能,最主要的是理解本程序的制作原理。欢迎新手朋友加入夏日源码交流群:<SPANid="qid">101140934</SPAN></td>
</tr>
</table>
</body>
</html>
四、用户发言页面speak.php如下:
<html> <head> <title>简单的php+mysql聊天室--发言页</title> <linkhref="style.css"rel="stylesheet"type="text/css"/> </head> <body> <tablewidth="80%"border="0"cellspacing="0"cellpadding="0"> <tr> <tdheight="2"></td> </tr> </table> <formaction="show.php"target="mainFrame"method="post"> 发言表情: <inputtype="radio"value="1"name="face"checked="checked"/> <imgsrc="face/PIC1.GIF"width="20"height="20"border="0"/> <inputtype="radio"value="2"name="face"/> <imgsrc="face/PIC2.GIF"width="20"height="20"border="0"/> <inputtype="radio"value="3"name="face"/> <imgsrc="face/PIC3.GIF"width="20"height="20"border="0"/> <inputtype="radio"value="4"name="face"/> <imgsrc="face/PIC4.GIF"width="20"height="20"border="0"/> <inputtype="radio"value="5"name="face"/> <imgsrc="face/PIC5.GIF"width="20"height="20"border="0"/> <inputtype="radio"value="6"name="face"/> <imgsrc="face/PIC6.GIF"width="20"height="20"border="0"/> <inputtype="radio"value="7"name="face"/> <imgsrc="face/PIC7.GIF"width="20"height="20"border="0"/> <inputtype="text"name="words"cols="20"> <inputtype="submit"value="发言"> </form> </body> </html>
五、显示聊天室内容页show.php如下:
<?phprequire_once('config.php');?>
<?php
if($words){
$query="insertintochat(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>
<linkhref="style.css"rel="stylesheet"type="text/css"/>
<metahttp-equiv="refresh"content="5;url=show.php">
</head>
<body>
<?php
//最新发言显示在最下面
$sql="select*fromchatorderbychtimeasc";
$result=mysql_query($sql);
$total=mysql_num_rows($result);
$info=($total/15-1)*15;
if($total<15){
$str="select*fromchatorderbychtimeasc;";//查询字符串
}else{
$str="select*fromchatorderbychtimeasclimit$info,15;";//查询字符串
}
$result=mysql_query($str,$link_ID);//送出查询
while($row=mysql_fetch_array($result)){
?>
<tablewidth="700"border="0"align="center"cellpadding="5"cellspacing="1"bgcolor="#CBB486">
<tr>
<tdwidth="33"align="left"bgcolor="#F5E6C1"class="font">昵称:</td>
<tdwidth="41"align="center"bgcolor="#F5E6C1"class="font"><?phpif($row[nick]==""){echo"游客";}else{echo$row[nick];}?></td>
<tdwidth="42"align="center"bgcolor="#F5E6C1"class="font"><imgsrc="face/PIC<?phpecho$row[face];?>.GIF"width="20"height="20"></td>
<tdwidth="56"align="left"bgcolor="#F5E6C1"class="font">发言内容:</td>
<tdwidth="160"align="left"bgcolor="#F5E6C1"class="font"><?phpecho$row[words];?></td>
<tdwidth="56"align="left"bgcolor="#F5E6C1"class="font">发言时间:</td>
<tdwidth="244"align="left"bgcolor="#F5E6C1"class="font"><?phpecho$row[chtime];?></td>
</tr>
</table>
<tablewidth="100"border="0"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdheight="5"></td>
</tr>
</table>
<?php}?>
</body>
</html>
完整实例代码点击此处本站下载。
希望本文所述对大家PHP程序设计有所帮助。