]> granicus.if.org Git - php/commitdiff
Implemented Request #11100 (session_gc() function).
authorYasuo Ohgaki <yohgaki@php.net>
Sat, 10 Aug 2013 06:20:24 +0000 (15:20 +0900)
committerYasuo Ohgaki <yohgaki@php.net>
Sat, 10 Aug 2013 06:20:24 +0000 (15:20 +0900)
NEWS
ext/session/session.c
ext/session/tests/session_gc_basic.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index b444b448a19365170081ef2abab60ed2a276c9d7..ab9edc8c8e82cd2348da8904445b5c9db6a1cf6d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,7 @@ PHP                                                                        NEWS
     (Yasuo)
   . Implemented Request #54649 (Create session_serializer_name()). (Yasuo)
   . Implemented Request #17860 (Session write short circuit). (Yasuo)
+  . Implemented Request #11100 (session_gc() function). (Yasuo)
 
 - mysqlnd:
   . Disabled flag for SP OUT variables for 5.5+ servers as they are not natively
index d0bafa2bee6a730c4f1085f5bcb0664d7f205ef9..7a4e601e10ea772b5d03a08b57973ef5cebffedc 100644 (file)
@@ -2068,6 +2068,39 @@ static PHP_FUNCTION(session_status)
 }
 /* }}} */
 
+/* {{{ proto int session_gc([int maxlifetime])
+   Execute garbage collection returns number of deleted data */
+static PHP_FUNCTION(session_gc)
+{
+       int nrdels = -1;
+       long maxlifetime = PS(gc_maxlifetime);
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &maxlifetime) == FAILURE) {
+               return;
+       }
+
+       /* Session must be active to have PS(mod) */
+       if (PS(session_status) != php_session_active) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to garbage collect without active session");
+               RETURN_FALSE;
+       }
+
+       if (!PS(mod) || !PS(mod)->s_gc) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session save handler does not have gc()");
+               RETURN_FALSE;
+       }
+       PS(mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels TSRMLS_CC);
+
+       if (nrdels < 0) {
+               /* Files save handler return -1 if there is not a permission to remove.
+                  Save handlder should return negative nrdels when something wrong. */
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session gc failed. Check permission or session storage");
+               RETURN_FALSE;
+       }
+       RETURN_LONG((long)nrdels);
+}
+/* }}} */
+
 /* {{{ proto void session_register_shutdown(void)
    Registers session_write_close() as a shutdown function */
 static PHP_FUNCTION(session_register_shutdown)
@@ -2163,6 +2196,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_cookie_params, 0, 0, 1)
        ZEND_ARG_INFO(0, httponly)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO(arginfo_session_gc, 0)
+       ZEND_ARG_INFO(0, maxlifetime)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO(arginfo_session_class_open, 0)
        ZEND_ARG_INFO(0, save_path)
        ZEND_ARG_INFO(0, session_name)
@@ -2213,6 +2250,7 @@ static const zend_function_entry session_functions[] = {
        PHP_FE(session_get_cookie_params, arginfo_session_void)
        PHP_FE(session_write_close,       arginfo_session_void)
        PHP_FE(session_status,            arginfo_session_void)
+       PHP_FE(session_gc,                arginfo_session_gc)
        PHP_FE(session_register_shutdown, arginfo_session_void)
        PHP_FALIAS(session_commit, session_write_close, arginfo_session_void)
        PHP_FE_END
diff --git a/ext/session/tests/session_gc_basic.phpt b/ext/session/tests/session_gc_basic.phpt
new file mode 100644 (file)
index 0000000..498f980
--- /dev/null
@@ -0,0 +1,56 @@
+--TEST--
+Test session_gc() function : basic functionality
+--SKIPIF--
+<?php include('skipif.inc'); ?>
+--INI--
+session.serialize_handler=php
+session.save_handler=files
+session.maxlifetime=782000
+--FILE--
+<?php
+error_reporting(E_ALL);
+
+ob_start();
+
+/*
+ * Prototype : int session_get_cookie_params([int maxlifetime])
+ * Description : Execute gc and return number of deleted data
+ * Source code : ext/session/session.c
+ */
+
+echo "*** Testing session_gc() : basic functionality ***\n";
+
+// Should fail. It requires active session.
+var_dump(session_gc());
+
+session_start();
+// Should succeed with some number
+var_dump(session_gc());
+// Secound time must be int(0)
+var_dump(session_gc());
+session_write_close();
+
+// Start&stop session to generate
+session_start();
+session_write_close();
+session_start();
+session_write_close();
+session_start();
+session_write_close();
+
+session_start();
+$ndeleted = session_gc(0); // Delete all
+var_dump($ndeleted >= 3);
+
+echo "Done".PHP_EOL;
+
+?>
+--EXPECTF--
+*** Testing session_gc() : basic functionality ***
+
+Warning: session_gc(): Trying to garbage collect without active session in %s on line 15
+bool(false)
+int(%d)
+int(0)
+bool(true)
+Done