Store in the database or in sessions

4

When doing a search, I found that some colleagues store the cart products in sessions like this:

$_SESSION[cesta][$indice][produto]

In particular, I usually store client products in a database table and after a certain open period (I have a StatusCompra = 'A' field and the 'NOW ()' date), for example 3 days depending on the customer's inventory, the system automatically deletes.

But now I was in doubt. What is the best way to work with a shopping cart in a virtual store? I mean good practices ...

    
asked by anonymous 14.03.2017 / 22:27

3 answers

4

There is a problem with using only a session, which is default that takes very little time ( in general the lifetime of a session is 24mins ), I can put it in the cart now, I'll talk / ask my girlfriend / mother / father ... and in a hour or two already there is not, in this scenario is "bad" because there is very little leeway (time) for the user to keep the "candidate" products to be bought. In this case the ideal would be to strengthen with cookies (your way is also correct, in the database and after 3 days these are eliminated), or if you can, change the lifetime of the session as the colleague @TTKDroid mentions in his response (although this consumes resources on the server).

That said, then:

In reality it depends on your needs, there is no rule for this, if you want to permanently save what users thought but did not buy, you should insert in the database if you want it to be only temporary you can only leave in session / cookies.

I believe that many systems, especially big , permanently store this data for future functionalities (eg algorithms to know the interests and show results according to the categories of products that have already been in your cart and / or that you have already bought) or simply for statistics, as colleague @Daniel Omine said can also help in strategic sales decisions.

It all depends on whether you think it's worth it, you have support for it, and for what purposes you'll keep it permanently.

Personally, it has been very rare to create a system in which to store the cart information in the database, simply because for most online stores this information would be wasted, that is, it would be data that would have no objective in the future and would only take up space on the disk, so it's not worth saving.

Thinking / weighing this decision is always in the hands of system programmers / owners / owners.

    
14.03.2017 / 22:42
2

I know that you expect a definitive answer but have no definitive answer to that question, everything will depend on the architecture of your application.

If you are hosting the system in a webfarm, using session in memory as usual is not an option, since each client request can be answered by a different server. In that case you would have to store in a database or have a session server.

If you are going to use a REST API, it is advisable to store in database as well.

You can also store on the client, in SessionStorage, if the data is not critical. This avoids the session timeout problem and does not use storage space on the server.

Sessions occupy memory on the server, on a large-scale system it is best to avoid using session.

Based on these facts I suggest database storage or LocalStorage for better scalability of your system.

    
15.03.2017 / 12:55
1

You can change the session timeout parameters in php

// manter sessão por 1 hora
ini_set('session.gc_maxlifetime', 3600);

// cada cliente vai se "lembrar" da sessão por 1hora
session_set_cookie_params(3600);

session_start(); 

So you adjust how long in seconds you want your session to last.

So extending the session or storing in the database has its pros and cons. Where in the database you get a lot of junk that should be cleaned if the user does not proceed with the checkout, the longer session consumes webserver resources for the time that you have determined.

    
14.03.2017 / 23:37