用php(session)实现留言板功能----2017-05-09

时间:2023-03-09 13:25:59
用php(session)实现留言板功能----2017-05-09

要实现留言功能,发送者和接受者必不可少,其次就是留言时间留言内容。

要实现的功能:

1、登录者只能查看自己和所有人的信息,并能够给好友留言

2、留言板页面,好友采取下拉列表,当留言信息为空时,显示提示,并停留在当前页面

3、查看过的信息,消息显示已读

4、用$_SESSION[]获取登陆者,并用$_SESSION[]防止用户绕过登录直接进入主页面

第一步建数据库:

需要用到的三张表分别为:friend表,yuangong表,chenai表

yuangong表:

用php(session)实现留言板功能----2017-05-09

friend表:

用php(session)实现留言板功能----2017-05-09

chenai表

用php(session)实现留言板功能----2017-05-09

第二步写登录页面:

1、denglu.php

效果图:(简单)

用php(session)实现留言板功能----2017-05-09

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.aa{
height: 200px;
width: 300px;
margin: 100px auto;
border: 1px solid black;
text-align: center;
line-height: 20px;
padding-top: 50px;
}
</style>
</head>
<body>
<div class="aa">
<form method="post" action="chuli.php">
<div>用户名:<input type="text" name="username"/></div></br>
<div> &nbsp;&nbsp;密码:<input type="text" name="pwd"/></div></br>
<input type="submit" value="登录" />
</form>
</div>
</body>
</html>

2、登录页面的处理页面chuli.php

<?php
session_start(); //开始会话控制
$users = $_POST["username"];
$pwd = $_POST["pwd"];
require "DB.class.php";
$db = new DB();
$sql = "select password from yuangong where UserName = '{$users}'";
//echo $sql;
$arr = $db->query($sql);
//判断用户名和密码是否和数据库匹配
if(count($arr)){
if($arr[0][0]==$pwd && !empty($pwd)){
$_SESSION["username"]="$users"; //存取数据,便于后面的php页面调用
header("location:main.php");
}
}
?>

第三步主页面main.php

效果图:

用php(session)实现留言板功能----2017-05-09

注:如上图所示:“陈一“为登陆者,故此处为陈一,显示的都是陈一接收到的信息。

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title> </head>
<body>
<?php
//防止用户绕过登录
session_start();
if(empty($_SESSION["username"]))
{
header("location:denglu.php");
exit;
} require "DB.class.php";
$db = new DB();
$zr = $_SESSION["username"];
//显示登录者,并据此只显示接收者是登陆者的留言和群发的留言
$sql2 = "select name from yuangong where username ='{$zr}'";
$arr2 = $db->query($sql2);
$zr = $arr2[0][0]; //取出员工表中username ='{$zr}'的name
echo $zr.",请查看您的留言信息:";
?> <table border="1px" cellpadding="0px" cellspacing="0px" width="50%">
<tr> <td>发送人</td>
<td>接收人</td>
<td>发送时间</td>
<td>信息内容</td>
<td>是否已读</td>
</tr>
<?php //遍历输出属于登陆者的留言和群发消息
$sql ="select * from chenai where Receiver = '{$zr}' or receiver = 'all'";
$arr = $db->query($sql);
foreach($arr as $v){
//当为群发时,显示所有人,不显示all. 如果前面为汉字“所有人”,则不需要此步骤
if($v[2]=="all"){$v[2]="所有人";} // 实现状态的转变
//var_dump($v[5]);
$str = "";
if($v[5])
{
$str = "<span >已读</span>";
}
else{
$str = " <a href='zt-chuli.php?ids={$v[0]}'> 未读</a>";
} echo"<tr>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td value='{$v[4]}'> <a href='xx-chuli.php?ids={$v[0]}'> {$v[4]} </a></td>
<td>{$str}</td>
</tr>";
}
?>
</table>
<a href="fabu.php">返回留言页面</a> <a href="exit.php">退出系统</a>
</body>
</html>

第四步写留言页面:

1、发布页面fabu.php

效果图:

用php(session)实现留言板功能----2017-05-09用php(session)实现留言板功能----2017-05-09

实现功能:

下拉列表是陈一的好友

