zoukankan      html  css  js  c++  java
  • session 的用法

    </head>
    
    <body>
    <?php
    //session_start();//开启session,必须写在PHP代码最顶端
    
    
    //HTTP,无状态性
    //记录登陆者状态:SESSION COOKIE
    
    
    //SESSION:存储在服务端的,每个人存一份,可以存储任意类型的数据,默认过期时间15分钟,(安全,但是,服务器压力大)
    //COOKIE:存储在客户端的,每个人存一份,只能存储字符串,默认永不过期,(安全性低,很少用)
    
    
    $_SESSION["uid"]="zhangsan";//写入SESSION,在服务器中开辟一个空间 uid ,
    echo $_SESSION["uid"];
    
    
    /*setcookie("uid","zhangsan");//设置COOKIE,不常用
    echo $_COOKIE("uid");
    */
    
    ?>
    <a href="test1.php">跳转</a>
    
    zaq123
    
    
    </body>
    </html>

    做水果购物的购物车

    showlist.php

    <body>
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>水果名称</td>
        <td>水果价格</td>
        <td>水果产地</td>
        <td>水果库存</td>
        <td>操作</td>
    </tr>
    <?php
    
    include("../DBDA.php");
    $db = new DBDA();
    
    $sql = "select * from fruit";
    
    $attr = $db->Query($sql);
    
    foreach($attr as $v)
    {
        echo "<tr><td>{$v[1]}</td>
        <td>{$v[2]}</td>
        <td>{$v[3]}</td>
        <td>{$v[4]}</td>
        <td><a href='addgwc.php?code={$v[0]}'>加入购物车</a></td></tr>";
    }
    
    ?>
    </table>
    
    <a href="gouwuche.php">查看购物车</a>
    </body>
    </html>

    addgwc.php

    <?php
    session_start();//必须在顶部
    
    $code = $_GET["code"];
    
    //思路:存入数据,是否是第一次,如果是,操作。。。如果不是,是否已经存在,存入。。。
    //如果第一次点击
    if(empty($_SESSION["sg"]))
    {
        $attr = array(array($code,1));
        $_SESSION["sg"] = $attr;
    }
    else
    {
        
    //第n次点击,n!=1
        $attr = $_SESSION["sg"];
        
        //判断该水果是否已经存在
        if(iscunzai($code))
        {
            foreach($attr as $k=>$v)
            {
                if($v[0]==$code)
                {
                    $attr[$k][1] = $v[1]+1;
                }
            }
            
            $_SESSION["sg"] = $attr;
        }
        else
        {
            $arr = array($code,1);
            array_push($attr,$arr);
            
            $_SESSION["sg"] = $attr;
        }
    
    }
    
    //判断是否存在的函数
    function iscunzai($c)
    {
        $attr = $_SESSION["sg"];
        
        $b = false;
        
        foreach($attr as $v)
        {
            $b = $b || in_array($c,$v);
        }
        
        return $b;
    }
    
    header("location:showlist.php");

    gouwuche.php

    <body>
    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>水果名称</td>
        <td>水果价格</td>
        <td>数量</td>
    </tr>
    <?php
    session_start();
    
    include("../DBDA.php");
    $db = new DBDA();
    
    $attr = $_SESSION["sg"];
    
    foreach($attr as $v)
    {
        $sql = "select Name,Price from fruit where Ids='{$v[0]}'";
        
        $arr = $db->Query($sql);
        
        echo "<tr>
            <td>{$arr[0][0]}</td>
            <td>{$arr[0][1]}</td>
            <td>{$v[1]}</td>
            </tr>";
    }
    
    
    ?>
    </table>
    </body>
    </html>

  • 相关阅读:
    linux centos 常用命令(需掌握)
    centos轻松搭建NFS
    Centos6.1在yum安装软件的时候,居然报错了,如何解决
    安装好Centos后,需要做的几件事情。
    使用scp命令传输文件
    批量删除文件或者批量修改文件
    Centos7搭建常用的LNMP架构
    python实现自动抠名字签名,比PS还快
    inotify软件实现实时同步,ssh-key 秘钥连接方式,saltstack实战批量管理Linux,expect批量分发秘钥
    Cisco 路由器配置OSPF 动态路由 (开放式最短路径优先)
  • 原文地址:https://www.cnblogs.com/wanlibingfeng/p/5517231.html
Copyright © 2011-2022 走看看