目錄
点击注册" >点击注册
注销" >注销
首頁 後端開發 php教程 php简略多人聊天界面的设计代码

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

Jun 13, 2016 am 11:38 AM
echo gt lt quot user

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,是专门用于存放用户注册信息的:


下面是登录页面login.php

<span style="font-size:18px; color:#333333"><meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7">
<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>
<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="/static/imghw/default1.png" data-src="tupian.php" class="lazy"   style="max-width:90%" onclick="javascript:this.+Math.random()" alt="php简略多人聊天界面的设计代码" > <span class="pic">看不清点击图片换一张</span><br><span id="show"></span><input class="login" type="submit" value="登录">
</form>
<h2 id="a-href-regester-php-点击注册-a"><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 "";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"><meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7">
<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>
<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="/static/imghw/default1.png" data-src="tupian.php" class="lazy"   style="max-width:90%" onclick="javascript:this.+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 "";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 for return public function queryquery from where sendor="$sendor" order by id desc mysql_query names gbk>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 for return public function setdatabase h:i:s mysql_query names gbk>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 return public function logout set status="0" where user="$name" mysql_query names gbk>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);	}}</count></count></count></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;	}?><meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7">
<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>
<h3 id="a-href-chat-php-id-amp-log-lt-php-echo-urlencode-user-gt-注销-a"><a href="chat.php?id=1&log=<?php%20echo%20urlencode(%24user);?>">注销</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%20echo%20urlencode(%24user);?>" 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 if echo value='\"$arr[$i]\"'>$arr[$i]";	}?></count></select>说:<input name="content" type="text" size="30" maxlength="30/"><input type="submit" value="发送">
</form>
<?php //show the information when chatting?></span>
登入後複製

聊天界面图:




本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

華為GT3 Pro和GT4的差異是什麼? 華為GT3 Pro和GT4的差異是什麼? Dec 29, 2023 pm 02:27 PM

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

修復:截圖工具在 Windows 11 中不起作用 修復:截圖工具在 Windows 11 中不起作用 Aug 24, 2023 am 09:48 AM

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

五個精選的Go語言開源項目,帶你探索科技世界 五個精選的Go語言開源項目,帶你探索科技世界 Jan 30, 2024 am 09:08 AM

在現今科技快速發展的時代,程式語言也如雨後春筍般湧現。其中一門備受矚目的語言就是Go語言,它以其簡潔、高效、並發安全等特性受到了許多開發者的喜愛。 Go語言以其強大的生態系統而著稱,其中有許多優秀的開源專案。本文將介紹五個精選的Go語言開源項目,帶領讀者一起探索Go語言開源專案的世界。 KubernetesKubernetes是一個開源的容器編排引擎,用於自

Laravel開發:如何使用Laravel Echo和Pusher實現WebSockets通訊? Laravel開發:如何使用Laravel Echo和Pusher實現WebSockets通訊? Jun 13, 2023 pm 05:01 PM

Laravel是一個流行的PHP框架,具有高度可擴展性和高效性,它提供了許多強大的工具和程式庫,讓開發者可以快速建立高品質的網路應用程式。其中,LaravelEcho和Pusher是兩個非常重要的工具,透過它們可以輕鬆實現WebSockets通信,本文將詳細介紹如何在Laravel應用程式中使用這兩個工具。什麼是WebSockets? WebSockets

Go語言開發必備:5個熱門框架推薦 Go語言開發必備:5個熱門框架推薦 Mar 24, 2024 pm 01:15 PM

《Go語言開發必備:5個熱門框架推薦》Go語言作為一門快速、高效的程式語言,受到越來越多開發者的青睞。為了提高開發效率,優化程式碼結構,許多開發者選擇使用框架來快速建立應用。在Go語言的世界中,有許多優秀的框架可供選擇。本文將介紹5個熱門的Go語言框架,並提供具體的程式碼範例,幫助讀者更好地理解和使用這些框架。 1.GinGin是一個輕量級的Web框架,擁有快速

PHP中echo關鍵字的作用與使用方法詳解 PHP中echo關鍵字的作用與使用方法詳解 Jun 28, 2023 pm 08:12 PM

PHP中echo關鍵字的作用和使用方法詳解PHP是一種廣泛使用的伺服器端腳本語言,它在網頁開發中被廣泛應用。而echo關鍵字是在PHP中用來輸出內容的一種方法。本文將詳細介紹echo關鍵字的作用與使用方法。作用:echo關鍵字的主要作用是將內容輸出到瀏覽器。在網頁開發中,我們需要將資料動態地呈現到前端頁面上,這時就可以使用echo關鍵字將資料輸出到頁面上。 e

市面上最流行的golang框架有哪些? 市面上最流行的golang框架有哪些? Jun 01, 2024 pm 08:05 PM

目前最受歡迎的Go框架有:Gin:輕量級、高效能的Web框架,簡潔易用。 Echo:快速、高度可自訂的網路框架,提供高效能路由和中介軟體。 GorillaMux:快速、靈活的多工器,提供進階路由設定選項。 Fiber:效能最佳化的高效能Web框架,處理高並發請求。 Martini:物件導向設計的模組化網路框架,提供豐富的功能集。

如何修復無法連線到iPhone上的App Store錯誤 如何修復無法連線到iPhone上的App Store錯誤 Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

See all articles