diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 18e8d9eaaf2e..d040e6f01787 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -1031,6 +1031,11 @@ public function storePreviousURL($uri) return; } + // Ignore non-HTML responses + if (strpos($this->response->getHeaderLine('Content-Type'), 'text/html') === false) { + return; + } + // This is mainly needed during testing... if (is_string($uri)) { $uri = new URI($uri); diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index 8245d3bf18df..29d511d2a9d3 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -424,6 +424,30 @@ public function testNotStoresPreviousURL() $this->assertArrayNotHasKey('_ci_previous_url', $_SESSION); } + public function testNotStoresPreviousURLByCheckingContentType() + { + $_SERVER['argv'] = ['index.php', 'image']; + $_SERVER['argc'] = 2; + + $_SERVER['REQUEST_URI'] = '/image'; + + // Inject mock router. + $routes = Services::routes(); + $routes->add('image', static function () { + $response = Services::response(); + + return $response->setContentType('image/jpeg', ''); + }); + $router = Services::router($routes, Services::request()); + Services::injectMock('router', $router); + + ob_start(); + $this->codeigniter->useSafeOutput(true)->run(); + ob_get_clean(); + + $this->assertArrayNotHasKey('_ci_previous_url', $_SESSION); + } + /** * The method after all test, reset Servces:: config * Can't use static::tearDownAfterClass. This will cause a buffer exception diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index 077cf64a1ac8..32df0dbdf3d3 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -32,6 +32,7 @@ Behavior Changes - To prevent unexpected access from the web browser, if a controller is added to a cli route (``$routes->cli()``), all methods of that controller are no longer accessible via auto-routing. - There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method. - The :php:func:`dot_array_search`'s unexpected behavior has been fixed. Now ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returns ``null``. The previous versions returned ``23``. +- The ``CodeIgniter::storePreviousURL()`` has been changed to store only the URLs whose Content-Type was ``text/html``. It also affects the behavior of :php:func:`previous_url` and :php:func:`redirect()->back() `. Enhancements ************