0

0

php简略多人聊天界面的设计代码

php中文网

php中文网

发布时间:2016-06-13 11:38:44

|

2026人浏览过

|

来源于php中文网

原创

php简单多人聊天界面的设计代码

下面来简单介绍一个最简单的登录多人聊天系统的设计,只有四个文件,分别是登录页面login.php、多人聊天界面chat.php、设计数据库操作的sql.php文件以及注册页面regester.php,其中注册页面和登录页面的代码有%98的代码是雷同的。都是采用同样的结构。难点其实还是在sql.php文件中,因为这个是涉及到数据库操作的文件,所以很多问题基本都是由这个页面引起的,当然还有那个多人聊天界面也是很容易出问题。总体说来其实无非就是从数据库中根据用户名和接收者的名字取出相应的对话内容。这个缺点挺大的,在实际应用中几乎是毫无用武之地的,仅仅是为了学习才会想到这样做的,对话内容放在数据库里,那也是很耗费系统资源的行为。不过保存少量的对话信息应该还是可以的。闲话就不多说了,只要能够看懂sql.php文件的代码基本上就看懂了这整个多人聊天系统,注意我的四个文件全部都是放在一个文件下,所以当你要复制这个系统时,请务必保证这四个文件都是处于同一个文件夹下,并且最好再次确保这四个文件是用utf-8编码,如果不是请修改为utf-8编码。要不然就会出现中文乱码现象。一个汉字在gbk编码下占用2个字符,而在utf-8编码下占用三个字符,这一点可以用strlen()函数检验。这里要建立两个表,两个数据表如下所示:

这个表名叫comm,是专门用于存放用户对话信息的数据表:


下面的表名叫rege,是专门用于存放用户注册信息的:


立即学习PHP免费学习笔记(深入)”;

下面是登录页面login.php

酒店管理系统项目源码(三层开发)
酒店管理系统项目源码(三层开发)

系统采用VS2008+Sql2005开发适用于中小型的酒店管理,全部采用三层架构,ASP.NET开发,运用CSS加DIV的界面布局,完整的源代码和数据库设计,是你不可多得的参考资料。 有客房管理、房间类型管理、入住和退房管理等简单功能HotelManager为网站目录DB_51aspx下为Sql2005数据库,附加即可(Sql2000格式数据库转换后稍后发布)

下载
<span style="font-size:18px; color:#333333"><!Doctype html><html xmlns=http://www.w3.org/1999/xhtml><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=7" /></meta><title>welcom to login page</title><style type="text/css">body {	margin:auto;width:500px;background:url('table.jpg')no-repeat 200px;}div {	width:550px;padding-top:200px;	overflow:hidden;}form {    padding:3px;	border:dashed 1px green;	font-size:23px;	color:red;	background:white;}form input {	height:25px;}form span {	width:150px;height:40px;text-align:center;	display:inline-block;}form span.pic {	font-size:12px;	display:inline;}form  img{	  position: relative;	  left: 10px;	  top: 15px;	  padding-right:3px;}#show {display:inline;height:20px;margin-left:20px;font-size:12px;}#user {display:inline;font-size:14px;}#pwd {display:inline;font-size:14px;}.login {font-size:21px;align:center;display:block;height:26px;margin-left:150px;}</style></head><body><div><form action="login.php" method="post"><span>用户名:</span><input type="text" size="20"  name="user"/><span id="user"></span><br/><span>密码:</span><input type="text" size="20" name="pwd"/><span id="pwd"></span><br/><span>请输入验证码:</span><input type="text" size="10" name="check"/><img  src="tupian.php"   style="max-width:90%" onclick="javascript:this.src='tupian.php?dd'+Math.random()"/ alt="php简略多人聊天界面的设计代码" > <span class="pic">看不清点击图片换一张</span><br/><span id="show"></span><input class="login" type="submit" value="登录"/></form><h2><a href="regester.php">点击注册</a></h2></div><?php		session_id("yanzhen");	session_start();	$chars=strtolower($_SESSION['yanzhen']);//获得验证码;	if(isset($_POST['check'])){		if(trim($_POST['check'])==""){			echo "<script type=text/javascript>";			echo "document.getElementById('show').innerHTML='请输入验证码'";			echo "</script>";exit();		}		if($chars!=trim($_POST['check'])){			echo "<script type=text/javascript>";			echo "document.getElementById('show').innerHTML='验证码输入错误'";			echo "</script>";exit();		}	}	if(isset($_POST['user'])){		//check the user name		$user=trim($_POST['user']);		if($user==""){			echo "<script type=text/javascript>";			echo "document.getElementById('user').innerHTML='用户名为空'";			echo "</script>";exit();		}		if(preg_match("/^[\x7f-\xff]+$/", $user)==0){			echo "<script type=text/javascript>";			echo "document.getElementById('user').innerHTML='用户名只允许汉字'";			echo "</script>";exit();		}		if(isset($_POST['pwd'])){			$pwd=trim($_POST['pwd']);			if(preg_match("/^[a-z0-9\-]+$/i", $pwd)==0){				echo "<script type=text/javascript>";				echo "document.getElementById('pwd').innerHTML='密码只允许数字、下划线和字母'";				echo "</script>";exit();			}			if($pwd==''){				echo "<script type=text/javascript>";				echo "document.getElementById('pwd').innerHTML='密码为空'";				echo "</script>";			}else {				include_once 'Sql.php';				$sql = new Sql();				if($sql->checkPassword($user, $pwd)){					//$_SESSION["$user"]=true;					header("location:chat.php?name=".urlencode($user));					}				else {					echo "<script type=text/javascript>";					echo "document.getElementById('show').innerHTML='{$sql->infor}'";					echo "</script>";				}			}		}	}</span>

