当前位置: 移动技术网 > IT编程>开发语言>JavaScript > PHP+txt聊天室

PHP+txt聊天室

2020年07月24日  | 移动技术网IT编程  | 我要评论

PHP+txt聊天室

最近朋友发了一篇ASP+txt聊天室,非常不爽,我可是支持PHP的啊
所以就发了这篇文章
由于是临时写的,所以可能会有问题,如果发现请指出

环境配置

PHP版本: PHP7
服务器: 推荐Apache24,其他的也没有影响
系统: Windows或Linux都可以
目录: 
	在你的服务器任意目录就可以
	在目录下要有几个文件:
		index.php
		chat.css
		chat.js
		//前端,以后会讲
		enter.php
		//进入聊天通道,前端
		background.php
		//后端
		chats (文件夹)
		//用于存放聊天记录
		chats_info.txt
		//在进入聊天时会用到
		//如果在检查聊天密码时想偷懒,就将聊天密码信息放在这
		/*格式:
		聊天名称  (换行)
		聊天密码*/

登录聊天

/*首先要有一个enter.php,这是用户进入聊天室的通道
这个页面可以放在后端
这个页面用于检查用户密码是否正确,聊天密码是否正确等
这个页面要创建几个COOKIE:
username: 用户名(这个在登录时就可以创建)
password: 用户密码(在登录时就可以创建)
chat_name: 聊天名称
chat_password: 聊天密码*/


//以下是示例代码

$username=$_POST["username"];
/*获取用户名
如果在登录时使用COOKIE则用
$username=$_COOKIE["username"];
用GET方式请求则相应变化*/
$password=$_POST["password"];
$chat_name=$_POST["chat_name"];
$chat_password=$_POST["chat_password"];

function check_password($password, $username)
{
	/*
	连接数据库检查用户密码是否存在
	最后返回OK或密码不正确等提示
	*/
	return "OK";//如果不想判断就直接返回OK
}
function check_chat_exists($chat_name)
{
	//检查聊天是否存在
	//这里通过检查存放聊天记录的文件是否存在来检查
	if(file_exists("chats/" . $chat_name . ".txt"))
	{
		return "OK";
	}
	else
	{
		return "Error: Can not find the chat";
	}
}
function check_chat_password($chat_name, $chat_password)
{
	/*
	连接数据库检查聊天密码
	如果你使用txt来检查密码,那么可以使用以下代码
	*/
	$f=fopen("chats_info.txt", "r");
	$line="";//存储读取到的信息
	$line_num=1;//行数
	$r="Can not find your chat";//返回值
	while(!feof($f))//逐行读取
	{
		$line=fgets($f);
		if($line_num%2==0)
		{
			//通过单复数来判断读取到的是聊天名称还是密码
			if($line==$chat_name)//聊天名称是否相符
			{
				//检查密码
				if(fgets($f)==$chat_password)
				{
					$r="OK";
					break;
				}
				else
				{
					$r="Your password is not correct";
					break;
				}
			}
		}
		$line_num=$line_num+1;
	}
	fclose($f);
	return $r;
}
$back=check_password($password, $username);
if($back=="OK")
{
	$back=check_chat_exists($chat_name);
	if($back=="OK")
	{
		$back=check_chat_password($chat_name, $chat_password);
		if($back=="OK")
		{
			setcookie("username", $username, time()+60*3600*24, "/");
			setcookie("password", $password, time()+60*3600*24, "/");
			setcookie("chat_name", $chat_name, time()+60*3600*24, "/");
			setcookie("chat_password", $chat_password, time()+60*3600*24, "/");
			echo <<<EOF
			<script>
			location.href="index.php";
			</script>
EOF;
		}
		else
		{
			die($back);
		}
	}
	else
	{
		die($back);
	}
}
else
{
	die("Please login first");
}

前端部分

这是前端部分

index.php

<html>
<head>
<meta charset="utf-8">
<title>PHP聊天室</title>
<link rel="stylesheet" href="chat.css" type="text/css">
</head>
<body>

<!--
UI还是得自己设计哦
这个页面很丑陋,只是做示范的聊天基本页面
-->

<div id="chat_frame" class="chat_frame">
  Welcome to PHP chatroom!<br>
  Beat ASP!
</div>
<!--聊天记录-->

<script src="chat.js">
</script>
<!--导入JS-->

<div>
  <input type="text" id="send_text" class="input" placeholder="text to send">
  <button type="button" class="button" onclick="send()" id="go_button">send</button>
</div>
<!--发送-->

</body>
</html>

chat.css

.chat_frame{
  width:90%;
  height:300;
  background-color:9999FF;/*底色*/
  border:5px solid #E0E0E0;/*边框*/
  border-radius:15px;/*圆角设计*/
  padding:10px;
}
.input{
  width:300;
  height:40;
  background-color:#000000;
  color:#FFFFFF;
  padding:5px;
}
.button{
  width:100;
  height:30;
}

