這篇文章主要為大家詳細介紹了php實現留言板功能,會話控制的案例,具有一定的參考價值,感興趣的小夥伴們可以參考一下
具體內容如下
資料庫用到的三張表
一.登入介面 (denglu.php login.php)
1.denglu.php
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body><h1>開發部內部留言板</h1><form action="login.php" method="post"><p>使用者名稱:<input type="text" name="UserName" /></p><p>口令:<input type="password" name="PassWord" /></p><input type="submit" value="登入" /><a href="denglu.php" style="text-decoration:none"><input type="button" value="複位" /></a></form></body></html>
2.login.php
<?phpsession_start();$UserName = $_POST["UserName"];$PassWord = $_POST["PassWord"];require "DBDA.class1.php";$db = new DBDA();$sql = "select PassWord from yuangong where UserName = '{$UserName}'";$arr = $db->query($sql);if(count($arr)){ if($arr[0][0] == $PassWord && !empty($PassWord)) { //儲存使用者名稱 $_SESSION["UserName"] = $UserName; header("location:main.php"); }}else{ header("location:denglu.php");}
二.主介面(main.php tuichu.php)
1.main.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><?phpsession_start(); // 防止繞過登陸直接進入主介面if(empty($_SESSION["UserName"])){ header("location:denglu.php"); exit;} require "DBDA.class1.php";$db = new DBDA();$UserName = $_SESSION["UserName"];?><p><a href="fabu.php">發布資訊</a><a href="tuichu.php">退出系統</a></p><br /><br /><h1>留言資訊:</h1><table width="100%" border="1" > <tr> <td>發送人</td> <td>發送時間</td> <td>接收人</td> <td>資訊內容</td> </tr> <?php //顯示接收者是我的,或者是所有人的 $sql = "select * from liuyan where Recever='{$UserName}' or Recever='suoyou'"; $arr = $db->query($sql); foreach($arr as $v) { echo "<tr> <td>{$v[1]}</td> <td>{$v[3]}</td> <td>{$v[2]}</td> <td>{$v[4]}</td> </tr>"; } ?> </table></body></html>
2.tuichu.php
<?phpsession_start();unset($_SESSION["UserName"]);header("location:denglu.php");
三.發送頁面(fabu.php fabuchuli.php)
1.fabu.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><p><a href="main.php">查看資訊</a><a href="tuichu.php">退出系統</a></p><h1>資訊發送:</h1><form action="fabuchuli.php" method="post"><p>接收人:<select name="jsr"> <option value="suoyou">所有人</option> <?php session_start(); $UserName = $_SESSION["UserName"]; require"DBDA.class1.php"; $db = new DBDA(); //方法一 $sql = "select friend.Friend,yuangong.Name from friend,yuangong where friend.Friend = yuangong.UserName and friend.Me = '{$UserName}'"; $arr = $db->query($sql); foreach($arr as $v) { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } //方法二 /*$sql = "select Friend from friend where Me ='{$UserName}'"; $arr = $db->query($sql); foreach($arr as $v) { $v[0]; $sname = "select Name from yuangong where UserName = '{$v[0]}'"; $aname = $db->query($sname); echo"<option value='{$v[0]}'>{$aname[0][0]}</option>"; }*/ ?> </select></p><p>資訊內容:<textarea name="neirong"></textarea></p><input type="submit" value="發送" /><a href="fabu.php" style="text-decoration:none"><input type="button" value="複位" /></a></form></body></html>
2.fabuchuli.php
<?phpsession_start();$UserName = $_SESSION["UserName"];$jsr = $_POST["jsr"];$nr = $_POST["neirong"];$Times = date("Y-m-d H:i:s");require"DBDA.class.php";$db = new DBDA();$sql = "insert into liuyan values('','{$UserName}','{$jsr}','{$Times}','{$nr}')";$db->query($sql,0);header("location:fabu.php");