php如何实现留言板功能

发布时间:2021-06-18 15:25:58 作者:小新
来源:亿速云 阅读:280

这篇文章给大家分享的是有关php如何实现留言板功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

下面是这个留言板的写法:

1 首先是登录页面:

<form action="chuli.php" method="post">
 <div style="margin-left: 500px; margin-top: 200px;
  height: 250px; width: 250px">/*为了把登录表放到页面中间,比较美观*/
  <h2>公司内部留言板</h2>
 <div >用户名:<input type="text" name="username"/></div><br/>
 <div>密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"/></div><br/>
 <div><input type="submit" value="登录"/></div>
 </div>
</form>

2 登录页面完成后要进入登录处理页面了,也就是上面提交到的chuli.php

<?php
session_start(); // 登录之后要把所包含登录的页面连接起来,开启session
include("DADB.class.php");
$db=new DADB();
$user=$_POST["username"];
$pwd=$_POST["password"];
$sql="select password from yuangong where username='{$user}'";
$arr=$db->Query($sql);
if($arr[0][0]==$pwd && !empty($pwd))
{
 $_SESSION["username"]=$user;
 header("location:main.php");
}
else
{
 echo"登录失败";
}
?>

如图所示,是登录页面

php如何实现留言板功能

3.登录完成后是进入主页面,也就是显示自己收到的对话内容,下面是设计的数据库的表格和主页面的代码:

php如何实现留言板功能

php如何实现留言板功能

php如何实现留言板功能

<body>
<div><h4><a href="fabu.php" rel="external nofollow" >发布信息</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <a href="tuichu.php" rel="external nofollow" >退出系统</a></h4> </div>
<br/><br/>
<h3>留言信息:</h3>
<table cellpadding="0" cellspacing="0" border="1" width="60%">
 <tr>
  <td>发送人</td>
  <td>接收人</td>
  <td>发送时间</td>
  <td>信息内容</td>
 </tr>
 <?php
 session_start();
 if(empty($_SESSION["username"]))
 {
  header("location:login.php");
 }
 $user=$_SESSION["username"];
 include("DADB.class.php");
 $db=new DADB();
 $sql="select * from liuyan where recever='$user' or recever='all' ";
 $arr=$db->Query($sql);
 foreach($arr as $v)
 { $fjr=uname($v[1]);
  $jsr=uname($v[2]);
  echo"<tr>
  <td>{$fjr}</td> //发送人和接收人要用到姓名,所以这里我们调用了一个方法
  <td>{$jsr}</td>
  <td>{$v[3]}</td>
  <td>{$v[4]}</td>
 </tr>";
 }
 function uname($user) //运用了uname方法
 {
  global $db;  //要想方法里面也可以用$db 这里用了全局变量
  if($user=="all")
  {
  return "所有人";
  }else
 {
  $sql1="select name from yuangong where username='{$user}'";
  $att=$db->Query($sql1);
  return $att[0][0];}
 }
 ?>
</table>
</body>

4代码写到这里,比较重要的部分就完成了,下面是要进入发布信息页面了,相当于之前写的添加的页面,其处理页面也是和之前没什么区别的,差别在于现在的处理页面是在用户登录的情况下操作的,需要用session把所有的登录情况下的页面连接起来

php如何实现留言板功能

<h2>发布信息</h2>
<a href="main.php" rel="external nofollow" >主页面</a>
<br />
<br />
<?php
session_start();
if(empty($_SESSION["username"]))
{
 header("location:login.php");
 exit;
}
$user = $_SESSION["username"];
include("DADB.class.php");
$db = new DADB();
$shaoyou = "select * from firend where me='{$user}'";
$ahaoyou = $db->Query($shaoyou);
?>
<form action="fabuchuli.php" method="post">
 <div>接收人:
  <select name="jsr">
   <option value="all">所有人</option>
   <?php
   foreach($ahaoyou as $v)
   {
    $name = uname($v[2]);
    echo "<option value='{$v[2]}'>{$name}</option>";
   }
   ?>
  </select>
 </div>
 <br />
 <div>
  信息内容:<textarea name="neirong"></textarea>
 </div><br />
 <input type="submit" value="发送" />
</form>
<?php
function uname($user)
{
 global $db;
 if($user=="all")
 {
  return "所有人";
 }
 else
 {
  $sql1 = "select name from yuangong where username='{$user}'";
  $att = $db->Query($sql1);
  return $att[0][0];
 }
}
?>
</body>
//这是发布页面的代码 和添加页面的代码相似
<?php
<?php
session_start();
$re=$_POST["jsr"];
$comment=$_POST["neirong"];
$time=date("Y-m-d H:i:s"); //获取当前时间
$_SESSION["username"]=$user;
include("DADB.class.php");
$db=new DADB();
$sql="insert into liuyan VALUES ('','{$user}','{$jsr}','{$time}','{$comment}',false)";
if($db->Query($sql,0))
{
  header("location:main.php");
}
else{
 echo"发布失败";
}
?>
?>

一个简单的留言板已经完成了,退出的时候需要清除session 并且返回到登录页面

//退出页面代码
<?php
session_start();
unset($_SESSION["username"]);
header("location:login.php");
?>

感谢各位的阅读!关于“php如何实现留言板功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. Kubernetes怎么实现留言板功能
  2. php留言板功能模块有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:php如何读取CSV文件

下一篇:python清洗文件中数据的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》