Greetings

Tell me, please, how can I set my life time for a particular session?

$_SESSION['log'];//Авторизация $_SESSION['cart'];//корзина 

I have a session to authorize a user and add goods to the cart. For an authorization session, you need to set your time to live, and for a basket session, this is not boring.

Is it possible to realize this and how, if possible? :)

    2 answers 2

    In fact, no way to ask. First you need to understand what a session is and what it is intended for.

    Each site visitor is assigned a unique identifier called a session id. It is either stored in a cookie on the user’s side, or transmitted via a URL.

    That is, the session should be created one for the session.

    The lifetime of the general session itself can be set in php.ini session.gc_maxlifetime

    In your case, the correct option will most likely set the data in the cookie .

    And work with the data from the cookie through $ _COOKIE

    • That is, authorization is best done through $ _COOKIE? Does something change, if not in session to do it? In my opinion, no) - Dima
    • @ Dima, as I understand you have one authorization + basket. You can leave the authorization for the session and change the basket to a cookie - Kostiantyn Okhotnyk
    • And if, on the contrary, it is also possible? - Dima
    • so it is not necessary to do, in terms of security :) - Kostiantyn Okhotnyk
    • Understood, thank you! :) I think you have a more correct and detailed answer. - Dima

    Specifically for $ _SESSION, you cannot specify a lifetime. It is better to do so, when filling $ _SESSION ['log'], fill $ _SESSION ['log_start'] with the current time. And then check whether too much time has passed.

     $_SESSION['log_start'] = time(); ... if (time() - $_SESSION['log_start'] > $log_expr_time) { сессия протухла 
    • Thank you, I had this option in the last place, I hoped that I could implement it as in the question, okay, I will do it like this :) - Dima
    • one
      lifetime can be specified for memcache or Redis or something similar. - Ivan Bolnikh