Skip to content

Commit cae264a

Browse files
authored
Merge pull request #6012 from tearoom6/fix/previous_url_only_html
2 parents f012cc7 + a86e392 commit cae264a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

system/CodeIgniter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,11 @@ public function storePreviousURL($uri)
10311031
return;
10321032
}
10331033

1034+
// Ignore non-HTML responses
1035+
if (strpos($this->response->getHeaderLine('Content-Type'), 'text/html') === false) {
1036+
return;
1037+
}
1038+
10341039
// This is mainly needed during testing...
10351040
if (is_string($uri)) {
10361041
$uri = new URI($uri);

tests/system/CodeIgniterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,30 @@ public function testNotStoresPreviousURL()
424424
$this->assertArrayNotHasKey('_ci_previous_url', $_SESSION);
425425
}
426426

427+
public function testNotStoresPreviousURLByCheckingContentType()
428+
{
429+
$_SERVER['argv'] = ['index.php', 'image'];
430+
$_SERVER['argc'] = 2;
431+
432+
$_SERVER['REQUEST_URI'] = '/image';
433+
434+
// Inject mock router.
435+
$routes = Services::routes();
436+
$routes->add('image', static function () {
437+
$response = Services::response();
438+
439+
return $response->setContentType('image/jpeg', '');
440+
});
441+
$router = Services::router($routes, Services::request());
442+
Services::injectMock('router', $router);
443+
444+
ob_start();
445+
$this->codeigniter->useSafeOutput(true)->run();
446+
ob_get_clean();
447+
448+
$this->assertArrayNotHasKey('_ci_previous_url', $_SESSION);
449+
}
450+
427451
/**
428452
* The method after all test, reset Servces:: config
429453
* Can't use static::tearDownAfterClass. This will cause a buffer exception

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Behavior Changes
3232
- 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.
3333
- There is a possible backward compatibility break for those users extending the History Collector and they should probably update ``History::setFiles()`` method.
3434
- 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``.
35+
- 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() <redirect>`.
3536

3637
Enhancements
3738
************

0 commit comments

Comments
 (0)