CodeIgniter Forums

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

El Forum

[eluser]nikokolev[/eluser]
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.ph...inea/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.

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

El Forum

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

It works fine for me with Firefox when I disable my cache, so I think this is a caching issue. Try using a 307 redirect instead:
Code:
redirect('/url', 'location', 307);

That should prevent caching.

El Forum

[eluser]nikokolev[/eluser]
[quote author="TheFuzzy0ne" date="1367674691"]Welcome to the CodeIgniter forums!

It works fine for me with Firefox when I disable my cache, so I think this is a caching issue. Try using a 307 redirect instead:
Code:
redirect('/url', 'location', 307);

That should prevent caching.[/quote]

I've tryed this one too, but unfortunately this is not the solution! I just don't understand why when everything was in local was all fine. And now i've uploaded to the server, it behaves this way. So even with
Code:
redirect('/url', 'location', 307);
the result is pretty much the same. This issue is getting be crazy. If caching is the problem, where should I look at to prevent that?

El Forum

[eluser]TheFuzzy0ne[/eluser]
If your browser is still using cached files, then no amount of coding will fix it. Now you're sending out the correct headers, clearing your browser's cache should fix it once and for all.

El Forum

[eluser]nikokolev[/eluser]
[quote author="TheFuzzy0ne" date="1367688361"]If your browser is still using cached files, then no amount of coding will fix it. Now you're sending out the correct headers, clearing your browser's cache should fix it once and for all.[/quote]

Not even close, man! I tryed to delete all cached data from Firefox, reinitialized Safari. Still same issue, even with the 307 specified. This is gettin weirder and weirder.

Trying maybe to reload the site? I mean, to the server! Delete this one and go on and upload again the files..

Don't know what to try anymore..

El Forum

[eluser]TheFuzzy0ne[/eluser]
I don't think it has anything to do with the server. I'm using the Web Developer Extension for Firefox. One of the tricks it can do, is disabling your cache. When I disable my cache everything works as expected. I can add things to the cart, and I can delete them.

This suggests to me that it's most likely an issue with caching. Please try the extension out yourself, it's definitely useful to have for any Web developer.

Also, please try enabling the profiler.
Code:
$this->output->enable_profiler(TRUE);
That might help you spot the problem if you keep an eye on your cookie.

El Forum

[eluser]nikokolev[/eluser]
[quote author="TheFuzzy0ne" date="1367690514"]I don't think it has anything to do with the server. I'm using the Web Developer Extension for Firefox. One of the tricks it can do, is disabling your cache. When I disable my cache everything works as expected. I can add things to the cart, and I can delete them.

This suggests to me that it's most likely an issue with caching. Please try the extension out yourself, it's definitely useful to have for any Web developer.

Also, please try enabling the profiler.
Code:
$this->output->enable_profiler(TRUE);
That might help you spot the problem if you keep an eye on your cookie.[/quote]

I have the We Dev Extention for Safari. And when i disable caching everything works as expected, as you said. I'll enable the profiler and try to see what else can this be due to. I really hope to fix this cause, as you may understand, it's one of the main reason i'm building that site: the client wants to sell shoes.

Anyway, thank you so much for the help. I'll keep you updated if I ever figure this out! Thanks man!

El Forum

[eluser]nikokolev[/eluser]
I'm also thinking,

Isn't caching actually useful? If anytime a user wants to add to cart the browser should reload the page, that will be killer weight on mobile devices for instanse. Would be great to update just that information but actually keep images and all the other stuff in cache, once they were loaded. It doesn't make sence to reload them too. :-(

El Forum

[eluser]TheFuzzy0ne[/eluser]
Images will be cached. It's the page itself you don't want to be cached, since that changes. The problem I think your having is that the redirect itself is cached. So rather than submitting the post data to the server, I think the browser is just redirecting back to the cart URL automatically, so the data is never actually being processed on the server.

El Forum

[eluser]nikokolev[/eluser]
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:

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

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

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