chat.js

var his="";//存储当前聊天记录
function get_his()//获取历史记录
{
	his=document.getElementById("chat_frame").innerHTML;
}
setInterval("get_his()", 500);//每过一段时间刷新历史

function send()
{
	//通过Ajax请求发送信息
	var text=document.getElementById("send_text").value;
	var button=document.getElementById("go_button");
	button.disabled="disabled";
	//在发送完成之前禁止按下按钮,防止刷屏
	
	var http_go;
	if(window.XMLHttpRequest)
	{
	    http_go=new XMLHttpRequest;
	}
	else
	{
	    http_go=new ActiveXObject("Microsoft.XMLHTTP");
	}
	http_go.onreadystatechange=function()
	{
		if(http_go.readyState==4 && http_go.status==200)
		{
			if(his!=http_go.responseText)
			{
				//只有历史记录和新聊天记录不一样才刷新
				document.getElementById("chat_frame").innerHTML=http_go.responseText;//刷新
			}
		}
		button.disabled="";
		button.innerHTML="send";
		//允许按下按钮
	}
	http_go.open("POST", "background.php", true);
	http_go.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	http_go.send("type=send&msg="+text);
}

function get_msg()
{
	//通过Ajax请求获取新信息
	
	var http_go;
	if(window.XMLHttpRequest)
	{
	    http_go=new XMLHttpRequest;
	}
	else
	{
	    http_go=new ActiveXObject("Microsoft.XMLHTTP");
	}
	http_go.onreadystatechange=function()
	{
		if(http_go.readyState==4 && http_go.status==200)
		{
			if(his!=http_go.responseText)
			{
				//只有历史记录和新聊天记录不一样才刷新
				document.getElementById("chat_frame").innerHTML=http_go.responseText;//刷新
			}
		}
	}
	http_go.open("POST", "background.php", true);
	http_go.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	http_go.send("type=get");
}
setInterval("get_msg()", 1000);


后端部分

background.php

$username=$_COOKIE["username"];
$password=$_COOKIE["password"];
$chat_name=$_COOKIE["chat_name"];
$chat_password=$_COOKIE["chat_password"];

function check_password($password, $username)
{
	/*
	连接数据库检查用户密码是否存在
	最后返回OK或密码不正确等提示
	*/
	return "OK";//如果不想判断就直接返回OK
}
function check_chat_exists($chat_name)
{
	//检查聊天是否存在
	//这里通过检查存放聊天记录的文件是否存在来检查
	if(file_exists("chats/" . $chat_name . ".txt"))
	{
		return "OK";
	}
	else
	{
		return "Error: Can not find the chat";
	}
}
function check_chat_password($chat_name, $chat_password)
{
	/*
	连接数据库检查聊天密码
	如果你使用txt来检查密码,那么可以使用以下代码
	*/
	$f=fopen("chats_info.txt", "r");
	$line="";//存储读取到的信息
	$line_num=1;//行数
	$r="Can not find your chat";//返回值
	while(!feof($f))//逐行读取
	{
		$line=fgets($f);
		if($line_num%2==0)
		{
			//通过单复数来判断读取到的是聊天名称还是密码
			if($line==$chat_name)//聊天名称是否相符
			{
				//检查密码
				if(fgets($f)==$chat_password)
				{
					$r="OK";
					break;
				}
				else
				{
					$r="Your password is not correct";
					break;
				}
			}
		}
		$line_num=$line_num+1;
	}
	fclose($f);
	return $r;
}

function get_msg($chat_name)//获取聊天记录
{
	global $username;
	$f=fopen("chats/" . $chat_name . ".txt", "r") or die("Can not load the history");
	while(!feof($f))//逐行读取
	{
		echo fgets($f) . "<br>";
	}
	fclose($f);
}

function send_msg($chat_name)//发送信息
{
	global $username;
	$f=fopen("chats/" . $chat_name . ".txt", "a") or die("Error");
	fwrite($f, "[" . date("d/m/Y, h:i, a") . ", " . $username . "]: " . $_POST["msg"]);
	fclose($f);
	
	$f=fopen("chats/" . $chat_name . ".txt", "r") or die("Can not load the history");
	while(!feof($f))//逐行读取
	{
		echo fgets($f) . "<br>";
	}
	fclose($f);
}

$back=check_password($password, $username);
if($back=="OK")
{
	$back=check_chat_exists($chat_name);
	if($back=="OK")
	{
		$back=check_chat_password($chat_name, $chat_password);
		if($back=="OK")
		{
			if($_POST["type"]=="send")
			{
			send_msg($chat_name);
			}
			else
			{
			get_msg($chat_name);
			}
		}
		else
		{
			die($back);
		}
	}
	else
	{
		die($back);
	}
}
else
{
	die("Please login first");
}

本文地址:https://blog.csdn.net/twxwjh/article/details/107541274

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网