搜索
首页后端开发php教程详细介绍PHP session实现购物车的实例教程

这篇文章主要为大家详细介绍了PHP session实现购物车功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在wamp环境下,用PHP的session会话控制完成购物车的效果,数据存放在数组里练习,没有连接数据库,效果不错,简单易懂,以下是各部分的代码

common.php


<?php
header("content-type:text/html;charset=utf-8");
$arrPro = array(
    array(&#39;id&#39;=>1,&#39;img&#39;=>&#39;img/1.jpg&#39;,&#39;title&#39;=>&#39;小米移动电源5000mAh&#39;,&#39;price&#39;=>49),
    array(&#39;id&#39;=>2,&#39;img&#39;=>&#39;img/2.jpg&#39;,&#39;title&#39;=>&#39;20000mAh小米移动电源2&#39;,&#39;price&#39;=>149),
    array(&#39;id&#39;=>3,&#39;img&#39;=>&#39;img/3.jpg&#39;,&#39;title&#39;=>&#39;小米圈铁耳机Pro&#39;,&#39;price&#39;=>129),
    array(&#39;id&#39;=>4,&#39;img&#39;=>&#39;img/4.jpg&#39;,&#39;title&#39;=>&#39;小米家电动滑板车&#39;,&#39;price&#39;=>1999),
    array(&#39;id&#39;=>5,&#39;img&#39;=>&#39;img/5.jpg&#39;,&#39;title&#39;=>&#39;小米笔记本&#39;,&#39;price&#39;=>3499),
    array(&#39;id&#39;=>6,&#39;img&#39;=>&#39;img/6.jpg&#39;,&#39;title&#39;=>&#39;米家LED智能台灯&#39;,&#39;price&#39;=>169),
    array(&#39;id&#39;=>7,&#39;img&#39;=>&#39;img/7.jpg&#39;,&#39;title&#39;=>&#39;小米体重秤&#39;,&#39;price&#39;=>99),
    array(&#39;id&#39;=>8,&#39;img&#39;=>&#39;img/8.png&#39;,&#39;title&#39;=>&#39;小米电视3s 48英寸&#39;,&#39;price&#39;=>2599)
  );

index.php


<?php 
header("content-type:text/html;charset=utf-8");
require &#39;common.php&#39;;
session_start();
$sum = 0;
$class = "";
//判断左上角购物车的样式显示
if(!empty($_SESSION[&#39;shopcar&#39;])){
  $data = $_SESSION[&#39;shopcar&#39;];
  $sum = array_sum($data[4]);
  $class = "on";
  //右上角圆点
    if(empty($data[0])){
      $class = "";
    }
  }
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>商品展示</title>
  <style type="text/css">
  section{
    width:1032px;
    height: 700px;
    margin: 40px auto;
  }
  .top{
    float: right;
    position: relative;
    width: 190px;
    height: 34px;
    border: 1px solid #ccc;
    margin-right: 32px;
    text-align: center;
    line-height: 34px;
    border-radius: 4px;
    transition: all .3s linear;
    -moz-transition: all .3s linear;
    -webkit-transition: all .3s linear;
  }
  .top a{
    color: #d00;
  }
  .top:hover{
    width: 210px;
    color: #fff;
    font-weight: bold;
    background-color: #d00;
    border-radius: 6px;
  }
  .top:hover a{
    color: #fff;
  }
  .top:hover span{
    background-color: #fff;
    color: #d00;
  }
  .top:hover .star{
    right:150px;
    top: 0;
    font-size: 14px;
    color: #ff0;
    transform: rotate(1080deg);
  }
  .top span{
    position: absolute;
    top:2px;
    right: 40px;
    width: 18px;
    height: 18px;
    font-weight: bold;
    border-radius: 9px;
    line-height: 18px;
    text-align: center;
    font-size: 12px;
    font-weight: border;
    color: #fff;
  }
  .star{
    color: #fff;
    font-size: 48px;
    font-style: normal;
    position: absolute;
    right:530px;
    top:186px;
    transform: rotate(60deg);
    transition: all .3s ease;
  }
  .on{
    background-color: #e00;
  }
  .list{
    margin: 20px 20px;
    padding: 36px 0;
    list-style: none;
  }  
  a{
    display: block;
    color: #757575;
    text-decoration: none;
  }
  .list li{
    float: left;
    height: 246px;
    width: 234px;
    padding: 10px 0 20px;
    margin-right:12px;
    margin-top: 20px;
    border: 1px solid #ccc;
    background: #fff;
    -webkit-transition: all .2s linear;
    transition: all .2s linear;
  }
  .list li:hover{
    box-shadow: 2px 4px 5px #aaa;
  }
  .figure{
    width: 150px;
    height: 150px;
    margin: 0 auto 18px;
  }
  .title{
    color: #222;
    font-size: 14px;
    font-weight: normal;
    text-align: center;
  }
  .price{
    margin: 0 10px 10px;
    text-align: center;
    color: #ff6700;
  }
  .cart{
    margin: 0 15px 5px;
    text-align: center;
  }
  .cart a{
    color: #a34;
    width: 190px;
    height: 24px;
    border-radius: 4px;
    margin: 0 8px 5px;
    text-align: center;
  }
  .cart a:hover{
    color: #eee;
    box-shadow: 0 2px 1px #333,0 2px 1px #666;
    background-color: #ccc;
    background-image: linear-gradient(#33a6b8,#0089a7)
  }
  .num{
    text-align: center;
    color: #ff6700;
  }
  </style>
</head>
<body>
  <section>
  <p class=&#39;top&#39;>
  <a href="spcar.php" rel="external nofollow" >我的购物车</a><span class="<?php echo $class;?>"><?php echo $sum;?></span>
  <em class=&#39;star&#39;>★</em>
  </p>
  <ul class="list">
  <?php foreach ($arrPro as $key => $value):?>
  <li>
    <p class="figure"> 
    <a href=""><img src=" rel="external nofollow" <?php echo $value[&#39;img&#39;];?>" width="150" height="150" alt="小米移动电源5000mAh"></a> 
    </p> 
    <h3 class="title"> 
    <a href=""><?php echo $value[&#39;title&#39;];?></a>
    </h3> 
    <p class="price"><span class="num">¥<?php echo $value[&#39;price&#39;];?></span></p>
    <p class=&#39;cart&#39;><a href="action.php?id=<?php echo $value[&#39;id&#39;];?>" rel="external nofollow" >加入购物车</a></p>
  </li>
  <?php endforeach;?>
  </ul>
  <p style=&#39;clear:both&#39;></p>
  </section>
</body>
</html>

action.php


<?php
if(!empty($_GET[&#39;id&#39;])){
  require &#39;common.php&#39;;
  session_start();
  $id = $_GET[&#39;id&#39;];
  //把所选ID的商品信息遍历出来
  foreach ($arrPro as $key => $value) {
    if($id == $value[&#39;id&#39;]){
    $arrData = $arrPro[$key];
    }
  }

  //用一个新的二维数组把商品信息存起来
  $arrDatax[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
  $arrDatax[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
  $arrDatax[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
  $arrDatax[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
  $arrDatax[4][$arrData[&#39;id&#39;]] = 1;
  //判断是否有SESSION存在,有则在数组后添加,没有则直接存
  if(empty($_SESSION[&#39;shopcar&#39;])){
    $_SESSION[&#39;shopcar&#39;] = $arrDatax;
    header(&#39;Location:index.php&#39;);
  }else{
    //第一次购物之后的购物
    //重新取出来,防止数据覆盖
    $arrDataz = $_SESSION[&#39;shopcar&#39;];
    if(in_array($id,$arrDataz[0])){
      $arrDataz[4][$arrData[&#39;id&#39;]] += 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }else{
      $arrDataz[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
      $arrDataz[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
      $arrDataz[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
      $arrDataz[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
      $arrDataz[4][$arrData[&#39;id&#39;]] = 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }

  }
}else{
  echo "购物车没有商品!";
}

spcar.php


<?php
if(!empty($_GET[&#39;id&#39;])){
  require &#39;common.php&#39;;
  session_start();
  $id = $_GET[&#39;id&#39;];
  //把所选ID的商品信息遍历出来
  foreach ($arrPro as $key => $value) {
    if($id == $value[&#39;id&#39;]){
    $arrData = $arrPro[$key];
    }
  }

  //用一个新的二维数组把商品信息存起来
  $arrDatax[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
  $arrDatax[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
  $arrDatax[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
  $arrDatax[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
  $arrDatax[4][$arrData[&#39;id&#39;]] = 1;
  //判断是否有SESSION存在,有则在数组后添加,没有则直接存
  if(empty($_SESSION[&#39;shopcar&#39;])){
    $_SESSION[&#39;shopcar&#39;] = $arrDatax;
    header(&#39;Location:index.php&#39;);
  }else{
    //第一次购物之后的购物
    //重新取出来,防止数据覆盖
    $arrDataz = $_SESSION[&#39;shopcar&#39;];
    if(in_array($id,$arrDataz[0])){
      $arrDataz[4][$arrData[&#39;id&#39;]] += 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }else{
      $arrDataz[0][$arrData[&#39;id&#39;]] = $arrData[&#39;id&#39;];
      $arrDataz[1][$arrData[&#39;id&#39;]] = $arrData[&#39;img&#39;];
      $arrDataz[2][$arrData[&#39;id&#39;]] = $arrData[&#39;title&#39;];
      $arrDataz[3][$arrData[&#39;id&#39;]] = $arrData[&#39;price&#39;];
      $arrDataz[4][$arrData[&#39;id&#39;]] = 1;
      $_SESSION[&#39;shopcar&#39;] = $arrDataz;
      header(&#39;Location:index.php&#39;);
    }

  }
}else{
  echo "购物车没有商品!";
}

delete.php


<?php
session_start();
if(!empty($_GET[&#39;id&#39;])){
  $arrData = $_SESSION[&#39;shopcar&#39;];
  //判断对应的商品ID信息
    if(in_array($_GET[&#39;id&#39;],$arrData[0])){
      unset($arrData[0][$_GET[&#39;id&#39;]]);
      unset($arrData[1][$_GET[&#39;id&#39;]]);
      unset($arrData[2][$_GET[&#39;id&#39;]]);
      unset($arrData[3][$_GET[&#39;id&#39;]]);
      unset($arrData[4][$_GET[&#39;id&#39;]]);
      $_SESSION[&#39;shopcar&#39;] = $arrData;
    }
    header(&#39;Location:spcar.php&#39;);
}

以上是详细介绍PHP session实现购物车的实例教程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
哪些常见问题会导致PHP会话失败?哪些常见问题会导致PHP会话失败?Apr 25, 2025 am 12:16 AM

PHPSession失效的原因包括配置错误、Cookie问题和Session过期。1.配置错误:检查并设置正确的session.save_path。2.Cookie问题:确保Cookie设置正确。3.Session过期:调整session.gc_maxlifetime值以延长会话时间。

您如何在PHP中调试与会话相关的问题?您如何在PHP中调试与会话相关的问题?Apr 25, 2025 am 12:12 AM

在PHP中调试会话问题的方法包括:1.检查会话是否正确启动;2.验证会话ID的传递;3.检查会话数据的存储和读取;4.查看服务器配置。通过输出会话ID和数据、查看会话文件内容等方法,可以有效诊断和解决会话相关的问题。

如果session_start()被多次调用会发生什么?如果session_start()被多次调用会发生什么?Apr 25, 2025 am 12:06 AM

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

您如何在PHP中配置会话寿命?您如何在PHP中配置会话寿命?Apr 25, 2025 am 12:05 AM

在PHP中配置会话生命周期可以通过设置session.gc_maxlifetime和session.cookie_lifetime来实现。1)session.gc_maxlifetime控制服务器端会话数据的存活时间,2)session.cookie_lifetime控制客户端cookie的生命周期,设置为0时cookie在浏览器关闭时过期。

使用数据库存储会话的优点是什么?使用数据库存储会话的优点是什么?Apr 24, 2025 am 12:16 AM

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

您如何在PHP中实现自定义会话处理?您如何在PHP中实现自定义会话处理?Apr 24, 2025 am 12:16 AM

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

什么是会话ID?什么是会话ID?Apr 24, 2025 am 12:13 AM

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

您如何在无状态环境(例如API)中处理会议?您如何在无状态环境(例如API)中处理会议?Apr 24, 2025 am 12:12 AM

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器