php实现留言板功能的代码详细介绍

黄舟
黄舟 原创
2023-03-06 12:12:01 18175浏览

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

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

1 首先是登录页面:

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

</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"登录失败";
}

?>

如图所示,是登录页面

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

<body>
<p><h3><a href="fabu.php">发布信息</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="tuichu.php">退出系统</a></h3> </p>
<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">主页面</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">
    <p>接收人:
        <select name="jsr">
            <option value="all">所有人</option>
            <?php
            foreach($ahaoyou as $v)
            {
                $name = uname($v[2]);
                echo "<option value='{$v[2]}'>{$name}</option>";
            }
            ?>
        </select>
    </p>
    <br />
    <p>
        信息内容:<textarea name="neirong"></textarea>
    </p><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实现留言板功能的代码详细介绍的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。