下面是注册页面regester.php

<span style="font-size:18px; color:#333333"><!Doctype html><html xmlns=http://www.w3.org/1999/xhtml><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=7" /></meta><title>welcom to login page</title><style type="text/css">body {	margin:auto;width:500px;background:url('table.jpg')no-repeat 200px;}div {	width:550px;padding-top:200px;	overflow:hidden;}form {    padding:3px;	border:dashed 1px green;	font-size:23px;	color:red;	background:white;}form input {	height:25px;}form span {	width:150px;height:40px;text-align:center;	display:inline-block;}form span.pic {	font-size:12px;	display:inline;}form  img{	  position: relative;	  left: 10px;	  top: 15px;	  padding-right:3px;}#show {display:inline;height:20px;margin-left:20px;font-size:12px;}.login {font-size:21px;align:center;display:block;height:26px;margin-left:150px;}</style></head><body><div><form action="regester.php" method="post"><span>用户名:</span><input type="text" size="20"  name="user"/><span id="user"></span><br/><span>密码:</span><input type="text" size="20" name="pwd"/><span id="pwd"></span><br/><span>请输入验证码:</span><input type="text" size="10" name="check"/><img  src="tupian.php"   style="max-width:90%" onclick="javascript:this.src='tupian.php?dd'+Math.random()"/ alt="php简略多人聊天界面的设计代码" > <span class="pic">看不清点击图片换一张</span><br/><span id="show"></span><input class="login" type="submit" value="注册"/></form></div><?php	require_once 'Sql.php';if(isset($_POST['user'])){		//check the user name		$user=trim($_POST['user']);		if($user==""){			echo "<script type=text/javascript>";			echo "document.getElementById('user').innerHTML='用户名为空'";			echo "</script>";exit();		}		if(strlen($user)>6){			echo "<script type=text/javascript>";			echo "document.getElementById('user').innerHTML='用户名太长'";			echo "</script>";exit();		}		if(preg_match("/^[\x7f-\xff]+$/", $user)==0){			echo "<script type=text/javascript>";			echo "document.getElementById('user').innerHTML='用户名只允许汉字'";			echo "</script>";exit();		}		if(isset($_POST['pwd'])){			$pwd=trim($_POST['pwd']);			if(preg_match("/^[a-z0-9\-]+$/i", $pwd)==0){				echo "<script type=text/javascript>";				echo "document.getElementById('pwd').innerHTML='密码只允许数字、下划线和字母'";				echo "</script>";exit();			}			if($pwd==''){				echo "<script type=text/javascript>";				echo "document.getElementById('pwd').innerHTML='密码为空'";				echo "</script>";exit();			}elseif (strlen($pwd)>=19){				echo "<script type=text/javascript>";				echo "document.getElementById('pwd').innerHTML='密码长度太长'";				echo "</script>";exit();			}			else {				include_once 'Sql.php';				$sql = new Sql();				$sql->regerster($user, $pwd);			}		}	}		?></span>

