首頁 >後端開發 >php教程 >php基於會話控制實作留言板功能

php基於會話控制實作留言板功能

墨辰丷
墨辰丷原創
2018-05-22 11:37:452480瀏覽

這篇文章主要為大家詳細介紹了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

<?php
session_start();
$UserName = $_POST["UserName"];
$PassWord = $_POST["PassWord"];

require "DBDA.class1.php";
$db = new DBDA();
$sql = "select PassWord from yuangong where UserName = &#39;{$UserName}&#39;";
$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>
<?php
session_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=&#39;{$UserName}&#39; or Recever=&#39;suoyou&#39;";
  $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

##
<?php
session_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 = &#39;{$UserName}&#39;";
  $arr = $db->query($sql);
    
  foreach($arr as $v)
  {
    echo "<option value=&#39;{$v[0]}&#39;>{$v[1]}</option>";
  }
  //方法二
  /*$sql = "select Friend from friend where Me =&#39;{$UserName}&#39;";
  $arr = $db->query($sql);
  foreach($arr as $v)
    {
      $v[0];
      $sname = "select Name from yuangong where UserName = &#39;{$v[0]}&#39;";
      $aname = $db->query($sname);
      echo"<option value=&#39;{$v[0]}&#39;>{$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

<?php
session_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(&#39;&#39;,&#39;{$UserName}&#39;,&#39;{$jsr}&#39;,&#39;{$Times}&#39;,&#39;{$nr}&#39;)";
$db->query($sql,0);
header("location:fabu.php");
相關推薦:

##簡單實現PHP

留言板

功能

###php開發###留言板###的增,刪,改,查操作####### #########php實作###留言板###功能(會話控制)########################## #

以上是php基於會話控制實作留言板功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn