簡(jiǎn)單的php+mysql聊天室實(shí)現(xiàn)方法(附源碼)
本文實(shí)例講述了簡(jiǎn)單的php+mysql聊天室實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這里介紹的程序分為 8 個(gè)文件:
frameset框架頁(yè)面:index.php
顯示聊天室內(nèi)容頁(yè):show.php
用戶登陸頁(yè)面:login.php
用戶發(fā)言頁(yè)面:speak.php
數(shù)據(jù)庫(kù)配置文件:config.php
頁(yè)面美化樣式:style.css
數(shù)據(jù)庫(kù)文件:chat.sql
發(fā)言表情包:face/
分別介紹如下:
一、數(shù)據(jù)庫(kù)文件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', '測(cè)試顯示發(fā)言', '3');
INSERT INTO chat VALUES ('2013-03-21 04:46:26', 'smiling', '時(shí)間有問(wèn)題,', '5');
INSERT INTO chat VALUES ('2013-03-21 04:47:28', 'php新手', '新手來(lái)了。', '1');
INSERT INTO chat VALUES ('2013-03-21 04:55:19', 'php新手', '顯示正確啦', '6');
INSERT INTO chat VALUES ('2013-03-21 17:12:47', 'php新手', '正確顯示時(shí)間', '5');
INSERT INTO chat VALUES ('2013-03-21 17:23:19', 'php新手', '時(shí)間顯示正確。', '7');
INSERT INTO chat VALUES ('2013-03-21 17:23:29', 'php新手', '哈哈', '1');
INSERT INTO chat VALUES ('2013-03-22 08:28:00', '', '今天再來(lái)看看。', '3');
二、框架頁(yè)面如下:
<!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>簡(jiǎn)單的php+mysql聊天室--框架頁(yè)</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>
三、用戶登陸頁(yè)面login.php如下:
<html>
<head>
<title>簡(jiǎn)單的php+mysql聊天室--登陸頁(yè)</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 "請(qǐng)輸入您的昵稱";}?></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">程序說(shuō)明:因本聊天室是作者僅花了一天時(shí)間而寫的程序,所以僅適合新手練習(xí)研究,高手可以進(jìn)行繞行,新手可以在本基礎(chǔ)上進(jìn)行增加發(fā)言IP和其它字段功能,最主要的是理解本程序的制作原理。歡迎新手朋友加入夏日源碼交流群:<SPAN id="qid">101140934</SPAN></td>
</tr>
</table>
</body>
</html>
四、用戶發(fā)言頁(yè)面speak.php如下:
<html> <head> <title>簡(jiǎn)單的php+mysql聊天室--發(fā)言頁(yè)</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"> 發(fā)言表情: <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="發(fā)言"> </form> </body> </html>
五、顯示聊天室內(nèi)容頁(yè)show.php如下:
<?php require_once('config.php'); ?>
<?php
if($words){
$query="insert into chat(chtime,nick,words,face)values(now(),'$nick','$words','$face')";//插入SQL語(yǔ)句
mysql_query($query,$link_ID); //發(fā)送留言到數(shù)據(jù)庫(kù)
header("refresh:0; URL='show.php'"); }
?>
<html>
<head>
<title>簡(jiǎn)單的php+mysql聊天室--顯示留言頁(yè)</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="refresh" content="5;url=show.php">
</head>
<body>
<?php
//最新發(fā)言顯示在最下面
$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">發(fā)言內(nèi)容:</td>
<td width="160" align="left" bgcolor="#F5E6C1" class="font"><?php echo $row[words];?></td>
<td width="56" align="left" bgcolor="#F5E6C1" class="font">發(fā)言時(shí)間:</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>
完整實(shí)例代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP框架實(shí)現(xiàn)WebSocket在線聊天通訊系統(tǒng)
- ThinkPHP5.0框架結(jié)合Swoole開(kāi)發(fā)實(shí)現(xiàn)WebSocket在線聊天案例詳解
- Ajax PHP JavaScript MySQL實(shí)現(xiàn)簡(jiǎn)易無(wú)刷新在線聊天室
- PHP+swoole實(shí)現(xiàn)簡(jiǎn)單多人在線聊天群發(fā)
- 基于javascript、ajax、memcache和PHP實(shí)現(xiàn)的簡(jiǎn)易在線聊天室
- PHP聊天室簡(jiǎn)單實(shí)現(xiàn)方法詳解
- 基于Swoole實(shí)現(xiàn)PHP與websocket聊天室
- 值得分享的php+ajax實(shí)時(shí)聊天室
- php實(shí)現(xiàn)簡(jiǎn)易聊天室應(yīng)用代碼
- php+html5基于websocket實(shí)現(xiàn)聊天室的方法
- 基于PHP實(shí)現(xiàn)一個(gè)簡(jiǎn)單的在線聊天功能
相關(guān)文章
CI操作cookie的方法分析(基于helper類庫(kù))
這篇文章主要介紹了CI操作cookie的方法,結(jié)合實(shí)例形式分析了CI使用helper類庫(kù)的cookie類庫(kù)實(shí)現(xiàn)操作cookie的相關(guān)技巧,需要的朋友可以參考下2016-03-03
PHP實(shí)現(xiàn)用戶認(rèn)證及管理完全源碼
PHP實(shí)現(xiàn)用戶認(rèn)證及管理完全源碼...2007-03-03
PHP代碼實(shí)現(xiàn)爬蟲(chóng)記錄——超管用
這篇文章主要通過(guò)創(chuàng)建crawler數(shù)據(jù)庫(kù),使用robot.php記錄來(lái)訪的爬蟲(chóng)信息從而將信息插入數(shù)據(jù)庫(kù),從而使用php代碼實(shí)現(xiàn)爬蟲(chóng)記錄,有需要的小伙可以來(lái)參考下。2015-07-07
Laravel5.1 框架模型創(chuàng)建與使用方法實(shí)例分析
這篇文章主要介紹了Laravel5.1 框架模型創(chuàng)建與使用方法,結(jié)合實(shí)例形式分析了laravel5.1框架模型的原理、創(chuàng)建、更新、獲取等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
destoon二次開(kāi)發(fā)模板及調(diào)用語(yǔ)法匯總
這篇文章主要介紹了destoon二次開(kāi)發(fā)模板及調(diào)用語(yǔ)法,需要的朋友可以參考下2014-06-06
php計(jì)算程序運(yùn)行時(shí)間的簡(jiǎn)單例子分享
這篇文章主要介紹了php計(jì)算程序運(yùn)行時(shí)間的簡(jiǎn)單例子分享,需要的朋友可以參考下2014-05-05
PHP將URL轉(zhuǎn)換成短網(wǎng)址的算法分享
短網(wǎng)址(Short URL)顧名思義就是在形式上比較短的網(wǎng)址。在Web 2.0的今天,不得不說(shuō)這是一個(gè)潮流。目前已經(jīng)有許多類似服務(wù),借助短網(wǎng)址您可以用簡(jiǎn)短的網(wǎng)址替代原來(lái)冗長(zhǎng)的網(wǎng)址,讓使用者可以更容易的分享鏈接,下面來(lái)看看如何用PHP實(shí)現(xiàn)這個(gè)功能,有需要的朋友們可以參考。2016-09-09
Zend Framework自定義Helper類相關(guān)注意事項(xiàng)總結(jié)
這篇文章主要介紹了Zend Framework自定義Helper類相關(guān)注意事項(xiàng),總結(jié)分析了編寫自定義Helper類的相關(guān)原則與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03

