首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

session_gc

(PHP 7 >= 7.1.0)

session_gc — Perform session data garbage collection

Description

代码语言:javascript
复制
int session_gc ( void )

session_gc() is used to perform session data GC(garbage collection). PHP does probability based session GC by default.

Probability based GC works somewhat but it has few problems. 1) Low traffic sites' session data may not be deleted within the preferred duration. 2) High traffic sites' GC may be too frequent GC. 3) GC is performed on the user's request and the user will experience a GC delay.

Therefore, it is recommended to execute GC periodically for production systems using, e.g., "cron" for UNIX-like systems. Make sure to disable probability based GC by setting session.gc_probability to 0.

Return Values

session_gc() returns number of deleted session data for success, FALSE for failure.

Old save handlers do not return number of deleted session data, but only success/failure flag. If this is the case, number of deleted session data became 1 regardless of actually deleted data.

Examples

Example #1 session_gc() example for task managers like cron

代码语言:javascript
复制
<?php
//?Note:?This?script?should?be?executed?by?the?same?user?of?web?server?process.

//?Need?active?session?to?initialize?session?data?storage?access.
session_start();

//?Executes?GC?immediately
session_gc();

//?Clean?up?session?ID?created?by?session_gc()
session_destroy();
?>

Example #2 session_gc() example for user accessible script

代码语言:javascript
复制
<?php
//?Note:?session_gc()?is?recommended?to?be?used?by?task?manager?script,?but
//?it?may?be?used?as?follows.

//?Used?for?last?GC?time?check
$gc_time?=?'/tmp/php_session_last_gc';
$gc_period?=?1800;

session_start();
//?Execute?GC?only?when?GC?period?elapsed.?
//?i.e.?Calling?session_gc()?every?request?is?waste?of?resources.?
if?(file_exists($gc_time))?{
????if?(filemtime($gc_time)?<?time()?-?$gc_period)?{
????????session_gc();
????????touch($gc_time);
????}
}?else?{
????touch($gc_time);
}
?>

See Also

  • session_start() - Start new or resume existing session
  • session_destroy() - Destroys all data registered to a session

← session_encode

session_get_cookie_params →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com