If you have used the session in asp.net and used the session in php, you will feel that the session in php is so uncomfortable than the session in asp.net. When using a php session, you may encounter a session that does not expire. closing the browser session still exists and re-open the browser sess... "/> <scripttype =" t
If you have used the session in asp.net and used the session in php, you will feel that the session in php is so uncomfortable than the session in asp.net. When using a php session, you may encounter problems such as the session does not expire, closing the browser session still exists, and re-opening the browser sessionid is the same as before...
Let's take a look at the php session mechanism:
Session collection mechanism:
PHP uses the Garbage Collection process to recycle expired sessions. However, not every session creation can evoke the 'garbage collection 'process. gc is started based on a certain probability. This is mainly due to the consideration of server performance. each session triggers gc. if the page views are large, the server cannot afford it. However, gc is enabled based on a certain probability. when the traffic volume is large, the session expiration mechanism can run normally, and the server efficiency is reduced. The details are derived from years of experience.
Three parameters related to PHP session expiration (in php. ini ):
Session. gc_probability = 1
Session. gc_divisor = 1000
Session. gc_maxlife time = 1440
Gc startup probability = gc_probability/gc_divisor = 0.1%
Session expiration time gc_maxlifetime unit: Seconds
When a web service is officially provided, the session expiration probability needs to be comprehensively considered based on the web service page views and server performance. It is obviously unwise to enable gc for each session. it feels a bit "luck" and a little less likely to hit the page if the traffic is small. During my local testing, I almost never got hit, and the sessionid remains unchanged for several days, even if the machine is restarted. During the test, this expiration probability value must be set to a higher hit probability.
By modifying the expiration probability value of the php configuration file, you can set the session expiration by "Taking Chances". Is there a better way?
The session class written below can completely solve the problem that the session does not expire and the sessionid remains unchanged.
[Php]
/**
* Extended Session class (simple encapsulation)
*
* @ Author slimboy
*
*/
Class Session {
/**
* Initialization
*/
Static function _ init (){
Ini_set ('session. auto_start ', 0 );
// Session: start ();
}
/**
* Start the Session
*/
Static function start (){
Session_start ();
}
/**
* Set the Session
*
* @ Param $ name Session name
* @ Param $ value
* @ Param $ time timeout (seconds)
*/
Public static function set ($ name, $ value, $ time ){
If (empty ($ time )){
$ Time = 1800; // default value
}
$ _ SESSION [$ name] = $ value;
$ _ SESSION [$ name. '_ Expires'] = time () + $ time;
}
/**
* Obtain the Session value.
*
* @ Param $ name Session name
*/
Public static function get ($ name ){
// Check whether the Session has expired
If (isset ($ _ SESSION [$ name. '_ Expires']) & $ _ SESSION [$ name. '_ Expires']> time ()){
Return $ _ SESSION [$ name];
} Else {
Session: clear ($ name );
Return null;
}
}
/**
* Set Session Domain
*
* @ Param $ sessionDomain domain
* @ Return string
*/
Static function setDomain ($ sessionDomain = null ){
$ Return = ini_get ('session. cookie_domain ');
If (! Empty ($ sessionDomain )){
Ini_set ('session. cookie_domain ', $ sessionDomain); // Cross-origin access session
}
Return $ return;
}
/**
* Clear a Session value.
*
* @ Param $ name Session name
*/
Static function clear ($ name ){
Unset ($ _ SESSION [$ name]);
Unset ($ _ SESSION [$ name. '_ Expires']);
}
/**
* Reset and destroy the Session
*/
Static function destroy (){
Unset ($ _ SESSION );
Session_destroy ();
}
/**
* Get or set the Session id
*/
Static function sessionid ($ id = null ){
Return session_id ($ id );
}
}
Session: _ init ();
/**
* Extended Session class (simple encapsulation)
*
* @ Author slimboy
*
*/
Class Session {
/**
* Initialization
*/
Static function _ init (){
Ini_set ('session. auto_start ', 0 );
// Session: start ();
}
/**
* Start the Session
*/
Static function start (){
Session_start ();
}
/**
* Set the Session
*
* @ Param $ name Session name
* @ Param $ value
* @ Param $ time timeout (seconds)
*/
Public static function set ($ name, $ value, $ time ){
If (empty ($ time )){
$ Time = 1800; // default value
}
$ _ SESSION [$ name] = $ value;
$ _ SESSION [$ name. '_ Expires'] = time () + $ time;
}
/**
* Obtain the Session value.
*
* @ Param $ name Session name
*/
Public static function get ($ name ){
// Check whether the Session has expired
If (isset ($ _ SESSION [$ name. '_ Expires']) & $ _ SESSION [$ name. '_ Expires']> time ()){
Return $ _ SESSION [$ name];
} Else {
Session: clear ($ name );
Return null;
}
}
/**
* Set Session Domain
*
* @ Param $ sessionDomain domain
* @ Return string
*/
Static function setDomain ($ sessionDomain = null ){
$ Return = ini_get ('session. cookie_domain ');
If (! Empty ($ sessionDomain )){
Ini_set ('session. cookie_domain ', $ sessionDomain); // Cross-origin access session
}
Return $ return;
}
/**
* Clear a Session value.
*
* @ Param $ name Session name
*/
Static function clear ($ name ){
Unset ($ _ SESSION [$ name]);
Unset ($ _ SESSION [$ name. '_ Expires']);
}
/**
* Reset and destroy the Session
*/
Static function destroy (){
Unset ($ _ SESSION );
Session_destroy ();
}
/**
* Get or set the Session id
*/
Static function sessionid ($ id = null ){
Return session_id ($ id );
}
}
Session: _ init ();
Call example:
[Php]
// Set the session
Session: set ('userid', $ UserId, 3600 );
// Set the session
Session: set ('userid', $ UserId, 3600 );
[Php]
// Read the session
$ UserId = Session: get ('userid ');
// Read the session
$ UserId = Session: get ('userid ');
Of course, there should be other methods. welcome to the kids shoes!