数据库留言板例题:session和cookie区别

2023-03-11,,

session和cookie区别

<?php
session_start();      //session_start();必须写在所有的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>

1.session
(1)session存储在服务器的
(2)session每个人存一份
(3)session有默认的过期时间
(4)session里面可以存储任意类型的数据
安全,对服务造成压力
用法:
1.当一个页面需要使用session的时候,需要在页面顶部加session_start();
2.操作session
    赋值  $_SEESION["uid"] = "";
    取值  $_SESSINO["uid"]

2.cookie
(1)cookie存储在客户端的
(2)cookie每人存一份
(3)cookie没有默认过期时间
(4)cookie只能存储字符串
不安全,不会对服务器造成压力
用法:
1.操作cookie
    赋值:setcookie(key,value)
    取值:$_COOKIE["uid"]

<?php
    $_SESSION["uid"] = "zhangsan";

    setcookie("uid","lisi");
?>

</body>
</html>
<?php
    session_starts();
    if(empty(session["uid"]))
    {
        hrader("location:页面.php");                             //此方法可设定用户必须走登录界面
        exit;
    }

留言板例题

登录页面:

<style type="text/css">
.a{ float:left}
.b{ margin-left:70px}
</style>
</head>
<body>
<form action="uidcl.php" method="post">
<table width="45%"  border="1">
    <tr><td colspan="2"><h1>开发部内部留言板</h1></td></tr>
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="uid" /></td>
    </tr>
    <tr>
        <td>口令:</td>
        <td><input type="password" name="pwd" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <div class="b">
            <input type="submit" value="登录" />
            <input type="reset" value="复位" />
            </div>
        </td>
    </tr>
</table>
</form>
</body>

登录处理页面:session_starts();缓存用户名方式,在添加信息处理页面和主页面提取

<?php
session_start();
?>
<?php
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];
include("../fzl/czy.class.php");
$db = new czy();
$sql = "select password from yuangong where username = '{$uid}'";
$attr = $db->strQuery($sql);
//var_dump($attr);
if($pwd == $attr && !empty($pwd))
{
    $_SESSION["uid"]=$uid;
    header("location:main.php");
}
else
{
    echo "用户名或密码错误";
}
?>

主页面:查询:-----

<body>
<?php
session_start();
?>
<table width="40%">
<tr>
    <td><a href = "maincl.php"><div class="a"><h3>发布信息</h3></div></a></td>
    <td><a href="uid.php"><div class="a"><h3>退出系统</h3></div></a></td>
</tr>
<tr>
    <td><h4>留言信息:</h4></td>
</tr>
</table>
<form action="main.php" method="post">
<table width="80%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>发送人</td>
    <td>发送时间</td>
    <td>接受人</td>
    <td>信息内容</td>
</tr>
<?php
$ss = "";
if(!empty($_POST["ss"]))
{
    $ss = $_POST["ss"];
}
$uid = $_SESSION["uid"];
include("../fzl/czy.class.php");
$db = new czy();
$tj = " 1=1 ";
if($_SESSION["uid"] != "" && $_POST["ss"] != "")
{
    $tj = " sender = '{$uid}' or recever = '{$uid}' ";
}
$sql = "select * from liuyan where ".$tj;
$attr = $db->Query($sql);
foreach($attr as $v)
{
    echo "<tr>
            <td>{$v[1]}</td>
          <td>{$v[3]}</td>
          <td>{$v[2]}</td>
          <td>{$v[4]}</td>
          </td>";    
}
?>
</table>
<div><input id="bb" type="text" name="ss" value="<?php echo "{$uid}" ?>" /><input type="submit" value="查询信息" />
<input type="hidden" name="ss1"  /><input type="button" onclick="show(this)" value="清除显示id" /></div>
</form>
</div>
<script type="text/javascript">
function show(a)
{
    document.getElementById("bb").value="";    
}
</script>
</body>

添加信息页面:

<body>
<table width="300">
<tr>
    <td><h3><a href="main.php">查看信息</a></h3></td>
    <td><h3><a href="uid.php">退出系统</a></h3></td>
</tr>
<tr>
    <td><h4>信息发送:</h4></td>
</tr>
</table>
<form action="chuli.php" method="post">
<table width="300" border="1" >
<tr>
    <td>接收人:</td>
    <td><input type="text" name="xm" /></td>
</tr>
<tr>
    <td>信息内容:</td>
    <td><textarea name="nr"></textarea></td>
</tr>
<tr>
    <td colspan="2">
        <div style="margin-left:85px">
            <input type="submit" value="发送" />
            <input type="reset" value="复位" />
        </div>
    </td>
</tr>
</table>
</form>
</body>

添加信息处理页面:

<?php
session_start();
?>
<body>
<?php
$sj = date("Y-m-d H:i:s");
$xm = $_POST["xm"];
$nr = $_POST["nr"];
$uid = $_SESSION["uid"];
include("../fzl/czy.class.php");
$db = new czy();
$sql = " insert into liuyan values ('','$uid','$xm','{$sj}','$nr','') ";
$n = $db->Query($sql,0);
if($n)
{
    header("location:main.php");
}
else
{
    echo "失败";
}
?>

数据库留言板例题:session和cookie区别的相关教程结束。

《数据库留言板例题:session和cookie区别.doc》

下载本文的Word格式文档,以方便收藏与打印。