ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Session problem

November 05, 2009 2:44am

Subscribe [4]
  • #1 / Nov 05, 2009 2:44am

    shinokada

    144 posts

    I am reading a Codeigniter book “Professional Codeigniter”.

    The welcome.php start session in constructor.

    When I click “Add to cart” button, I get error message relating to the session.

    Add to cart button is linked welcome/cart/4 etc.

    If I refresh or go back and try it, then it does not show errors any more.

    However if I delete cookies and repeat the above procedure, I get the same error messages.

    If I change index.php error reporting from error_reporting(E_ALL) to error_reporting(E_ALL & ~E_NOTICE);, errors do not appear any more.

    error_reporting(E_ALL  & ~E_NOTICE);

    Could anyone tell me if there is another way to solve this?

    And why changing error reporting solve this problem?

    Regards.


    welcome.php

    class Welcome extends Controller {
      function Welcome(){
        parent::Controller();
        session_start();
        $this->output->enable_profiler(FALSE);
      }
    
      function index(){    
          $this->output->cache(30);
        $data['title'] = "Welcome to Claudia's Kids";
        $data['navlist'] = $this->MCats->getCategoriesNav();
        $data['mainf'] = $this->MProducts->getMainFeature();
        $skip = $data['mainf']['id'];
        $data['sidef'] = $this->MProducts->getRandomProducts(3,$skip);
        $data['main'] = 'home';
        $this->load->vars($data);
        $this->load->view('template');
      }
    
    ...
    ...
     function cart($productid=0){
    
        if ($productid > 0){
            //$productid = $this->uri->segment(3);
            $fullproduct = $this->MProducts->getProduct($productid);
            $this->MOrders->updateCart($productid,$fullproduct);
            redirect('welcome/product/'.$productid, 'refresh');
        
        }else{
            $data['title'] = "Claudia's Kids | Shopping Cart";
            if (count($_SESSION['cart'])){
                $data['main'] = 'shoppingcart';
                $data['navlist'] = $this->MCats->getCategoriesNav();
                $this->load->vars($data);
                $this->load->view('template');    
            }else{
                redirect('welcome/index','refresh');
            }
        }
      }
      ...
    ...

    models/moders.php

    class MOrders extends Model{
     function MOrders(){
        parent::Model();
     }
    
    function updateCart($productid,$fullproduct){
        //pull in existing cart first!
        $cart = $_SESSION['cart'];//$this->session->userdata('cart');
        $productid = id_clean($productid);
        $totalprice = 0;
        if (count($fullproduct)){
            if (isset($cart[$productid])){
                $prevct = $cart[$productid]['count'];
                $prevname = $cart[$productid]['name'];
                $prevprice = $cart[$productid]['price'];
                $cart[$productid] = array(
                        'name' => $prevname,
                        'price' => $prevprice,
                        'count' => $prevct + 1
                        );
            }else{
                $cart[$productid] = array(
                        'name' => $fullproduct['name'],
                        'price' => $this->format_currency($fullproduct['price']),
                        'count' => 1
                        );            
            }
        
            foreach ($cart as $id => $product){
                $totalprice += $product['price'] * $product['count'];
            }        
            
            $_SESSION['totalprice'] = $this->format_currency($totalprice);
            //$this->session->set_userdata('totalprice', $totalprice);
            $_SESSION['cart'] = $cart;
            //$this->session->set_userdata('cart',true);
            $this->session->set_flashdata('conf_msg', "We've added this product to your cart."); 
        }
    
    }
    ...
    ...


    error messages.

    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Undefined index: cart
    
    Filename: models/morders.php
    
    Line Number: 10
    A PHP Error was encountered
    
    Severity: Warning
    
    Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\codeigniter_shopping_original\system\libraries\Exceptions.php:166)
    
    Filename: libraries/Session.php
    
    Line Number: 662
    A PHP Error was encountered
    
    Severity: Warning
    
    Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\codeigniter_shopping_original\system\libraries\Exceptions.php:166)
    
    Filename: helpers/url_helper.php
    
    Line Number: 539
  • #2 / Nov 05, 2009 5:56am

    überfuzz

    350 posts

    You’re using the session data to set $cart. Start searching in the session array. What’s in it? Keep track on it as you progress throw the process you were describing.

  • #3 / Nov 05, 2009 8:30am

    shinokada

    144 posts

    There is nothing in Session array at the beginning.
    Is it a problem? If so, how can I solve it?

    Thanks for your reply.

  • #4 / Nov 05, 2009 8:33am

    John_Betong

    690 posts

     
     
    Try moving $session_start() to the first line of code in your index.php file.
     
     
     

  • #5 / Nov 05, 2009 9:51am

    überfuzz

    350 posts

    There is nothing in Session array at the beginning.
    Is it a problem? If so, how can I solve it?

    Thanks for your reply.

    Well if you’re going to use it, it is a problem.

    I didn’t look throw your code, but from the top of my head, I’d consider two things.

    if(isset($_SESSION['some_key'])){ /* do stuff */ } else {/* don't do stuff */}
    //or
    if(!isset($_SESSION['some_key'])) {$_SESSION['some_key'] = 'default';}
  • #6 / Nov 05, 2009 10:48am

    shinokada

    144 posts

    After session_start(), I have seen other codes having $cart = $_SESSION[‘cart’];.
    (http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart)

    I still don’t understand why it gives errors.

  • #7 / Nov 05, 2009 11:23am

    BrianDHall

    760 posts

    After session_start(), I have seen other codes having $cart = $_SESSION[‘cart’];.
    (http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart)

    I still don’t understand why it gives errors.

    The errors are caused by the fact that $_SESSION[‘cart’] is not a valid array element when you are trying to access it. It is the same thing as doing this, which will also throw an error:

    $a = $b;

    ...because $b has not been defined.

    You’ll need to use something like:

    if (! empty($_SESSION['cart']))
    {
       $cart = $_SESSION['cart'];
    }

    ...as uberfuzz suggested.

  • #8 / Nov 05, 2009 12:47pm

    shinokada

    144 posts

    Thanks Brian!
    I added
    $cart = isset($_SESSION[‘cart’]) ? $_SESSION[‘cart’] : array();

    And it works.

    Thanks again.

  • #9 / Nov 05, 2009 12:52pm

    überfuzz

    350 posts

    Thanks Brian!
    I added
    $cart = isset($_SESSION[‘cart’]) ? $_SESSION[‘cart’] : array();

    And it works.

    Thanks again.

    If you’re really into CI you might consider adding some CI flavour to the session handling: session - user_guide

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases