1
votes

I have a really strange behaviour of CodeIgniter's Shopping Cart Class. I have set the ci_session table in my database and already have changed the sess_use_database to TRUE.

What is happening is that when i add items to the Shopping Cart everything is fine: i see my total items counter going up and everything. When i go to my shopping cart page the first time i see all my items there, where they should be. I have also provided delete item button and an Empty Cart button. The strange things happen here: when i click to remove an item, the page refreshes (due to the redirect i think) but the item is still there! Then if i manually refresh the page of the cart, i see that same item i had removed disappear. It's like, when i use redirect, i get the cache of the page, not the actual page with the fresh information on it.

Anyway, here are some links:

Try to add some items from this page: http://www.pantanishoes.it/Niko/index.php/store/linea/urban Clicking on the big dark button on bottom of every item description.

Then try to go to Carrello on the menu on top and try to delete some of the items or Svuota(which does a simple destroy()) and see what happens! Any help would be greatly appreciated! Thanks in advice!

Here is some code of the Cart.

function add() {

    $item = $this->store_model->get_item($this->input->post('id'));

    $data = array(
        'id' => $this->input->post('id'),
        'name' => $item->modello,
        'qty' => $this->input->post('qty'),
        'price' => $item->prezzo,
        'options' => array(
            'taglia' => $this->input->post('taglia'),
            'linea' => $item->linea,
            'modello' => $item->modello,
            'foto' => $item->foto1
            )
    );

    $this->cart->insert($data);

    $linea = str_replace(' ', '-', $item->linea);

    redirect('/store/linea/' . $linea . '/', 'location');
}

function remove() {
    $rowid = $this->uri->segment(3);
    $data = array(
        'rowid' => $rowid,
        'qty' => 0
    );

    $this->cart->update($data);
    redirect('cart');
}

function destroy() {
    $this->cart->destroy();
    redirect('cart');
}

On localhost everything was working great! When i loaded the website to the server, it started to have this issues. Really really strange! Is there some config thing I'm missing?

Chrome says: Request URL:http://www.pantanishoes.it/Niko/index.php/cart/remove/fb1b4a9869de6f24aa620e2307192d93 Request Method:GET Status Code:302 Moved Temporarily (from cache)

2
are you using codeigniter cache or some kind of caching method? cause if i disable cache on browser it works, if leave cache enabled it returns the same page as you described. So prova a vedere se stai cachando (non "cacando") qualcosa :Ditsme

2 Answers

1
votes

Ok guys. So i finally fixed this little bug thanks to the help of a friend of mine who has used CI for a while. As some of you guessed, this is a cache problem. The only thing you need to do is to force PHP headers like so:

header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' ); 
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); 
header( 'Cache-Control: no-store, no-cache, must-revalidate' ); 
header( 'Cache-Control: post-check=0, pre-check=0', false ); 
header( 'Pragma: no-cache' );

You can do a private function in your controller so that it's not accessible from outside and call it in your relative add, delete, destroy methods. The private function must look something like this:

private function _set_headers() {
    header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' ); 
    header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i(worry)' ) . ' GMT' ); 
    header( 'Cache-Control: no-store, no-cache, must-revalidate' ); 
    header( 'Cache-Control: post-check=0, pre-check=0', false ); 
    header( 'Pragma: no-cache' );
}

And you call it inside the other functions like so:

$this->_set_headers();

Remember that this will work only in the scope of the class. You can't call this function in other controllers and it can't be accessed from outside the scope, cause it's not public.

Thanks to you all!!

0
votes

i would advice you to try this:

redirect('/store/linea/' . $linea . '/', 'refresh');

instead of

 redirect('/store/linea/' . $linea . '/', 'location');

it seems your page remains cached after redirecting

Spero che funzioni