From 93a60507fc7451b6ce987978dc09ac7e0cea636f Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 24 Sep 2020 16:30:39 +1000 Subject: [PATCH 1/1] MDL-69779 core: Improved the X-Redirect-By header when debugging is on --- lib/upgrade.txt | 1 + lib/weblib.php | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/upgrade.txt b/lib/upgrade.txt index c5fe41f75e1..99afdf138a8 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -52,6 +52,7 @@ information provided here is intended especially for developers. renamed to `is_listed()` and `get_not_listed()` respectively. * Method `mustache_helper_collection::strip_blacklisted_helpers()` has been deprecated and renamed to `strip_disallowed_helpers()`. +* Function redirect() now emits a line of backtrace into the X-Redirect-By header when debugging is one === 3.9 === * Following function has been deprecated, please use \core\task\manager::run_from_cli(). diff --git a/lib/weblib.php b/lib/weblib.php index 29ef0e7168d..42518461eb6 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2954,9 +2954,17 @@ function redirect($url, $message='', $delay=null, $messagetype = \core\output\no \core\session\manager::write_close(); if ($delay == 0 && !$debugdisableredirect && !headers_sent()) { + // This helps when debugging redirect issues like loops and it is not clear - // which layer in the stack sent the redirect header. - @header('X-Redirect-By: Moodle'); + // which layer in the stack sent the redirect header. If debugging is on + // then the file and line is also shown. + $redirectby = 'Moodle'; + if (debugging('', DEBUG_DEVELOPER)) { + $origin = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; + $redirectby .= ' /' . str_replace($CFG->dirroot . '/', '', $origin['file']) . ':' . $origin['line']; + } + @header("X-Redirect-By: $redirectby"); + // 302 might not work for POST requests, 303 is ignored by obsolete clients. @header($_SERVER['SERVER_PROTOCOL'] . ' 303 See Other'); @header('Location: '.$url); -- 2.43.0