溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用php+mysql實現(xiàn)聊天室功能

發(fā)布時間:2021-08-09 17:58:09 來源:億速云 閱讀:175 作者:chen 欄目:開發(fā)技術

這篇文章主要講解了“怎么用php+mysql實現(xiàn)聊天室功能”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用php+mysql實現(xiàn)聊天室功能”吧!

本文實例講述了簡單的php+mysql聊天室實現(xiàn)方法。分享給大家供大家參考,具體如下:

這里介紹的程序分為 8 個文件:

frameset框架頁面:index.php

顯示聊天室內(nèi)容頁:show.php

用戶登陸頁面:login.php

用戶發(fā)言頁面:speak.php

數(shù)據(jù)庫配置文件:config.php

頁面美化樣式:style.css

數(shù)據(jù)庫文件:chat.sql

發(fā)言表情包:face/

分別介紹如下:

一、數(shù)據(jù)庫文件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', '測試顯示發(fā)言', '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>&nbsp;</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 "歡迎您&nbsp;".$_COOKIE["nick"]."&nbsp;<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>&nbsp;</td>
 </tr>
</table>
<table width="250" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#CBB486">
 <tr>
  <td height="70" bgcolor="#F5E6C1" class="login">程序說明:因本聊天室是作者僅花了一天時間而寫的程序,所以僅適合新手練習研究,高手可以進行繞行,新手可以在本基礎上進行增加發(fā)言IP和其它字段功能,最主要的是理解本程序的制作原理。歡迎新手朋友加入夏日源碼交流群:<SPAN id="qid">101140934</SPAN></td>
 </tr>
</table>
</body>
</html>

四、用戶發(fā)言頁面speak.php如下:

<html>
<head>
<title>簡單的php+mysql聊天室--發(fā)言頁</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">
&nbsp;&nbsp;發(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)容頁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); //發(fā)送留言到數(shù)據(jù)庫
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 
    //最新發(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ā)言時間:</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+mysql實現(xiàn)聊天室功能”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對怎么用php+mysql實現(xiàn)聊天室功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI