Home > Backend Development > PHP Tutorial > Simple php mysql chat room implementation method (with source code), mysql chat room_PHP tutorial

Simple php mysql chat room implementation method (with source code), mysql chat room_PHP tutorial

WBOY
Release: 2016-07-12 09:01:38
Original
831 people have browsed it

Simple php mysql chat room implementation method (with source code), mysql chat room

The example in this article describes the simple php mysql chat room implementation method. Share it with everyone for your reference, the details are as follows:

The program introduced here is divided into 8 files:

frameset frame page:index.php

Show chat room content page: show.php

User login page: login.php

User speaking page: speak.php

Database configuration file: config.php

Page beautification style: style.css

Database file: chat.sql

Speech emoticon package: face/

The respective introductions are as follows:

1. The database file chat.sql is as follows:

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');

Copy after login

2. The frame page is as follows:

<!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>

Copy after login

3. The user login page login.php is as follows:

<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>

Copy after login

4. The user speech page speak.php is as follows:

<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="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="发言">
</form>
</body>
</html>

Copy after login

5. Display the chat room content page show.php as follows:

<&#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;>.GIF" width="20" height="20"></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>

Copy after login

Click here to download the complete example code from this website.

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Using PHP MySQL to build a chat room function example code
  • Using PHP MySql to write a chat room
  • Chat room php&mysql( 5)
  • Chat room php&mysql (4)
  • Chat room php&mysql (3)
  • Chat room php&mysql (2)
  • Chat room php&mysql (1)
  • Chat room php&mysql (6)
  • Chat room design based on PHP MySQL

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088780.htmlTechArticleSimple php mysql chat room implementation method (with source code), mysql chat room This article describes the simple php mysql example Chat room implementation method. Share it with everyone for your reference, the details are as follows: This...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template