PHP实现基本留言板功能原理与步骤详解


Posted in PHP onMarch 26, 2020

本文实例讲述了PHP实现基本留言板功能的方法。分享给大家供大家参考,具体如下:

作为一个PHP的初学者,我试着写了一个留言板,页面有点丑,多多见谅,嘻嘻嘻
#我们写留言板需要用到数据库,所以我们先要建立三个表
user表
PHP实现基本留言板功能原理与步骤详解
friend表
PHP实现基本留言板功能原理与步骤详解
text表
PHP实现基本留言板功能原理与步骤详解

#首先需要写一个注册与登录

##注册
zhuce.html

<meta charset="utf-8">
<title>zhuce</title>
</head>
<body>
<form method="POST" action="zhuce.php">
<div style="margin-left: 500px;margin-top:200px;height: 250px;width: 250px">
<h1>用户注册页面</h1>
用户名:<input type="text" name="username">
<div>密   码:<input type="password" name="password">
<div><input type="submit" name="submit" value="注册"></div>
</div>
</form>
</body>

zhuce.php

<?php
session_start();
header("Content-type: text/html; charset=utf-8"); //处理数据库用户名乱码
$user=$_POST["username"];
$pwd=$_POST["password"];
if($user==""||$pwd=="")
	 {
   	echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>"; 
	 }
else 
  	{
	 		
 			$link=mysqli_connect("localhost","root","","liuyan");//连接数据库
	 		mysqli_query($link,"set names utf8"); 
	 		$sql="select username from user where username='$_POST[username]'";
    	$result=mysqli_query($link,$sql);//执行sql语句
     	$num=mysqli_num_rows($result);//统计执行结果影响的行数
     	if($num)//如果存在该用户
     		{
     			echo "<script>alert('用户名已存在!'); history.go(-1);</script>"; 
    		}
      else//注册新用户
    		 { 
     			$sql_insert="insert into user (username,password)values('$_POST[username]','$_POST[password]')";
     			$res_insert=mysqli_query($link,$sql_insert);
     			if($res_insert)
     			{
     				echo "<script>alert('注册成功!');window.location='denglu.html';</script>"; 
     			}
     			else
     			{
     				echo "<script>alert('系统繁忙请重试!'); history.go(-1);</script>"; 
     			}
     	  }
  }
 ?>

#效果如下
PHP实现基本留言板功能原理与步骤详解

##登录
denglu.html

<head>
<meta charset="utf-8">
<title>denglu</title>
</head>
<body>
<form method="POST" action="denglu.php">
<div style="margin-left: 500px;margin-top:200px;height: 250px;width: 250px">
<h1>用户登录页面</h1>
用户名:<input type="text" name="username">
<div>密   码:<input type="password" name="password"></div><br/>
<input type="submit" name="submit" value="登录"> 
<a href="zhuce.html" rel="external nofollow" >注册</a> 
 </div>
</form>
</body>

denglu.php

<?php
  session_start();
  	$user=$_POST["username"];
	$_SESSION["uesrname"]=$user;//session超全局变量
	$pwd=$_POST["password"];//获取密码
	if($user=""||$pwd="")
	{
   echo "<script>alert('请输入用户名或密码!'); history.go(-1);</script>"; 
	}
	else
	{
		$link=mysqli_connect("localhost","root","","liuyan");//连接数据库
    mysqli_query($link,"set names utf8"); 
    $sql = "select username,password from user where username = '$_POST[username]' and password = '$_POST[password]'"; 
		$result=mysqli_query($link,$sql);//执行sql语句
		$num=mysqli_num_rows($result);//统计影响结果行数,作为判断条件
		if($num)
		{
			echo "<script>alert('登录成功!');window.location='003.php';</script>";//登录成功页面跳转 
		}
		else
		{
			echo "<script>alert('用户名或密码不正确!');history.go(-1);</script>"; 
	  }
	}
?>

#效果如下
PHP实现基本留言板功能原理与步骤详解
#下面需要我们写一个登录进去的页面,它显示别人给你发来的留言以及进入你要给别人留言的页面或者退出系统,在这里我将html代码嵌入php
php代码如下
003.php

<?php
session_start();
global $user;//定义$user
global $user1;
$_SESSION["username"]=$user;
$username=$_SESSION["uesrname"];
$user1=implode("",$_SESSION);//将session中的数组变为字符串元素
$link=mysqli_connect("localhost","root","","liuyan");//连接数据库
mysqli_query($link,"set names utf8"); 
$sql="select * from text where recever='{$username}'";
$result=mysqli_query($link,$sql);//执行语句
if($num=mysqli_num_rows($result))//将HTML嵌入PHP中,实现从数据库中获得留言信息
{?>
	<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
</head>
 <body>
 <div>
 	<a href="fabu.html" rel="external nofollow" >发布信息</a>      
 	<a href="tuichu.php" rel="external nofollow" >退出系统</a></h3></div>
 	<br/><br/>
 	
 	<h2>留言信息:</h2>
 	<table cellpadding="0" cellspacing="0" border="1" width="60%">
<tr bgcolor="#8BBCC7">

	<td>发送人</td>
	<td>接收人</td>
	<td>发送时间</td>
	<td>信息内容</td>
	<?php echo '<pre>';
	while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
	{?>
		<tr bgcolor="#FFFFFF">
	<td ><?php echo $row['sender'];?> </td>
	<td > <?php echo $row['recever'];?> </td>
	<td > <?php echo $row['comment'];?> </td>
	<td > <?php echo $row['time'];?> </td>
  <?php 
    }
    ?>
</tr>
</table>
</body>
</html>
   <?php	


}?>

#效果如下
PHP实现基本留言板功能原理与步骤详解
#接下来我们就该写发布的页面以及PHP代码了
fabu.html

