简单的php+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"> <?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 "请输入您的昵称";}?></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如下:
<?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'"); } ?> <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 //最新发言显示在最下面 $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];? 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"><?php echo $row[words];?></td> <td width="56" align="left" bgcolor="#F5E6C1" class="font">发言时间:</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程序设计有所帮助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MySQL和phpMyAdmin是強大的數據庫管理工具。 1)MySQL用於創建數據庫和表、執行DML和SQL查詢。 2)phpMyAdmin提供直觀界面進行數據庫管理、表結構管理、數據操作和用戶權限管理。

在MySQL中,外鍵的作用是建立表與表之間的關係,確保數據的一致性和完整性。外鍵通過引用完整性檢查和級聯操作維護數據的有效性,使用時需注意性能優化和避免常見錯誤。

IIS和PHP可以兼容,通過FastCGI實現。 1.IIS通過配置文件將.php文件請求轉發給FastCGI模塊。 2.FastCGI模塊啟動PHP進程處理請求,提高性能和穩定性。 3.實際應用中需注意配置細節、錯誤調試和性能優化。

MySQL和MariaDB的主要區別在於性能、功能和許可證:1.MySQL由Oracle開發,MariaDB是其分支。 2.MariaDB在高負載環境中性能可能更好。 3.MariaDB提供了更多的存儲引擎和功能。 4.MySQL採用雙重許可證,MariaDB完全開源。選擇時應考慮現有基礎設施、性能需求、功能需求和許可證成本。

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

SQL是一種用於管理關係數據庫的標準語言,而MySQL是一個使用SQL的數據庫管理系統。 SQL定義了與數據庫交互的方式,包括CRUD操作,而MySQL實現了SQL標準並提供了額外的功能,如存儲過程和触發器。

AI可以幫助優化Composer的使用,具體方法包括:1.依賴管理優化:AI分析依賴關係,建議最佳版本組合,減少衝突。 2.自動化代碼生成:AI生成符合最佳實踐的composer.json文件。 3.代碼質量提升:AI檢測潛在問題,提供優化建議,提高代碼質量。這些方法通過機器學習和自然語言處理技術實現,幫助開發者提高效率和代碼質量。

MySQL和phpMyAdmin可以通過以下步驟進行有效管理:1.創建和刪除數據庫:在phpMyAdmin中點擊幾下即可完成。 2.管理表:可以創建表、修改結構、添加索引。 3.數據操作:支持插入、更新、刪除數據和執行SQL查詢。 4.導入導出數據:支持SQL、CSV、XML等格式。 5.優化和監控:使用OPTIMIZETABLE命令優化表,並利用查詢分析器和監控工具解決性能問題。