留言板内容不能为空!

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.aa{
height: 100px;
width: 300px;
margin: 0px auto;
}
.bb{
height: 200px;
width: 300px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="aa">
<h2>&nbsp;&nbsp;留言板</h2>
<?php
session_start();
if(empty($_SESSION["username"]))
{
header("location:denglu.php");
exit;
}
require "DB.class.php";
$db = new DB();
$username= $_SESSION["username"];
//获取发送者
$sql3 = "select name from yuangong where username='{$username}'";
$arr3 = $db->query($sql3);
$lyz= $arr3[0][0]; echo "欢 迎 ".$lyz.", 请 留 言!";
?>
</div> <div class="bb">
<form method="post" action="nr-chuli.php" id="bb">
<div>接收人:
<!-- 有问题,从用工表里读出的人并不一定都是我的好友,所以应该从friend表里读取属于我的好友,这样读出来的好友不包含我-->
<!--用下拉列表输出所有好友-->
<select name="receive">
<?php
$sql4 = "select Friend from friend where me='{$lyz}'";
echo $sql4;
$arr4 = $db->query($sql4);
var_dump($arr4); foreach($arr4 as $v){
echo"<option value='{$v[0]}'>{$v[0]}</option>";
}
?> <!--也可以直接把all换成所有人,后面就可以不用转换-->
<option value="all" value="all">All</option>
</select>
</div>
<div>留言:<br /><textarea name="nr" cols="30" rows="10" id="cc"></textarea></div>
<input type="submit" value="发送" onclick="panduan()" /></br>
<a href="denglu.php" >返回登录页面</a>
<a href="main.php">查看我的留言</a>
</form> </div>
</body>
</html>
<!--当留言为空时-->
<script>
function panduan(){
var a = document.getElementById("cc").value;
if(a=="")
{
//给提示
alert ("留言不能为空,您难道对您的好友这么无语么?哈哈请重新输入!");
//页面不跳转
document.getElementById("bb").removeAttribute("action");
}
}
</script>

2、发布处理页面:nr-chuli.php

<?php
session_start();
$_SESSION["username"];
if(empty($_SESSION["username"]))
{
header("location:denglu.php");
exit;
} $receive = $_POST["receive"];
$comment = $_POST["nr"];
$t=date("Y-m-d H:i:s"); $sender = $_SESSION["username"]; //var_dump($sender); require "DB.class.php";
$db = new DB(); //获取发送者
$sql = "select name from yuangong where username='{$sender}'";
$arr = $db->query($sql);
$sender = $arr[0][0];
//var_dump($arr); //往chenai表里添加信息
$sql2 = "insert into chenai values ('','$sender','$receive','$t','$comment',0)";
if($db->query($sql2,0))
{
echo "留言成功!";
}
echo " <a href='fabu.php'>继续留言</a> ";
echo " <a href='main.php'>查看我的留言</a> ";
?>

第五步:信息处理页面xx-chuli.php

用php(session)实现留言板功能----2017-05-09用php(session)实现留言板功能----2017-05-09

用php(session)实现留言板功能----2017-05-09

实现功能:点击最后一条留言内容,跳转到上面第二图,返回我的留言查看会发现最后一条信息变成已读

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style> .aa{
height: 200px;
width: 300px;
margin-left: 100px;
border: 1px solid black;
}
</style>
</head>
<body> <?php
session_start();
$ids = $_GET["ids"];
$zrname=$_SESSION["username"];
//防止用户绕过登录 if(empty($_SESSION["username"]))
{
header("location:denglu.php");
exit;
} //var_dump($ids);
//var_dump($_SESSION["username"]); require "DB.class.php";
$db =new DB();
//调取登陆者的姓名
$sql = "select name from yuangong where username = '{$zrname}'";
$arr = $db->query($sql);
$zrname=$arr[0][0];
//var_dump($zrname); //获取发送者的姓名
$sql2 = "select sender from chenai where ids='{$ids}'";
$arr2 = $db->query($sql2);
$name=$arr2[0][0];
//var_dump($name); echo $zrname.", 这是".$name."给您的留言:"."</br> </br>"; $sql3 = "select comment from chenai where ids='{$ids}'";
$arr3 = $db->query($sql3);
$nr=$arr3[0][0]; echo "<div class='aa'> {$nr} </div>"; //改变是否已读的状态
$sql4 = "update chenai set states=1 where ids='{$ids}'";
$db->query($sql4,0);
//echo "<a href='zt-chuli.php?ids={$ids}'>我的留言</a>";
// 这样也可以传值
//echo "<a href='fabu.php'>去留言</a>";
?>
<a href="main.php">我的留言</a> <a href="fabu.php">去留言</a>
</body>
</html>

如果不想查看内容,可直接点击未读也可以转成已读:zt-chuli.php

<?php
$ids = $_GET["ids"]; require "DB.class.php";
$db =new DB();
$sql = "update chenai set states=1 where ids='{$ids}'"; var_dump($db->query($sql,0));
if($db->query($sql,0)){
header("location:main.php");
} ?>

 第六步退出登录exit.php

<?php
session_start();
unset($_SESSION["username"]);
header("location:denglu.php");
?>

到这一步,一个简单的留言功能就可以实现了~~~~