当前位置:Gxlcms > PHP教程 > php基于会话控制实现留言板功能

php基于会话控制实现留言板功能

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

这篇文章主要为大家详细介绍了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. <body>
  8. <h1>开发部内部留言板</h1>
  9. <form action="login.php" method="post">
  10. <p>用户名:<input type="text" name="UserName" /></p>
  11. <p>口令:<input type="password" name="PassWord" /></p>
  12. <input type="submit" value="登录" />
  13. <a href="denglu.php" style="text-decoration:none"><input type="button" value="复位" /></a>
  14. </form>
  15. </body>
  16. </html>

2.login.php

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

二.主界面(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. <body>
  8. <?php
  9. session_start();
  10. // 防止绕过登陆直接进入主界面
  11. if(empty($_SESSION["UserName"]))
  12. {
  13. header("location:denglu.php");
  14. exit;
  15. }
  16. require "DBDA.class1.php";
  17. $db = new DBDA();
  18. $UserName = $_SESSION["UserName"];
  19. ?>
  20. <p>
  21. <a href="fabu.php">发布信息</a>
  22. <a href="tuichu.php">退出系统</a>
  23. </p><br /><br />
  24. <h1>留言信息:</h1>
  25. <table width="100%" border="1" >
  26. <tr>
  27. <td>发送人</td>
  28. <td>发送时间</td>
  29. <td>接收人</td>
  30. <td>信息内容</td>
  31. </tr>
  32. <?php
  33. //显示接收者是我的,或者是所有人的
  34. $sql = "select * from liuyan where Recever='{$UserName}' or Recever='suoyou'";
  35. $arr = $db->query($sql);
  36. foreach($arr as $v)
  37. {
  38. echo "<tr>
  39. <td>{$v[1]}</td>
  40. <td>{$v[3]}</td>
  41. <td>{$v[2]}</td>
  42. <td>{$v[4]}</td>
  43. </tr>";
  44. }
  45. ?>
  46. </table>
  47. </body>
  48. </html>

2.tuichu.php

  1. <?php
  2. session_start();
  3. unset($_SESSION["UserName"]);
  4. 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. <body>
  8. <p>
  9. <a href="main.php">查看信息</a>
  10. <a href="tuichu.php">退出系统</a>
  11. </p>
  12. <h1>信息发送:</h1>
  13. <form action="fabuchuli.php" method="post">
  14. <p>接收人:
  15. <select name="jsr">
  16. <option value="suoyou">所有人</option>
  17. <?php
  18. session_start();
  19. $UserName = $_SESSION["UserName"];
  20. require"DBDA.class1.php";
  21. $db = new DBDA();
  22. //方法一
  23. $sql = "select friend.Friend,yuangong.Name from friend,yuangong where friend.Friend = yuangong.UserName and friend.Me = '{$UserName}'";
  24. $arr = $db->query($sql);
  25. foreach($arr as $v)
  26. {
  27. echo "<option value='{$v[0]}'>{$v[1]}</option>";
  28. }
  29. //方法二
  30. /*$sql = "select Friend from friend where Me ='{$UserName}'";
  31. $arr = $db->query($sql);
  32. foreach($arr as $v)
  33. {
  34. $v[0];
  35. $sname = "select Name from yuangong where UserName = '{$v[0]}'";
  36. $aname = $db->query($sname);
  37. echo"<option value='{$v[0]}'>{$aname[0][0]}</option>";
  38. }*/
  39. ?>
  40. </select></p>
  41. <p>信息内容:<textarea name="neirong"></textarea></p>
  42. <input type="submit" value="发送" />
  43. <a href="fabu.php" style="text-decoration:none"><input type="button" value="复位" /></a>
  44. </form>
  45. </body>
  46. </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. require"DBDA.class.php";
  8. $db = new DBDA();
  9. $sql = "insert into liuyan values('','{$UserName}','{$jsr}','{$Times}','{$nr}')";
  10. $db->query($sql,0);
  11. header("location:fabu.php");

相关推荐:

简单实现PHP留言板功能

php开发留言板的增,删,改,查操作

php实现留言板功能(会话控制)

以上就是php基于会话控制实现留言板功能的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行