PHP使用cookie實現簡單的購物車功能

2021-08-25 11:06:40 字數 3917 閱讀 2143

array(

'商品id1'=>array('名字','庫存等'),

'商品id2'=>array('名字','庫存等')

function.php 函式頁面

<?php// 加入購物車function addcart($gid, $gname)else else}

}

// 刪除乙個商品

// $del是get請求的刪除id $del = $_get['del'];

function delcart($shop_car, $del)

//清空購物車全部

function delall()

}

}

// 計算總**

function totalprice($shop_car)

return $price;

}

```

shop.php 商品頁面

```

<?php

include 'mysqli.php';

$select = $db->query('select * from one');

$result = $select->fetch_assoc();

while ($result)

echo '

檢視購物車

';shop.php 商品頁面

<?phpinclude 'mysqli.php';$select = $db->query('select * from one');$result = $select->fetch_assoc();while ($result)

echo '

檢視購物車

';addcar.php 加入購物車

<?phpini_set('error_reporting', 'e_all & ~e_notice');require_once 'mysqli.php';require_once 'function.php';$gid = $_get['gid'];

$sql = 'select gname from one where id=' . $gid;

$select = $db->query($sql);

// 查詢錯誤返回

if (!$select)

$result = $select->fetch_assoc();

$gname = $result['gname'];

// 查詢為空返回,避免空商品加到購物車

if (empty($gname))

addcart($gid, $gname);

//setcookie('cart','',time()-3600);

header('location:shop.php');

showcar.php 購物車展示頁面

<?phprequire_once 'function.php';ini_set('error_reporting', 'e_all & ~e_notice');$shop_car = unserialize($_cookie['cart']);echo '';

if (empty($shop_car))

// 展示

$key = array_keys($shop_car);

for ($i = 0; $i < count($shop_car); $i++)

// 總**

echo '

總**:$' . totalprice($shop_car);

if (isset($_get['del']))

}

// 清空按鈕

echo '

清空購物車

';if (isset($_get['clean']))

}

```

conn.php

```

<?php

define('host','localhost');

define('user','root');

define('pwd','');

define('dbname','goods');

conn.php

<?phpdefine('host','localhost');define('user','root');define('pwd','');define('dbname','goods');

mysqli.php

<?phpinclude 'conn.php';$db = new mysqli(host,user,pwd,dbname);

php使用cookie實現簡單登入功能

cookie 是用來將 上的資料記錄在客戶端的技術,這種web技術讓伺服器將一些資料存放於客戶端。比如當使用者成功登入 後,伺服器會把使用者資訊儲存到使用者的cookie中,當再次訪問同乙個 的其他指令碼時就會攜帶cookie中的資料一起訪問,在伺服器的每乙個指令碼中都可以接受攜帶的cooike資料...

php使用cookie實現購物車

檢視 原始碼時 看到的做下筆記 檔案包括 test.php,cart.php 一.test.php 商品顯示頁面 cart list empty cookie cart list unserialize cookie cart list array 例子使用陣列實際會用mysql product l...

PHP 使用cookie實現記住登入狀態

php使用cookie實現記住登入狀態,本文用最原始的方法講解如何實現記住登入狀態,給出3個步驟和具體實現 需要的朋友可以參考下。要實現記住密碼自動登入的功能,我們大多數據都是利用了客戶端的cookies來實現。php製作記住密碼自動登入的解決思路,其實也就是對session,cookies的操作 ...