Cookie和Session技术

<?php

//服务器端执行的,浏览器不能识别PHP标签

if(! isset($_COOKIE['visits'])) {

    $_COOKIE['visits'] = 0;

}

$visits = $_COOKIE['visits'] + 1;

setcookie('visits',$visits,time() + 3600 * 24 * 365);//有效期是一年

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>例6.8 COOKIE的使用</title>

</head>

<body>    

    <h4>COOKIE的使用</h4><hr/>

    <p>这是您的第<?php echo $visits ?>次登录,感谢您的关注!

    <hr/><a href="example6_8_1.php">进入另外页面</a>    

    <a href="example6_8_2.php">重新统计登录次数</a>

</body>

</html>

 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>例6.8 COOKIE的使用</title>

</head>

<body>

    <h4>COOKIE的使用</h4><hr/>

    <p>这里是页面内容!

    <hr/>

    <p>登录次数统计:<?php echo $_COOKIE['visits'] ?>次

</body>

</html>

<?php

if (isset($_COOKIE['visits'])) {

    setcookie('visits','',time()-1);

}

header('location:example6_8.php');

?>

 

<?php

    $items = array(

        array('id'=>'1','name'=>'PHP Web应用开发教程','price'=>'50'),

        array('id'=>'2','name'=>'面向对象程序设计','price'=>'40'),

        array('id'=>'3','name'=>'Visual C++程序设计','price'=>'30'),//下标为2

        // $_SESSION['goods']=$items;

        // $_SESSION['goods'][2];访问下标为2的数组

        // $_SESSION['goods'][2]['name'];访问'Visual C++程序设计'

        array('id'=>'4','name'=>'数据结构与应用教程','price'=>'35')

    );

    session_start();

    if (!isset($_SESSION['cart'])) {

        $_SESSION['cart'] = array();

    }

    if (isset($_POST['action']) && $_POST['action'] == '购买') {

        $_SESSION['cart'][] = $_POST['id'];

    }

    if (isset($_POST['action']) && $_POST['action'] == '清空购物车') {

        // $_SESSION['cart']=array();

        $_SESSION['cart']=[];

    }

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>例6.9 SESSION的使用</title>

    <style type="text/css">

        table{border-collapse:collapse}

        td,th{border:1px solid black;text-align:center}

    </style>

</head>

<body>

    <p>您的购物车包含<?php echo count($_SESSION['cart']); ?>项商品!</p>

    <?php if (count($_SESSION['cart']) != 0):?>

    <p><a href="example6_9_1.php?cart">显示我的购物车</a></p>

    <?php else: ?>

    <p>请您选购商品</p  >

    <?php endif; ?>

    <table>

        <thead>

            <tr><th>商品名称</th><th>价格(元)</th></tr>

        </thead>

        <tbody>

        <?php foreach ($items as $item): ?>

            <tr>

                <td><?php echo $item['name'];?></td>

                <td><?php echo $item['price'];?></td>

                <td>

                    <form action="" method="post">

                        <div>

                            <input type="hidden" name="id"

                                value="<?php echo $item['id']; ?>" />

                            <input type="submit" name="action" value="购买" />

                        </div>

                    </form>

                </td>

            </tr>

        <?php endforeach;?>

        </tbody>

    </table>

    <p>注:价格单位为人民币。</p>

</body>

</html>

 

<?php

    $items = array(

        array('id'=>'1','name'=>'PHP Web应用开发教程','price'=>'50'),

        array('id'=>'2','name'=>'面向对象程序设计','price'=>'40'),

        array('id'=>'3','name'=>'Visual C++程序设计','price'=>'30'),

        array('id'=>'4','name'=>'数据结构与应用教程','price'=>'35')

    );

    session_start();

    $cart=array();

    if (isset($_SESSION['cart'])) {        

        $total = 0;

        foreach ($_SESSION['cart'] as $id) {

            foreach ($items as $product) {

                if ($product['id'] == $id) {

                    $cart[] = $product;

                    $total += $product['price'];

                    break;

                }

            }

        }

    }

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>例6.9 SESSION的使用</title>

    <style type="text/css">

        table{border-collapse:collapse}

        td,th{border:1px solid black;text-align:center}

    </style>

</head>

<body>

    <h3>您的购物车</h3>

    <?php if(count($cart) > 0): ?>

    <table>

        <thead>

            <tr><th>商品名称</th><th>价格(元)</th></tr>

        </thead>

        <tbody>

        <?php foreach ($cart as $item): ?>

            <tr>

                <td><?php echo $item['name']; ?></td>

                <td><?php echo $item['price']; ?></td>

            </tr>

        <?php endforeach;?>

        </tbody>

        <tfoot>

            <tr>

                <td>总计:</td>

                <td><?php echo $total; ?></td>

            </tr>

        </tfoot>

    </table>

    <?php else: ?>

    <p>您的购物车为空!</p>

    <?php endif; ?>

    <form action="example6_9.php" method="post">

        <p>

            <a href="example6_9.php">继续购物</a>&nbsp;&nbsp;或者&nbsp;&nbsp;

            <input type="submit" name="action" value="清空购物车"/>

        </p>

    </form>

</body>

</html>

 

 

 

 

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值