PHP 留言板练习
登录页面同session一样
login页面 <form action="loginchuli.php" method="post"> <div>用户名:<input type="text" name="uid" /></div> <div>密码:<input type="text" name="pwd" /></div> <input type="submit" value="登录" /> </form> loginchuli页面 <?php session_start(); $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; include("../DBDA.php"); $db = new DBDA(); $sql = "select count(*) from yuangong where username='{$uid}' and password = '{$pwd}'"; $z = $db->StrQuery($sql); if($z == 1) { $_SESSION["uid"] = $uid; header("location:main.php"); } else { header("location:login.php"); }
fabu页面
<?php session_start(); if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } $uid = $_SESSION["uid"]; include("../DBDA.php"); $db = new DBDA(); ?> <br /><br /> <a href="main.php">查看信息</a> <a href="tuichu.php">退出系统</a> <br /> <h2>信息发送</h2> <form action="fabuchuli.php" method="post"> <div>接收人: <select name="jieshou"> <option value="suoyou">所有人</option> <?php $sql = "select firend from firend where me='{$uid}'"; $attr = $db->Query($sql); foreach($attr as $v) { $sqln = "select Name from yuangong where username='{$v[0]}'"; $name = $db->StrQuery($sqln); echo "<option value='{$v[0]}'>{$name}</option>"; } ?> </select> </div> <br /> <div> 信息内容: <textarea name="neirong"> </textarea> </div> <br /> <div> <input type="submit" value="发送" /> <input type="reset" value="复位" /> </div> </form>
fabuchuli页面
<?php session_start(); include("../DBDA.php"); $db = new DBDA(); $jieshou = $_POST["jieshou"]; $neirong = $_POST["neirong"]; $uid = $_SESSION["uid"]; $time = date("Y-m-d H:i:s"); $sql = "insert into liuyan values('','{$uid}','{$jieshou}','{$time}','{$neirong}',false)"; if($db->Query($sql,0)) { header("location:fabu.php"); } else { echo "发布失败!"; }
main页面
<a href="fabu.php">发布信息</a> <a href="tuichu.php">退出系统</a> <br /> <br /> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>发送人</td> <td>发送时间</td> <td>接收人</td> <td>信息内容</td> </tr> <?php session_start(); if(empty($_SESSION["uid"])) { header("location:login.php"); exit; } $uid = $_SESSION["uid"]; include("../DBDA.php"); $db = new DBDA(); $sql = "select * from liuyan where recever='{$uid}' or recever='suoyou'"; $attr = $db->Query($sql); foreach($attr as $v) { //处理发送者姓名 $sqlname = "select Name from yuangong where username='{$v[1]}'"; $name = $db->StrQuery($sqlname); //处理接收者姓名 $jieshou = ""; if($v[2]=="suoyou") { $jieshou = "所有人"; } else { $sqln = "select Name from yuangong where username='{$uid}'"; $jieshou = $db->StrQuery($sqln); } echo "<tr> <td>{$name}</td> <td>{$v[3]}</td> <td>{$jieshou}</td> <td>{$v[4]}</td> </tr>"; } ?> </table>
tuichu页面
<?php session_start(); unset($_SESSION["uid"]); header("location:login.php");