php会话控制例题:留言板

数据库用到的三张表

一.登录界面 (denglu.php   login.php)

1.denglu.php

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title></title>
 6 </head>
 7 
 8 <body>
 9 <h1>开发部内部留言板</h1>
10 <form action="login.php" method="post">
11 <div>用户名:<input type="text" name="UserName" /></div>
12 <div>口令:<input type="password" name="PassWord" /></div>
13 <input type="submit" value="登录" />
14 <a href="denglu.php" style="text-decoration:none"><input type="button" value="复位" /></a>
15 </form>
16 </body>
17 </html>

2.login.php

 1 <?php
 2 session_start();
 3 $UserName = $_POST["UserName"];
 4 $PassWord = $_POST["PassWord"];
 5 
 6 require "DBDA.class1.php";
 7 $db = new DBDA();
 8 $sql = "select PassWord from yuangong where UserName = '{$UserName}'";
 9 $arr = $db->query($sql);
10 
11 if(count($arr))
12 {
13     if($arr[0][0] == $PassWord && !empty($PassWord))
14     {
15         //存储用户名
16         $_SESSION["UserName"] = $UserName;
17         
18         header("location:main.php");
19     }
20 }
21 else
22 {
23     header("location:denglu.php");
24 }

二.主界面 (main.php   tuichu.php)

1.main.php

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>无标题文档</title>
 6 </head>
 7 
 8 <body>
 9 <?php
10 session_start();
11     
12 //    防止绕过登陆直接进入主界面
13 if(empty($_SESSION["UserName"]))
14 {
15     header("location:denglu.php");
16     exit;
17 }
18     
19 require "DBDA.class1.php";
20 $db = new DBDA();
21 $UserName = $_SESSION["UserName"];
22 ?>
23 <div>
24 <a href="fabu.php">发布信息</a>
25 <a href="tuichu.php">退出系统</a>
26 </div><br /><br />
27 <h1>留言信息:</h1>
28 <table width="100%" border="1" >
29     <tr>
30         <td>发送人</td>
31         <td>发送时间</td>
32         <td>接收人</td>
33         <td>信息内容</td>
34     </tr>
35       <?php
36     
37     //显示接收者是我的,或者是所有人的
38     $sql = "select * from liuyan where Recever='{$UserName}' or Recever='suoyou'";
39     $arr = $db->query($sql);
40     foreach($arr as $v)
41     {
42     
43          echo "<tr>
44                    <td>{$v[1]}</td>
45                 <td>{$v[3]}</td>
46                 <td>{$v[2]}</td>
47                 <td>{$v[4]}</td>
48                </tr>";
49     }
50     
51     ?>    
52 
53 </table>
54 </body>
55 </html>

2.tuichu.php

1 <?php
2 
3 session_start();
4 
5 unset($_SESSION["UserName"]);
6 
7 header("location:denglu.php");

三.发送页面 (fabu.php   fabuchuli.php)

1.fabu.php

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>无标题文档</title>
 6 </head>
 7 
 8 <body>
 9 <div>
10 <a href="main.php">查看信息</a>
11 <a href="tuichu.php">退出系统</a>
12 </div>
13 <h1>信息发送:</h1>
14 <form action="fabuchuli.php" method="post">
15 <div>接收人:
16 <select name="jsr">
17     <option value="suoyou">所有人</option>
18     <?php
19     session_start();
20     $UserName = $_SESSION["UserName"];
21     require"DBDA.class1.php";
22     $db = new DBDA();
23     //方法一
24     $sql = "select friend.Friend,yuangong.Name from friend,yuangong where friend.Friend = yuangong.UserName and friend.Me = '{$UserName}'";
25     $arr = $db->query($sql);
26         
27     foreach($arr as $v)
28     {
29         echo "<option value='{$v[0]}'>{$v[1]}</option>";
30     }
31     //方法二
32     /*$sql = "select Friend from friend where Me ='{$UserName}'";
33     $arr = $db->query($sql);
34     foreach($arr as $v)
35         {
36             $v[0];
37             $sname = "select Name from yuangong where UserName = '{$v[0]}'";
38             $aname = $db->query($sname);
39             echo"<option value='{$v[0]}'>{$aname[0][0]}</option>";
40         }*/
41     ?>                
42 </select></div>
43 <div>信息内容:<textarea name="neirong"></textarea></div>
44 <input type="submit" value="发送" />
45 <a href="fabu.php" style="text-decoration:none"><input type="button" value="复位" /></a>
46 </form>
47 </body>
48 </html>

2.fabuchuli.php

 1 <?php
 2 session_start();
 3 $UserName = $_SESSION["UserName"];
 4 $jsr = $_POST["jsr"];
 5 $nr = $_POST["neirong"];
 6 $Times = date("Y-m-d  H:i:s");
 7 
 8 
 9 require"DBDA.class.php";
10 $db = new DBDA();
11 $sql = "insert into liuyan values('','{$UserName}','{$jsr}','{$Times}','{$nr}')";
12 $db->query($sql,0);
13 header("location:fabu.php");
原文地址:https://www.cnblogs.com/zhaohui123/p/6829058.html