What php.ini settings are required to allow a session to remain active for approximately two days?

前端 未结 5 1086
天命终不由人
天命终不由人 2021-01-13 07:53

http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime

says that a session.cookie_lifetime of 0 \"goes until the browser is closed\". Is

相关标签:
5条回答
  • 2021-01-13 08:21

    First off I do not recommend to anyone that they play around with session life unless they are aware of the consequences.

    In regards to the your question there are actually two systems in place to manage a session.

    Firstly if you are using PHP's default system you are employing a file based session system where by a file is created on your server which holds the actual data of the clients session, this file is normally named the same as the session id. The user then has a cookie send to there browser which holds the session id client side.

    The setting you are referring to ONLY defines the life of the cookie in the clients browser not the life of the session.

    A setting of 0: causes the cookie to last until the browser is closed.

    A setting higher than 0: causes the session to last that many seconds and is only terminated after that time. The browser can be opened and closed as many times as the user wants and the cookie will remain until the time of expiry.

    I believe but could be wrong that the setting is only the number of seconds from when the cookie is created and not an actual timestamp but I could be wrong.

    You can change this setting in your php.ini or you can use a combination of session_get_cookie_params and session_set_cookie_params

    Clarification

    Ignoring server side. The client holds a cookie which holds the SessionID and allows them to access there session. If the cookie is lost the client no longer has the ability to access the session and is in essence lost.

    A value of 0 will cause the clients browser to keep the cookie until the browser is closed. If the user was to keep there browser open for a week, the cookie would be kept for a week.

    A value greater than 0 will cause the clients browser to keep the cookie for that number of seconds. E.g. If the value was set to 172800 seconds (2 days) the cookie would be held by the browser for 2 days. If the browser is closed during this 2 days the cookie is not destroyed, it is only lost after 2 days.

    Why use 0

    Using 0 is more secure because when a user has finished using your website on a public system and close the browser the cookie is lost and the session can no longer be accessed preventing another user from opening the browser and continuing the session. It is not reliable to presume that a user will end the session manually (e.g. logout) as many don't.

    0 讨论(0)
  • 2021-01-13 08:25

    It means that the session is lost at the time the browser gets closed.

    That is, the cookie containing that session id gets deleted together with the onclose event of the browser.

    session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser

    The recommended period depends basically on what your session needs to hold. Say you want to keep your user logged in the website (remind me), you should go for the largest period. Just as an example.

    If you want the session alive for approximately two days, you just count

    60 [seconds] * 60 [minutes] * 48 [hours] = 172800 seconds

    0 讨论(0)
  • 2021-01-13 08:31

    There are two parameters you need to worry about regarding sessions. The first is the TTL for the cookie, the other is how old a session data file can become before it gets garbage collected.

    session.cookie_lifetime determines, in seconds, how long the cookie sent to the browser will last. It defaults to 0, which means until the browser closes. For two days it'd need to be 172800 seconds.

    session.gc_maxlifetime determines, also in seconds, how long before session data marked on the server will be regarded as garbage and can be deleted.

    Setting these two ini directives should give you sessions that survive for two days, except for one more thing of which you need to be aware.

    Some operating systems do automated garbage collection on their default temporary directories. If PHP is configured to store session data there, then if the GC period for the temp directory is short you may find yoruself losing your session before the value in session.gc_maxlifetime is reached. To avoid this, make sure PHP is storing session data to a location other than /tmp or whatever the temporary directory of your host operating system is.

    0 讨论(0)
  • 2021-01-13 08:36

    Wait there is the confusion .....

    "Session" will not lost if the the browser get closed....its the "Cookies" which get lost on close of browser.

    "Session" is altogether is different from "Cookies". Session stays at server and can be destroyed explicitly. Whereas "Cookies" resides at client end and can be destroyed manually or at a particular time interval or on browser close.

    0 讨论(0)
  • 2021-01-13 08:41

    Short (but slightly inaccurate) solution

    Set session.gc_maxlifetime to 172800 and session.cookie_lifetime to 0.


    Extended and more accurate solution

    In general, session.gc_maxlifetime is the configuration to control the session’s life time. So setting that directive to 172800 will make the session to expire after 172800 seconds (theoretically). But as the calculation of the age of a session is slightly odd, you might want to implement a more accurate expiration scheme. See my answer to How do I expire a PHP session after 30 minutes? for more information.

    And setting the session ID’s cookie lifetime to 0, the cookie will be valid until the browser is closed.

    0 讨论(0)
提交回复
热议问题