搜尋
首頁後端開發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="/static/imghwm/default1.png"  data-src=" rel="  class="lazy"  external nofollow" <?php echo $value[&#39;img&#39;];? alt="詳細介紹PHP session實作購物車的實例教學" >" width="150"    style="max-width:90%" 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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境