下面是操作数据库的Sql.php文件

<span style="font-size:18px; color:#333333"><?phpheader("charset=utf-8");class Sql {	protected $conn;	public $infor='';	public function __construct(){		$this->conn=mysql_connect("localhost","like","admin");		mysql_select_db("test");		if(!$this->conn)	die("fail to connect the mysql database");	}	public function queryInfor($receiver){		$arr=array();		$receiver=iconv("utf-8","gbk",$receiver);		$query="select * from `comm` where recever='$receiver' order by id desc";		mysql_query("set names gbk");		$result=mysql_query($query,$this->conn) or die("fuck error");		while($row=mysql_fetch_row($result)){			$arr[]=$row;		}		if(count($arr)>5){//保存的信息超过五条,就删除掉多余信息			for($i=count($arr)-1;$i>4;$i--){				mysql_query("delete from `comm` where id={$arr[$i][0]}") or die("fail to delete the extra information");				unset($arr[$i]);			}		}		mysql_free_result($result);		for($i=0;$i<count($arr);$i++){			for($j=1;$j<5;$j++){				$arr[$i][$j]=iconv("gbk", "utf-8", $arr[$i][$j]);			}		}		return $arr;	}	public function queryQuery($sendor){		$arr=array();		$sendor=iconv("utf-8","gbk",$sendor);		$query="select * from `comm` where sendor='$sendor' order by id desc";		mysql_query("set names gbk");		$result=mysql_query($query,$this->conn) or die("fuck error");		while($row=mysql_fetch_row($result)){			$arr[]=$row;		}		if(count($arr)>5){//保存的信息超过五条,就删除掉多余信息			for($i=count($arr)-1;$i>4;$i--){				mysql_query("delete from `comm` where id={$arr[$i][0]}") or die("fail to delete the extra information");				unset($arr[$i]);			}		}		mysql_free_result($result);		for($i=0;$i<count($arr);$i++){			for($j=1;$j<5;$j++){				$arr[$i][$j]=iconv("gbk", "utf-8", $arr[$i][$j]);			}		}		return $arr;	}	public function setDatabase($sender,$receiver,$content){		$timestamp=date("Y-m-d H:i:s",time());		$sender=iconv("utf-8","gbk",$sender);		$content=iconv("utf-8", "gbk", $content);		$receiver=iconv("utf-8","gbk",$receiver);		mysql_query("set names gbk",$this->conn);		$query="insert into `comm`(sendor,time,recever,content)values('$sender','$timestamp','$receiver','$content')";		mysql_query($query,$this->conn) or die("insert error");			}	public function getSenderAll($name){		$arr= array();		$name=iconv("utf-8","gbk",$name);		$query="select user from `rege` where 1 order by id desc";		mysql_query("set names gbk",$this->conn);		$result=mysql_query($query,$this->conn) or die("no data in database");		while($row=mysql_fetch_row($result)){			$arr[]=$row[0];		}		for($i=0;$i<count($arr);$i++){			$arr[$i]=iconv("gbk", "utf-8", $arr[$i]);		}		return $arr;	}	public function logout($name){		//采用这种数据库方式注销登录不建议使用,因为耗资源,采用SESSION更好		//而且写程序还要更难,只能用来练习用。		$name=iconv("utf-8","gbk",$name);		$query="update `rege` set status=0 where user='$name'";		mysql_query("set names gbk");		mysql_query($query,$this->conn) or die("注销失败");	}	public function  checkPassword($name,$password=null){		$name=iconv("utf-8","gbk",$name);		$queryP="select * from `rege` where user='$name' and password='$password' limit 1";		$queryQ="select * from `rege` where user='$name' limit 1";		mysql_query("set names gbk",$this->conn);			if($password!=null){			mysql_query($queryP,$this->conn) or die("ass error");			if(mysql_affected_rows($this->conn)>0){						$result=mysql_query("select status from `rege` where user='$name'");				$row=mysql_fetch_row($result);				if($row[0])	{					$this->infor="该用户已经登录";					return false;				}				$query="update `rege` set status=true where user='$name'";				mysql_query($query,$this->conn);				return true;				}else{				$this->infor="用户名或密码错误";				return false;			}		}		else {			mysql_query($queryQ,$this->conn) or die("sorry error");			if(mysql_affected_rows($this->conn)>0){				$this->infor="该用户名已被注册";				return false;			}			return true;		}	}	public function regerster($name,$pwd){		$name=iconv("utf-8","gbk",$name);		$query="INSERT INTO `rege`(`user`,`password`) VALUES ('$name','$pwd')";		mysql_query("set names gbk",$this->conn);		mysql_query($query,$this->conn) or die("注册失败");		$this->infor="注册成功";	}	public function close(){		mysql_close($this->conn);	}}</span>

下面是用户登录之后的多人聊天界面chat.php,说是多人聊天,其实是很粗糙的界面,就是一个ul标签列出了所有人传达给你的最新五条信息,多余信息会被自动删除,然后你可以选择任意一个人写下对他/她说的话,然后就是刷新显示。其实原理挺简单的。

<span style="font-size:18px; color:#333333"><?php 	require_once 'Sql.php';	session_id("yanzhen");	session_start();	if(isset($_GET['id'])&&isset($_GET['log'])){		if($_GET['id']==1){			$og= new Sql();			$sess_name=urldecode($_GET['log']);			$og->logout($sess_name);			$_SESSION["$sess_name"]=false;			header("content-type=text/html;charset=utf-8");			echo "注销成功";			echo "<a href='login.php'>返回到登录页面</a>";			exit();		}	}		if(isset($_GET['name'])){		$user=urldecode($_GET['name']);		$user=$_GET['name'];	}	else{		header("location:login.php");		}	if(isset($_POST['content'])){		$tmp_content=$_POST['content'];		$tmp_user=$_POST['user'];		$tmp_receive=$_POST['selec'];		$tmp=new Sql();		$tmp->setDatabase($tmp_user, $tmp_receive, $tmp_content);		$tmp=$tmp_content=$tmp_user=$tmp_receive=null;	}?><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=7" /></meta><title>welcom to chat page</title><style type="text/css">#area {margin:auto;width:500px;color:red;background-color:grey;}#area ul li{height:20px;list-style-type:none;}form {margin:auto;width:500px;height:35px;color:white;   background-color:black;}h3 {padding-left:200px;}   </style></head><body><h3><a href="chat.php?id=1&log=<?php echo urlencode($user);?>">注销</a></h3><div id="area"><ul><?php 	$sql= new Sql();	$arr=array();	$arr=$sql->queryInfor($user);	for($i=count($arr)-1;$i>=0;$i--){		echo "<li>".$arr[$i][1]."在"					.$arr[$i][2]."对你说过:</li><br/>";		echo "<li>".$arr[$i][4]."</li><br/>";	}	$arr=$sql->queryQuery($user);	for($i=count($arr)-1;$i>=0;$i--){		echo "<li style='text-align:right;color:green'>"."你在".$arr[$i][2]."对"		.$arr[$i][3]."说过:</li><br/>";		echo "<li style='text-align:right;color:green'>".$arr[$i][4]."</li><br/>";	}?></ul></div><form action="chat.php?name=<?php echo urlencode($user);?>" method="post"><input type="hidden" name="user" value="<?php echo $user; ?>"/><?php echo $user;?>想对<select name="selec"><?php 		$arr=$sql->getSenderAll($user);	for($i=0;$i<count($arr);$i++){			if($arr[$i]!=$user)			echo "<option value=\"$arr[$i]\">$arr[$i]</option>";	}?></select>说:<input name="content" type="text" size=30 maxlength=30/><input type="submit" value="发送"/></form></body></html><?php//show the information when chatting?></span>

聊天界面图:




相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

705

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

233

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

117

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

22

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

61

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

30

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

15

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

669

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

58

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 18.1万人学习

Pandas 教程
Pandas 教程

共15课时 | 1.1万人学习

C# 教程
C# 教程

共94课时 | 9.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号