Skip to content

Note where a session was already started #10736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/session/php_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ typedef struct _php_ps_globals {
const ps_module *default_mod;
void *mod_data;
php_session_status session_status;
zend_string *session_started_filename;
uint32_t session_started_lineno;
zend_long gc_probability;
zend_long gc_divisor;
zend_long gc_maxlifetime;
Expand Down
40 changes: 38 additions & 2 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ static inline void php_rinit_session_globals(void) /* {{{ */
}
/* }}} */

static inline void php_session_cleanup_filename(void) /* {{{ */
{
if (PS(session_started_filename)) {
zend_string_release(PS(session_started_filename));
PS(session_started_filename) = NULL;
PS(session_started_lineno) = 0;
}
}
/* }}} */

/* Dispatched by RSHUTDOWN and by php_session_destroy */
static inline void php_rshutdown_session_globals(void) /* {{{ */
{
Expand Down Expand Up @@ -149,6 +159,8 @@ static inline void php_rshutdown_session_globals(void) /* {{{ */
PS(mod_user_class_name) = NULL;
}

php_session_cleanup_filename();

/* User save handlers may end up directly here by misuse, bugs in user script, etc. */
/* Set session status to prevent error while restoring save handler INI value. */
PS(session_status) = php_session_none;
Expand Down Expand Up @@ -465,6 +477,13 @@ static zend_result php_session_initialize(void) /* {{{ */
php_session_decode(val);
zend_string_release_ex(val, 0);
}

php_session_cleanup_filename();
zend_string *session_started_filename = zend_get_executed_filename_ex();
if (session_started_filename != NULL) {
PS(session_started_filename) = zend_string_copy(session_started_filename);
PS(session_started_lineno) = zend_get_executed_lineno();
}
return SUCCESS;
}
/* }}} */
Expand Down Expand Up @@ -1490,7 +1509,14 @@ PHPAPI zend_result php_session_start(void) /* {{{ */

switch (PS(session_status)) {
case php_session_active:
php_error(E_NOTICE, "Ignoring session_start() because a session has already been started");
if (PS(session_started_filename)) {
php_error(E_NOTICE, "Ignoring session_start() because a session has already been started (started from %s on line %"PRIu32")", ZSTR_VAL(PS(session_started_filename)), PS(session_started_lineno));
} else if (PS(auto_start)) {
/* This option can't be changed at runtime, so we can assume it's because of this */
php_error(E_NOTICE, "Ignoring session_start() because a session has already been started automatically");
} else {
php_error(E_NOTICE, "Ignoring session_start() because a session has already been started");
}
return FAILURE;
break;

Expand Down Expand Up @@ -1600,6 +1626,7 @@ PHPAPI zend_result php_session_start(void) /* {{{ */
}
return FAILURE;
}

return SUCCESS;
}
/* }}} */
Expand Down Expand Up @@ -2513,7 +2540,14 @@ PHP_FUNCTION(session_start)
}

if (PS(session_status) == php_session_active) {
php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already active");
if (PS(session_started_filename)) {
php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already active (started from %s on line %"PRIu32")", ZSTR_VAL(PS(session_started_filename)), PS(session_started_lineno));
} else if (PS(auto_start)) {
/* This option can't be changed at runtime, so we can assume it's because of this */
php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already automatically active");
} else {
php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already active");
}
RETURN_TRUE;
}

Expand Down Expand Up @@ -2846,6 +2880,8 @@ static PHP_MINIT_FUNCTION(session) /* {{{ */
PS(module_number) = module_number;

PS(session_status) = php_session_none;
PS(session_started_filename) = NULL;
PS(session_started_lineno) = 0;
REGISTER_INI_ENTRIES();

#ifdef HAVE_LIBMM
Expand Down
8 changes: 4 additions & 4 deletions ext/session/tests/session_start_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ ob_end_flush();
*** Testing session_start() : variation ***
bool(true)

Notice: session_start(): Ignoring session_start() because a session is already active in %s on line %d
Notice: session_start(): Ignoring session_start() because a session is already active (started from %s on line %d) in %s on line %d
bool(true)

Notice: session_start(): Ignoring session_start() because a session is already active in %s on line %d
Notice: session_start(): Ignoring session_start() because a session is already active (started from %s on line %d) in %s on line %d
bool(true)

Notice: session_start(): Ignoring session_start() because a session is already active in %s on line %d
Notice: session_start(): Ignoring session_start() because a session is already active (started from %s on line %d) in %s on line %d
bool(true)

Notice: session_start(): Ignoring session_start() because a session is already active in %s on line %d
Notice: session_start(): Ignoring session_start() because a session is already active (started from %s on line %d) in %s on line %d
bool(true)
Done
2 changes: 1 addition & 1 deletion ext/session/tests/session_start_variation9.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ob_end_flush();
*** Testing session_start() : variation ***
string(%d) "%s"

Notice: session_start(): Ignoring session_start() because a session is already active in %s on line %d
Notice: session_start(): Ignoring session_start() because a session is already automatically active in %s on line %d
bool(true)
string(%d) "%s"
bool(true)
Expand Down