2011-01-27 6 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']; 
    } 

} 




?> 

答えて

3

あなたは(セッションの内容が変更される前に、または読み取り)ページの上部にsession_start()-functionを使用してセッションを初期化する必要があります。

1

コードは正常に見えますが、セッションを変更するか、セッションから読み取るすべてのページでsession_start()を呼び出す必要があります。それをページの上部に置くだけで動作します。

session_start(); 
1

最近、ページリダイレクト後に$ _SESSIONバールが問題を抱えていたため、問題が発生しました。

セッション変数は、http://www.example.comからhttp://example.comへのリダイレクトからリダイレクトされても生き残りません。だから、あなたは、相対または絶対パスを持つリダイレクトを使用しますが、サイト名なしでそれを行う:

使用

ここでは適用されません

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

に反対したが、旅をしたとして

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

私はかなりのビットアップ。

+0

正しい。その理由は、 'www.example.com'と' example.com'は異なるドメインです( 'www'はサブドメインです)。クッキーは1つのドメインに対してのみ有効なので、そこに格納されているセッションキーは、変更するときには存在しません。 – jwueller

関連する問題