Skip to content

Commit a472b8d

Browse files
committed
Session: emulates session.cache_limiter & session.cache_expire
1 parent 43e2829 commit a472b8d

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/Http/Session.php

+30
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function start(): void
105105
}
106106

107107
$this->sendCookie();
108+
$this->sendCachingCookie();
108109

109110
$this->initialize();
110111
}
@@ -509,4 +510,33 @@ private function sendCookie(): void
509510
$tmp[0], $cookie['domain'], $cookie['secure'], $cookie['httponly'], $cookie['samesite'] ?? $tmp[1] ?? null
510511
);
511512
}
513+
514+
515+
/**
516+
* Sends the cache control HTTP headers.
517+
*/
518+
private function sendCachingCookie(): void
519+
{
520+
$expire = 60 * ini_get('session.cache_expire');
521+
switch (ini_get('session.cache_limiter')) {
522+
case 'public':
523+
$this->response->setHeader('Expires', Helpers::formatDate(time() + $expire));
524+
$this->response->setHeader('Cache-Control', "public, max-age=$expire");
525+
$this->response->setHeader('Last-Modified', Helpers::formatDate(getlastmod()));
526+
return;
527+
case 'private_no_expire':
528+
$this->response->setHeader('Cache-Control', "private, max-age=$expire");
529+
$this->response->setHeader('Last-Modified', Helpers::formatDate(getlastmod()));
530+
return;
531+
case 'private':
532+
$this->response->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
533+
$this->response->setHeader('Cache-Control', "private, max-age=$expire");
534+
$this->response->setHeader('Last-Modified', Helpers::formatDate(getlastmod()));
535+
return;
536+
case 'nocache':
537+
$this->response->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
538+
$this->response->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate'); // For HTTP/1.1 conforming clients
539+
$this->response->setHeader('Pragma', 'no-cache'); // For HTTP/1.0 conforming clients
540+
}
541+
}
512542
}

tests/Http/Session.cache.phpt

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tester\Assert;
6+
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
11+
test(function () {
12+
$factory = new Nette\Http\RequestFactory;
13+
$response = new Nette\Http\Response;
14+
$session = new Nette\Http\Session($factory->fromGlobals(), $response);
15+
16+
$session->setOptions([
17+
'cache_limiter' => 'public',
18+
'cache_expire' => '180',
19+
]);
20+
$session->start();
21+
22+
Assert::same(
23+
[
24+
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
25+
'Expires' => [Nette\Http\Helpers::formatDate(time() + 180 * 60)],
26+
'Cache-Control' => ['public, max-age=10800'],
27+
'Last-Modified' => [Nette\Http\Helpers::formatDate(getlastmod())],
28+
],
29+
$response->getHeaders()
30+
);
31+
32+
$session->close();
33+
});
34+
35+
36+
test(function () {
37+
$factory = new Nette\Http\RequestFactory;
38+
$response = new Nette\Http\Response;
39+
$session = new Nette\Http\Session($factory->fromGlobals(), $response);
40+
41+
$session->setOptions([
42+
'cache_limiter' => 'private_no_expire',
43+
'cache_expire' => '180',
44+
]);
45+
$session->start();
46+
47+
Assert::same(
48+
[
49+
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
50+
'Cache-Control' => ['private, max-age=10800'],
51+
'Last-Modified' => [Nette\Http\Helpers::formatDate(getlastmod())],
52+
],
53+
$response->getHeaders()
54+
);
55+
56+
$session->close();
57+
});
58+
59+
60+
test(function () {
61+
$factory = new Nette\Http\RequestFactory;
62+
$response = new Nette\Http\Response;
63+
$session = new Nette\Http\Session($factory->fromGlobals(), $response);
64+
65+
$session->setOptions([
66+
'cache_limiter' => 'private',
67+
'cache_expire' => '180',
68+
]);
69+
$session->start();
70+
71+
Assert::same(
72+
[
73+
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
74+
'Expires' => ['Mon, 23 Jan 1978 10:00:00 GMT'],
75+
'Cache-Control' => ['private, max-age=10800'],
76+
'Last-Modified' => [Nette\Http\Helpers::formatDate(getlastmod())],
77+
],
78+
$response->getHeaders()
79+
);
80+
81+
$session->close();
82+
});
83+
84+
85+
test(function () {
86+
$factory = new Nette\Http\RequestFactory;
87+
$response = new Nette\Http\Response;
88+
$session = new Nette\Http\Session($factory->fromGlobals(), $response);
89+
90+
$session->setOptions([
91+
'cache_limiter' => 'nocache',
92+
]);
93+
$session->start();
94+
95+
Assert::same(
96+
[
97+
'Set-Cookie' => ['PHPSESSID=' . $session->getId() . '; path=/; HttpOnly'],
98+
'Expires' => ['Mon, 23 Jan 1978 10:00:00 GMT'],
99+
'Cache-Control' => ['no-store, no-cache, must-revalidate'],
100+
'Pragma' => ['no-cache'],
101+
],
102+
$response->getHeaders()
103+
);
104+
105+
$session->close();
106+
});

0 commit comments

Comments
 (0)