What php.ini settings are required to allow a sess

2019-02-17 19:54发布

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 that the absolute maximum length that the session can have (always wiped when the browser is closed), or would setting a session.cookie_lifetime of, say, 23243245234 yeild a result that would probably last beyond whenever the browser is closed?

More to the point, what php.ini settings would I need to set to make sessions last somewhere along the lines of two days, and is there a security reason to recommend a certain (I would expect lower) time limit, and if so what would the recommended period be?

Intended behavior Edit: Here is what I want to achieve, perhaps I'll be able to understand the behavior by getting some settings suggestions as opposed to the specific values of the php.ini settings:

I want the session to last as long as possible, up to (approximately) two days. If the session can last beyond browser close, I would like it to do so (up to approximately two days).

What would I set for php.ini settings (and yes, I have direct edit access to the php.ini) to acheive that?

标签: config php
5条回答
对你真心纯属浪费
2楼-- · 2019-02-17 20:18

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.

查看更多
一夜七次
3楼-- · 2019-02-17 20:18

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.

查看更多
Rolldiameter
4楼-- · 2019-02-17 20:19

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.

查看更多
对你真心纯属浪费
5楼-- · 2019-02-17 20:27

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

查看更多
等我变得足够好
6楼-- · 2019-02-17 20:30

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.

查看更多
登录 后发表回答