<head>
	<meta charset="utf-8">
	<title>fabu</title>
</head>
<body>
 <form method="POST" action="fabu.php">
<h1>发布信息<h1>
<a href="003.php" rel="external nofollow" >主页面</a>
<div>接收人:<input type="text" name="recever">
</select>
</div>
</select>
</div>
<br/>
<div>
	信息内容:<textarea input type="text" name="neirong"></textarea>
</div><br/>
<input type="submit" value="发送">
</form>

fabu.php

<?php
session_start();
header("Content-type: text/html; charset=utf-8");
global $user;
$re=$_POST["recever"];//获取recever
$comment=$_POST["neirong"];//获取留言
@date_default_timezone_set(PRC);//将数组变为字符串函数
$time=date("Y-m-d G:i:s");//获取时间,G为24小时制
$_SESSION["username"]=$user;//开启session
$user1=implode("",$_SESSION);//将数组转为字符串
$link=mysqli_connect("localhost","root","","liuyan");//连接数据库
mysqli_query($link,"set names utf8"); 
$sql="insert into text(sender,recever,comment,time) values('$user1','$re','$comment','$time')";
$result=mysqli_query($link,$sql);//执行语句
$sql1="insert into friend(me,friend) values('$user1','$re')";//将me,friend存入数据库
$result=mysqli_query($link,$sql1);//执行语句
if($recever=""||$comment="")
{
	echo "<script>alert('发布失败!');window.location='fabu.html';</script>";
}
else
{
	echo "<script>alert('发布成功!');window.location='fabu.html';</script>";
}
?>

#效果如下
PHP实现基本留言板功能原理与步骤详解
#最后是退出系统

<?php
session_start();
unset($_SESSION["uesrname"]);
echo "<script>alert('退出成功!');window.location='denglu.html';</script>"; 
?>

#效果如下
PHP实现基本留言板功能原理与步骤详解

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP自动生成月历代码
Oct 09 PHP
PHP判断远程url是否有效的几种方法小结
Oct 08 PHP
php函数array_merge用法一例(合并同类数组)
Feb 03 PHP
dedecms函数分享之获取某一栏目所有子栏目
May 19 PHP
php中$美元符号与Zen Coding冲突问题解决方法分享
May 28 PHP
php中adodbzip类实例
Dec 08 PHP
Yii获取当前url和域名的方法
Jun 08 PHP
windows8.1下Apache+Php+MySQL配置步骤
Oct 30 PHP
Yii2实现ActiveForm ajax提交
May 26 PHP
PHP基于GD2函数库实现验证码功能示例
Jan 27 PHP
php实现映射操作实例详解
Oct 02 PHP
让whoops帮我们告别ThinkPHP6的异常页面
Mar 02 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
Mar 26 #PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
Mar 26 #PHP
php设计模式之正面模式实例分析【星际争霸游戏案例】
Mar 24 #PHP
php设计模式之模板模式实例分析【星际争霸游戏案例】
Mar 24 #PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
Mar 24 #PHP
php设计模式之原型模式分析【星际争霸游戏案例】
Mar 23 #PHP
php高性能日志系统 seaslog 的安装与使用方法分析
Feb 29 #PHP
You might like
php 静态化实现代码
2009/03/20 PHP
php学习笔记(三)操作符与控制结构
2011/08/06 PHP
PHP的Socket网络编程入门指引
2015/08/11 PHP
thinkPHP5.0框架命名空间详解
2017/03/18 PHP
实例讲解PHP表单处理
2019/02/15 PHP
Smarty模板配置实例简析
2019/07/20 PHP
thinkphp5 模型实例化获得数据对象的教程
2019/10/18 PHP
Jquery插件 easyUI属性汇总
2011/01/19 Javascript
JavaScript中使用构造器创建对象无需new的情况说明
2012/03/01 Javascript
jquery $.each() 使用小探
2013/08/23 Javascript
JavaScript学习笔记之Cookie对象
2015/01/22 Javascript
JS实现的表格操作类详解(添加,删除,排序,上移,下移)
2015/12/22 Javascript
跨域资源共享 CORS 详解
2016/04/26 Javascript
JS实现的DIV块来回滚动效果示例
2017/02/07 Javascript
简述jQuery Easyui一些用法
2017/08/01 jQuery
浅谈ES6新增的数组方法和对象
2017/08/08 Javascript
使用canvas进行图像编辑的实例
2017/08/29 Javascript
Vue 实现双向绑定的四种方法
2018/03/16 Javascript
Vue+Element UI+vue-quill-editor富文本编辑器及插入图片自定义
2019/08/20 Javascript
python实现文件名批量替换和内容替换
2014/03/20 Python
python函数形参用法实例分析
2015/08/04 Python
python3制作捧腹网段子页爬虫
2017/02/12 Python
Python实现求解括号匹配问题的方法
2018/04/17 Python
Python3中的列表生成式、生成器与迭代器实例详解
2018/06/11 Python
对python Tkinter Text的用法详解
2018/10/11 Python
PyQt5实现画布小程序
2020/05/30 Python
详解css3中的伪类before和after常见用法
2020/11/17 HTML / CSS
HTML5实现桌面通知 提示功能
2017/10/11 HTML / CSS
杭州信雅达系统.NET工程师面试试题
2015/02/08 面试题
班委竞选演讲稿
2014/04/28 职场文书
大学生见习期满自我鉴定
2014/09/13 职场文书
乡镇党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书
试用期辞职信范文
2015/03/02 职场文书
驻村工作简报
2015/07/20 职场文书
2015年学校远程教育工作总结
2015/07/20 职场文书
python实现简单石头剪刀布游戏
2021/10/24 Python