当前位置:Gxlcms > PHP教程 > 如何用php简单制作留言板

如何用php简单制作留言板

时间:2021-07-01 10:21:17 帮助过:3人阅读

php实现留言板功能

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

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

1 首先是登录页面:

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

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

 1 <?php 
 2 session_start(); // 登录之后要把所包含登录的页面连接起来,开启session 
 3 include("DADB.class.php"); 
 4 
 5 $db=new DADB(); 
 6 
 7 $user=$_POST["username"]; 
 8 $pwd=$_POST["password"]; 
 9 
10 $sql="select password from yuangong where username='{$user}'";
11 
12 $arr=$db->Query($sql);
13 
14 if($arr[0][0]==$pwd && !empty($pwd))
15 {
16     $_SESSION["username"]=$user;
17     header("location:main.php");
18 }
19 else
20 {
21     echo"登录失败";
22 }
23 
24 ?>

如图所示,是登录页面

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把所有的登录情况下的页面连接起来

 1 <h1>发布信息</h1> 
 2 <a href="main.php">主页面</a> 
 3 <br /> 
 4 <br /> 
 5 
 6 <?php 
 7 session_start(); 
 8 if(empty($_SESSION["username"])) 
 9 {
 10     header("location:login.php");
 11     exit;12 }13 
14 $user = $_SESSION["username"];
15 
16 include("DADB.class.php");
17 $db = new DADB();
18 
19 $shaoyou = "select * from firend where me='{$user}'";
20 $ahaoyou = $db->Query($shaoyou);
21 
22 ?>
23 
24 <form action="fabuchuli.php" method="post">
25     <p>接收人:
26         <select name="jsr">
27             <option value="all">所有人</option>
28             <?php
29             foreach($ahaoyou as $v)
30             {
31                 $name = uname($v[2]);
32                 echo "<option value='{$v[2]}'>{$name}</option>";
33             }
34             ?>
35         </select>
36     </p>
37     <br />
38     <p
39         信息内容:<textarea name="neirong"></textarea>
40     </p><br />
41     <input type="submit" value="发送" />
42 </form>
43 <?php
44 function uname($user)
45 {
46     global $db;
47 
48     if($user=="all")
49     {
50         return "所有人";
51     }
52     else
53     {
54         $sql1 = "select name from yuangong where username='{$user}'";
55         $att = $db->Query($sql1);
56 
57         return $att[0][0];
58     }
59 }
60 ?>
61 </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简单制作留言板的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行