CodeIgniter Forums

Full Version: Session problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]shinokada[/eluser]
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.

Code:
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

Code:
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

Code:
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.

Code:
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

El Forum

[eluser]überfuzz[/eluser]
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.

El Forum

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

Thanks for your reply.

El Forum

[eluser]John_Betong[/eluser]
 
 
Try moving $session_start() to the first line of code in your index.php file.
 
 
 

El Forum

[eluser]überfuzz[/eluser]
[quote author="shinokada" date="1257445854"]There is nothing in Session array at the beginning.
Is it a problem? If so, how can I solve it?

Thanks for your reply.[/quote]
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.
Code:
if(isset($_SESSION['some_key'])){ /* do stuff */ } else {/* don't do stuff */}
//or
if(!isset($_SESSION['some_key'])) {$_SESSION['some_key'] = 'default';}

El Forum

[eluser]shinokada[/eluser]
After session_start(), I have seen other codes having $cart = $_SESSION['cart'];.
(http://v3.thewatchmakerproject.com/journ...pping-cart)

I still don't understand why it gives errors.

El Forum

[eluser]BrianDHall[/eluser]
[quote author="shinokada" date="1257454084"]After session_start(), I have seen other codes having $cart = $_SESSION['cart'];.
(http://v3.thewatchmakerproject.com/journ...pping-cart)

I still don't understand why it gives errors.[/quote]

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:

Code:
$a = $b;

...because $b has not been defined.

You'll need to use something like:
Code:
if (! empty($_SESSION['cart']))
{
   $cart = $_SESSION['cart'];
}

...as uberfuzz suggested.

El Forum

[eluser]shinokada[/eluser]
Thanks Brian!
I added
Code:
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();

And it works.

Thanks again.

El Forum

[eluser]überfuzz[/eluser]
[quote author="shinokada" date="1257461227"]Thanks Brian!
I added
Code:
$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();

And it works.

Thanks again.[/quote]
If you're really into CI you might consider adding some CI flavour to the session handling: session - user_guide