当我将元素添加到购物车时,我在会话覆盖方面遇到了一些问题。
$_SESSION["cart"] 元素仍在会话中,但是当我添加一个新元素时,它会覆盖以前的元素,如果我更改部分并返回商店,它会重置,我不确定这是为什么正在发生。
我检查了这个线程PHP: Storing 'objects' inside $_SESSION,但在任何会话修改之前我确实有 session_start() 。我还检查了这个线程Interal Server Error 500 - session_start(),但是在检查了服务器 phpinfo() 之后我明白了
Directive Local Value Master Value
session.save_handler files files
我有三个类,产品、演示和购物车。
演示文稿有产品,购物车有演示文稿。
类产品
class Product {
private $id;
private $name;
private $sku;
private $weight;
private $existence;
private $minimum_existence;
private $url;
private $images;
private $db;
private $lang;
public function __construct($id,$lang="es") {
$this -> id = $id;
$this -> db = new PDO(DB_CONNECTION,DB_USER,DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this -> lang = $lang;
$this -> images = array();
$this -> get_info();
}
public function __destruct() {
unset($this);
}
public function get_info() {
$info_product = $this -> db -> prepare("SELECT shop_products.*, shop_products_images.* FROM shop_products JOIN shop_products_images ON shop_products.id_product=shop_products_images.id_product WHERE shop_products.id_product=".$this -> id." AND shop_products_images.principal=1");
$info_product -> execute();
$info = $info_product -> fetch();
$this -> id = $info["id_product"];
$this -> name = $info["product_name_".$this -> lang];
$this -> sku = $info["product_sku"];
$this -> weight = $info["product_weight"];
$this -> existence = $info["product_existence"];
$this -> minimum_existence = $info["product_minimum_existence"];
$this -> url = $info["product_url_".$this -> lang];
$this -> images["original"] = $info["image_original"];
$this -> images["big"] = $info["image_big"];
$this -> images["medium"] = $info["image_medium"];
$this -> images["small"] = $info["image_small"];
$this -> images["thumbnail"] = $info["image_thumbnail"];
}
//
public function get_name() {
return $this -> name;
}
public function get_sku() {
return $this -> sku;
}
public function get_weight() {
return $this -> weight;
}
public function get_existence() {
return $this -> existence;
}
public function get_minimum_existence() {
return $this -> minimum_existence;
}
public function get_url() {
return $this -> url;
}
public function get_image($size) {
return $this -> images[$size];
}
}
课堂介绍
class Presentation {
private $id;
private $name;
private $description;
private $capacity;
private $weight;
private $price;
private $discount;
private $url;
private $images;
private $products;
private $db;
private $lang;
public function __construct($id,$lang="es") {
$this -> id = $id;
$this -> db = new PDO(DB_CONNECTION,DB_USER,DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this -> lang = $lang;
$this -> images = array();
$this -> products = array();
$this -> get_info();
}
public function __destruct() {
unset($this);
}
public function get_info() {
$info_presentation = $this -> db -> prepare("SELECT * FROM shop_product_presentations WHERE id_presentation=".$this -> id);
$info_presentation -> execute();
$info = $info_presentation -> fetch();
$this -> name = $info["presentation_name_".$this-> lang];
$this -> description = $info["presentation_description_".$this-> lang];
$this -> capacity = $info["presentation_capacity"];
$this -> weight = $info["presentation_weight"];
$this -> price = $info["presentation_price"];
$this -> discount = $info["presentation_discount"];
$this -> url = $info["presentation_url_".$this-> lang];
$this -> images["original"] = $info["presentation_image_original"];
$this -> images["medium"] = $info["presentation_image_medium"];
$this -> images["small"] = $info["presentation_image_small"];
}
//
public function add_product($id_product,$quantity=1) {
if($this -> get_quantity() < $this -> capacity) {
if(isset($this -> products[$id_product])) {
if($quantity > $this -> get_left()) {
$quantity = $this -> get_left();
}
$this -> products[$id_product]["quantity"] += $quantity;
if($this -> products[$id_product]["quantity"] == 0) {
unset($this -> products[$id_product]);
}
} else {
$this -> products[$id_product]["product"] = new Product($id_product);
$this -> products[$id_product]["quantity"] = $quantity;
}
} else {
return false;
}
}
public function total_weight() {
$weight = $this -> weight;
foreach($this -> products as $p) {
$weight += ($p["product"] -> get_weight() * $p["quantity"]);
}
return $weight;
}
public function display() {
}
//
public function get_name() {
return $this -> name;
}
public function get_description() {
return $this -> description;
}
public function get_capacity() {
return $this -> capacity;
}
public function get_quantity() {
$x = 0;
foreach($this -> products as $p) {
$x += $p["quantity"];
}
return $x;
}
public function get_left() {
return $this -> capacity - $this -> get_quantity();
}
public function get_weight() {
return $this -> weight;
}
public function get_price() {
return $this -> price;
}
public function get_discount() {
return $this -> discount;
}
public function get_url() {
return $this -> url;
}
public function get_image($size) {
return $this -> images[$size];
}
public function get_products() {
foreach($this -> products["product"] as $p) {
$p -> get_info();
}
return $this -> products;
}
}
类推车
class Cart {
private $id;
private $start_datetime;
private $end_datetime;
private $id_customer;
private $presentations;
public function __construct($id_customer=0) {
$this -> id = md5(uniqid(mt_rand()));
$this -> start_datetime = date("Y-m-d H:i:s");
$this -> end_datetime = date("Y-m-d H:i:s", strtotime($this -> start_datetime) + 1800);
$this -> id_customer = $id_customer;
$this -> presentations = array();
}
public function __destruct() {
unset($this);
}
//
public function add_presentation($presentation) {
array_push($this -> presentations,$presentation);
$this -> end_datetime = date("Y-m-d H:i:s", strtotime($this -> end_datetime) + 600);
}
public function remove_presentation($index) {
unset($this -> presentations[$index]);
}
public function get_subtotal() {
$subtotal = 0.00;
foreach($this -> presentations as $p) {
$p -> get_info();
$subtotal += $p -> get_price();
}
return number_format($subtotal,2,".","");
}
public function get_taxes($percentage) {
return number_format($this -> get_subtotal() * $percentage,2,".","");
}
public function get_total($percentage) {
return number_format($this -> get_subtotal() + $this -> get_taxes($percentage),2,".","");
}
public function get_total_weight() {
$weight = 0.00;
foreach($this -> presentations as $p) {
$weight += $p -> total_weight();
}
return number_format($weight,2,".","");
}
//
public function get_id() {
return $this -> id;
}
public function get_start_datetime() {
return $this -> start_datetime;
}
public function get_end_datetime() {
return $this -> end_datetime;
}
public function get_id_customer() {
return $this -> id_customer;
}
public function set_id_customer($id_customer) {
$this -> id_customer = $id_customer;
}
public function get_presentations() {
return $this -> presentations;
}
}
好的,所以我在 head.php 文件中的处理程序是:
require_once("lib/dbconfig.php");
require_once("lib/class.product.php");
require_once("lib/class.presentation.php");
require_once("lib/class.cart.php");
session_start();
if(isset($_SESSION["gandj_customer"])) { $id_customer = $_SESSION["gandj_customer"]["id_customer"]; }
else { $id_customer = 0; }
if(isset($_SESSION["cart"]))
{
if($_SESSION["cart"] -> get_end_datetime() < date("Y-m-d H:i:s"))
{
$_SESSION["cart"] = new Cart($id_customer);
}
if($_SESSION["cart"] -> get_id_customer() == 0 && $id_customer != 0)
{
$_SESSION["cart"] -> set_id_customer($id_customer);
}
}
else
{
$_SESSION["cart"] = new Cart($id_customer);
}
if($seccion == "shop") {
switch($_POST["action"]) {
case "add":
$pres = new Presentation($_POST["id_presentation"]);
$pres -> get_info();
foreach($_POST["cantidad"] as $key => $cantidad) {
if($cantidad > 0) {
$pres -> add_product($key,$cantidad);
}
}
$_SESSION["cart"] -> add_presentation($pres);
header("Location: /shop/");
break;
}
}
我遇到的问题是...
第 1 步:我打开商店部分,我的 print_r 打印出来:
Cart Object
(
[id:private] => c3ceee39137d55cbf6db97b67dc90cdc
[start_datetime:private] => 2013-06-07 12:46:36
[end_datetime:private] => 2013-06-07 13:16:36
[id_customer:private] => 0
[presentations:private] => Array
(
)
)
第 2 步:我添加了一个随机演示文稿,它显示正确,并且如函数中所述,它会在会话购物车结束时间前 10 分钟广告。
Cart Object
(
[id:private] => c3ceee39137d55cbf6db97b67dc90cdc
[start_datetime:private] => 2013-06-07 12:46:36
[end_datetime:private] => 2013-06-07 13:26:36
[id_customer:private] => 0
[presentations:private] => Array
(
...
this part displays correctly
...
)
)
第 3 步:我决定从 URL 再次打开该部分。这就是我得到的输出。如果您注意到,原始日期时间并没有改变,这让我相信 $_SESSION 购物车对象保持不变。
Cart Object
(
[id:private] => c3ceee39137d55cbf6db97b67dc90cdc
[start_datetime:private] => 2013-06-07 12:46:36
[end_datetime:private] => 2013-06-07 13:16:36
[id_customer:private] => 0
[presentations:private] => Array
(
)
)
我的一个假设是,当我将 Presentation 和 Product 对象添加到会话时,我没有很好地处理它们,但我之前使用了类似的过程并且它工作正常。有什么建议么?
编辑:
过了一会儿,我发现这是一个会话问题,因为原始的 Cart 对象保持不变,但会话中没有添加任何新对象。它在我执行该过程的那一刻被添加,但它不是通过页面持久存在的。我到处都有 session_start() 。
我正在处理 gettext 和其他一些会话变量,我不知道这是否会以任何方式影响。