2011-01-27 129 views
2

我正在使用基本购物车。但是,它看起来好像$ _SESSION变量没有正确存储或访问。例如,如果您前往here,它将显示项目名称。但是,如果没有任何$ _GET变量刷新到cart.php,它将不会返回任何内容。我究竟做错了什么?

<?php 
include "tickets/config.php"; 

if (isset ($_GET['action']) && isset($_GET['item'])) { 
    $cart = new Cart($_GET['item']); 
    if ($_GET['action'] == "addItem") { 
     $cart->addItem(); 
     $cart->get_items_code(); 
     $cart->populate(); 
    } 
    if($_GET['action'] == "removeItem") { 
     $cart->removeItem(); 
     $cart->get_items_code(); 
     $cart->populate(); 
    } 
    $cart->postAllItems(); 
} 
else { 
    $cart = new Cart(null); 
    $cart->get_items_code(); 
    $cart->populate(); 
    $cart->postAllItems(); 
} 

class Cart { 
    protected $all_items = array(); 
    protected $request_item; 
    protected $content; 
    protected $item_obj; 

    public function __construct($request_item) { 
     $this->request_item = $request_item; 
     if ($this->request_item) 
      $this->item_obj = new Item($this->request_item); 
     $this->all_items = $this->getAllItems(); 
    } 

    public function getAllItems() { 
     if ($_SESSION['cart']) 
      $request = $_SESSION['cart']; 
     else 
      $request = array(); 
     return $request; 
    } 

    public function postAllItems() { 
     $_SESSION['cart'] = $this->all_items; 
    } 

    public function addItem() { 
     array_push($this->all_items, $this->item_obj); 
    } 

    public function removeItem() { 
     unset($this->all_items[$this->item_obj->get_item()]); 
    } 

    public function get_items_code() { 

     //for($i = 0; $this->all_items[$i]; $i++) { 
     foreach($this->all_items as $item) { 
      $name = $item->get_name(); 
      $this->content .= <<<HTML 
<div class="item"> 
$name 
</div>  
HTML; 
     } 
    } 

    public function populate() { 
     echo <<<HTML 
<div id="list">  
$this->content 
<div> 
HTML; 
    } 
} 


class Item { 
    protected $id; 
    protected $name; 
    protected $price; 
    protected $desc; 
    protected $colors; 
    protected $sizes; 
    protected $pic_url; 
    protected $all_info; 

    public function __construct($id) { 
     $this->id = $id; 
     $this->get_item_info(); 
    } 

    public function get_item() { 
     return ($this); 
    } 

    public function get_name() { 
     return $this->name; 
    } 

    protected function get_item_info() { 
     $sql = "SELECT * FROM catalog WHERE id = ".$this->id; 
     $this->all_info = mysql_query($sql); 
     $this->all_info = mysql_fetch_array($this->all_info); 
     $this->name = $this->all_info['name']; 
     $this->price = $this->all_info['price']; 
     $this->desc = $this->all_info['description']; 
     $this->colors = $this->all_info['colors']; 
     $this->sizes = $this->all_info['sizes']; 
     $this->pic_url = $this->all_info['picture']; 
    } 

} 




?> 

回答

1

您的代码看起来不错,但您必须在修改会话或从会话读取的每个页面上调用session_start()。只要把它放在页面的顶部就行了。

session_start(); 
1

我最近也遇到了$ _SESSION vars在页面重定向后维护自己的问题,这可能是您的问题。

会话变量将无法通过重定向从http://www.example.com重定向到http://example.com重定向。所以,你可以使用相对或绝对路径重定向,但做没有站点名称:

使用

header('Location: /new_page.php'); 

,而不是

header('Location: http://www.exmple.com/new_page.php') 

也许这里并不适用,但没有行程我相当多。

+0

正确。原因是,“www.example.com”和“example.com”是不同的域名(当然,“www”是一个子域名)。 Cookie只对一个域有效,因此存储在其中的会话密钥在从一个域更改为另一个域时不存在。 – jwueller 2011-01-28 12:26:29