php实现留言板功能
author:一佰互联 2019-04-26   click:142

这个小小的留言板功能适合班级内或者公司内部之间的讨论,对话和留言,非常的方便,更重要的是无需网络,对于公司管理层来说是非常乐于常见的,

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

1 首先是登录页面:

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

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

<?phpsession_start(); // 登录之后要把所包含登录的页面连接起来,开启sessioninclude("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"登录失败";}?>

如图所示,是登录页面

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

<body><div><h3><a href="fabu.php" 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>  <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把所有的登录情况下的页面连接起来

<h1>发布信息</h1><a href="main.php" rel="external nofollow" >主页面</a><br /><br /><?phpsession_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><?phpfunction 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<?phpsession_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 并且返回到登录页面

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持网页设计!