Skip to content

Commit

Permalink
Merge pull request codeigniter4#2697 from michalsn/session_bug
Browse files Browse the repository at this point in the history
Fix session bug when the request is ajax call
  • Loading branch information
MGatner authored Mar 13, 2020
2 parents a1411ab + 1583981 commit 9506609
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public function get(string $key = null)
}
elseif (empty($_SESSION))
{
return [];
return $key === null ? [] : null;
}

if (! empty($key))
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ public function testGetReturnsNullWhenNotFound()
$this->assertNull($session->get('foo'));
}

public function testGetReturnsNullWhenNotFoundWithXmlHttpRequest()
{
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
$_SESSION = [];

$session = $this->getInstance();
$session->start();

$this->assertNull($session->get('foo'));
}

public function testGetReturnsEmptyArrayWhenWithXmlHttpRequest()
{
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
$_SESSION = [];

$session = $this->getInstance();
$session->start();

$this->assertEquals([], $session->get());
}

public function testGetReturnsItemValueisZero()
{
$_SESSION = [];
Expand Down

0 comments on commit 9506609

Please sign in to comment.