From: Jani Taskinen Date: Wed, 6 Aug 2008 05:34:55 +0000 (+0000) Subject: - Fixed some inconsistencies with the behaviour of sessions. Changed session_start... X-Git-Tag: BEFORE_HEAD_NS_CHANGE~850 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2faeee57f42ff7b077094315de16a7863d0cf0f8;p=php - Fixed some inconsistencies with the behaviour of sessions. Changed session_start() to return false when session start fails. --- diff --git a/ext/session/session.c b/ext/session/session.c index 8a959b84cf..f254579f7c 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -508,7 +508,10 @@ static PHP_INI_MH(OnUpdateSaveHandler) /* {{{ */ err_type = E_ERROR; } - php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find save handler %s", new_value); + /* Do not output error when restoring ini options. */ + if (stage != ZEND_INI_STAGE_DEACTIVATE) { + php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find save handler '%s'", new_value); + } return FAILURE; } PS(mod) = tmp; @@ -517,20 +520,6 @@ static PHP_INI_MH(OnUpdateSaveHandler) /* {{{ */ } /* }}} */ -static PHP_INI_MH(OnUpdateTransSid) /* {{{ */ -{ - SESSION_CHECK_ACTIVE_STATE; - - if (!strncasecmp(new_value, "on", sizeof("on"))) { - PS(use_trans_sid) = (zend_bool) 1; - } else { - PS(use_trans_sid) = (zend_bool) atoi(new_value); - } - - return SUCCESS; -} -/* }}} */ - static PHP_INI_MH(OnUpdateSerializer) /* {{{ */ { const ps_serializer *tmp; @@ -547,7 +536,10 @@ static PHP_INI_MH(OnUpdateSerializer) /* {{{ */ err_type = E_ERROR; } - php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find serialization handler '%s'", new_value); + /* Do not output error when restoring ini options. */ + if (stage != ZEND_INI_STAGE_DEACTIVATE) { + php_error_docref(NULL TSRMLS_CC, err_type, "Cannot find serialization handler '%s'", new_value); + } return FAILURE; } PS(serializer) = tmp; @@ -556,6 +548,20 @@ static PHP_INI_MH(OnUpdateSerializer) /* {{{ */ } /* }}} */ +static PHP_INI_MH(OnUpdateTransSid) /* {{{ */ +{ + SESSION_CHECK_ACTIVE_STATE; + + if (!strncasecmp(new_value, "on", sizeof("on"))) { + PS(use_trans_sid) = (zend_bool) 1; + } else { + PS(use_trans_sid) = (zend_bool) atoi(new_value); + } + + return SUCCESS; +} +/* }}} */ + static PHP_INI_MH(OnUpdateSaveDir) /* {{{ */ { /* Only do the safemode/open_basedir check at runtime */ @@ -1236,30 +1242,42 @@ PHPAPI void php_session_start(TSRMLS_D) /* {{{ */ { zval **ppid; zval **data; - char *p; + char *p, *value; int nrand; int lensess; PS(apply_trans_sid) = PS(use_trans_sid); - if (PS(session_status) != php_session_none) { - if (PS(session_status) == php_session_disabled) { - char *value; + switch (PS(session_status)) { + case php_session_active: + php_error(E_NOTICE, "A session had already been started - ignoring session_start()"); + return; + break; + case php_session_disabled: value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0); - - if (value) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find save handler %s", value); - } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find unknown save handler"); + if (!PS(mod) && value) { + PS(mod) = _php_find_ps_module(value TSRMLS_CC); + if (!PS(mod)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find save handler '%s' - session startup failed", value); + return; + } } - return; - } - php_error(E_NOTICE, "A session had already been started - ignoring session_start()"); - return; - } else { - PS(define_sid) = 1; - PS(send_cookie) = 1; + value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0); + if (!PS(serializer) && value) { + PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC); + if (!PS(serializer)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot find serialization handler '%s' - session startup failed", value); + return; + } + } + PS(session_status) = php_session_none; + /* fallthrough */ + + default: + case php_session_none: + PS(define_sid) = 1; + PS(send_cookie) = 1; } lensess = strlen(PS(session_name)); @@ -1560,7 +1578,6 @@ static PHP_FUNCTION(session_save_path) if (name) { if (memchr(name, '\0', name_len) != NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "The save_path cannot contain NULL characters"); - zval_dtor(return_value); RETURN_FALSE; } @@ -1762,6 +1779,10 @@ static PHP_FUNCTION(session_start) { /* skipping check for non-zero args for performance reasons here ?*/ php_session_start(TSRMLS_C); + + if (PS(session_status) != php_session_active) { + RETURN_FALSE; + } RETURN_TRUE; } /* }}} */ @@ -1778,7 +1799,6 @@ static PHP_FUNCTION(session_destroy) } /* }}} */ - /* {{{ proto void session_unset(void) U Unset all registered variables */ static PHP_FUNCTION(session_unset) @@ -1968,7 +1988,7 @@ static PHP_GINIT_FUNCTION(ps) /* {{{ */ } ps_globals->http_session_vars = NULL; } -/* }}}*/ +/* }}} */ static PHP_MINIT_FUNCTION(session) /* {{{ */ { @@ -2048,7 +2068,7 @@ static PHP_MINFO_FUNCTION(session) /* {{{ */ DISPLAY_INI_ENTRIES(); } -/* }}} */ +/* }}} */ zend_module_entry session_module_entry = { STANDARD_MODULE_HEADER, diff --git a/ext/session/tests/session_decode_variation3.phpt b/ext/session/tests/session_decode_variation3.phpt index 020723c8d3..29ceb3ef3e 100644 --- a/ext/session/tests/session_decode_variation3.phpt +++ b/ext/session/tests/session_decode_variation3.phpt @@ -23,8 +23,8 @@ $_SESSION["foo"] = 1234567890; $_SESSION["bar"] = "Blah!"; $_SESSION["guff"] = 123.456; var_dump($_SESSION); -$encoded = "A2Zvb2k6MTIzNDU2Nzg5MDs="; -var_dump(session_decode(base64_decode($encoded))); +$encoded = "foo|i:1234567890;"; +var_dump(session_decode($encoded)); var_dump($_SESSION); var_dump(session_destroy()); @@ -34,10 +34,11 @@ ob_end_flush(); --EXPECTF-- *** Testing session_decode() : variation *** -Warning: session_start(): Unknown session.serialize_handler. Failed to decode session object in %s on line %d -bool(true) -array(0) { -} +Warning: session_start(): Cannot find serialization handler 'blah' - session startup failed in %s on line %d +bool(false) + +Notice: Undefined variable: _SESSION in %s on line %d +NULL array(3) { [u"foo"]=> int(1234567890) @@ -49,14 +50,8 @@ array(3) { Warning: session_decode(): Unknown session.serialize_handler. Failed to decode session object in %s on line %d bool(true) -array(3) { - [u"foo"]=> - int(1234567890) - [u"bar"]=> - unicode(5) "Blah!" - [u"guff"]=> - float(123.456) -} -bool(true) -Done +unicode(17) "foo|i:1234567890;" +Warning: session_destroy(): Trying to destroy uninitialized session in %s on line %d +bool(false) +Done diff --git a/ext/session/tests/session_encode_variation3.phpt b/ext/session/tests/session_encode_variation3.phpt index 3166041a1a..db5a3685f5 100644 --- a/ext/session/tests/session_encode_variation3.phpt +++ b/ext/session/tests/session_encode_variation3.phpt @@ -31,4 +31,3 @@ bool(true) unicode(34) "foo|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}" bool(true) Done - diff --git a/ext/session/tests/session_encode_variation4.phpt b/ext/session/tests/session_encode_variation4.phpt index c2eb7d2ba2..a08b772ea2 100644 --- a/ext/session/tests/session_encode_variation4.phpt +++ b/ext/session/tests/session_encode_variation4.phpt @@ -33,4 +33,3 @@ bool(true) unicode(52) "foo|a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}guff|R:1;blah|R:1;" bool(true) Done - diff --git a/ext/session/tests/session_encode_variation5.phpt b/ext/session/tests/session_encode_variation5.phpt index 4eefa8fcf5..136b64a7a5 100644 --- a/ext/session/tests/session_encode_variation5.phpt +++ b/ext/session/tests/session_encode_variation5.phpt @@ -33,4 +33,3 @@ bool(true) unicode(64) "data|a:5:{i:0;i:1;i:1;i:2;i:2;i:3;U:3:"foo";R:1;U:4:"blah";R:1;}" bool(true) Done - diff --git a/ext/session/tests/session_encode_variation7.phpt b/ext/session/tests/session_encode_variation7.phpt index 21f9974043..0cac8af1af 100644 --- a/ext/session/tests/session_encode_variation7.phpt +++ b/ext/session/tests/session_encode_variation7.phpt @@ -32,4 +32,3 @@ bool(true) string(0) "" bool(true) Done - diff --git a/ext/session/tests/session_encode_variation8.phpt b/ext/session/tests/session_encode_variation8.phpt index 376c9ac1bf..033240aeae 100644 --- a/ext/session/tests/session_encode_variation8.phpt +++ b/ext/session/tests/session_encode_variation8.phpt @@ -29,11 +29,12 @@ ob_end_flush(); --EXPECTF-- *** Testing session_encode() : variation *** -Warning: session_start(): Unknown session.serialize_handler. Failed to decode session object in %s on line %d -bool(true) +Warning: session_start(): Cannot find serialization handler 'blah' - session startup failed in %s on line 13 +bool(false) -Warning: session_encode(): Unknown session.serialize_handler. Failed to encode session object in %s on line %d +Warning: session_encode(): Cannot encode non-existent session in %s on line 15 string(0) "" -bool(true) -Done +Warning: session_destroy(): Trying to destroy uninitialized session in %s on line 17 +bool(false) +Done