-
Notifications
You must be signed in to change notification settings - Fork 25
/
demo-session-exclude-keys.php
128 lines (111 loc) · 4.1 KB
/
demo-session-exclude-keys.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* This file is part of the PageCache package.
*
* @author Muhammed Mamedov <mm@turkmenweb.net>
* @copyright 2016
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* This demo demonstrates caching functionality of PageCache.
*
* We exclude certain session keys from caching logic
*
*/
require_once __DIR__ . '/../vendor/autoload.php';
session_start();
session_destroy();
if (isset($_POST['withsessions'])) {
if ($_POST['withsessions'] == '1') {
$_SESSION['excl'] = 1;
$_SESSION['PageCache'] = 'PHP full page caching';
} elseif ($_POST['withsessions'] == '2') {
$_SESSION['excl'] = 555;
$_SESSION['PageCache'] = 'PHP full page caching';
}
}
echo '<fieldset style="background-color: #eee; padding: 10px;">var_dump($_SESSION) call, before init(),
so this content is not cached.
Notice how with each button click below actual session value changes, but since it is excluded from tracking,
same cache for different session values is generated: ';
var_dump($_SESSION);
echo 'var_dump ends. All below is cached.</fieldset>';
$cache = new PageCache\PageCache(__DIR__ . '/config.php');
$cache->config()->setUseSession(true)
// Exclude $_SESSION['exc'] from cache strategy.
// Comment line below, and cached version for each 'excl' session variable
// will be saved in a different cache file
->setSessionExcludeKeys(array('excl'));
//init
$cache->init();
?>
<html xmlns="http://www.w3.org/1999/html">
<head>
<style type="text/css">
button {
margin: 0;
padding: 10px;
font-weight: bold;
}
fieldset {
margin-bottom: 10px;
background-color: #eee;
}
form {
float: left;
margin: 0;
margin-right: 20px;
padding: 20px;
}
</style>
</head>
<body>
<h1>Demo using Session Support + Exclude Session Keys</h1>
Click on the links below to see how PageCache works with session exclude. Although page URL doesn't change, PageCache is
able to cache 2 different version of this page based on Session variables.
Session key 'exc' changes, but PageCache ignores it and produces only a single cached page. If you don't call
excludeKeys(), 2 versions of the page will be generated. <br>
<br/>Whichever button you press first, it will be cached, while other won't - because 'excl' parameter doesn't effect
cache. <br/>But you will still get 2 caches of this page because $_SESSION['PageCache'] is being set, and this change is
being recorded.
<br/><br>
<fieldset>
<form>
<a href="demo-session-exclude-keys.php">Main</a>
</form>
<form method="post" action="">
<input type="hidden" value="1" name="withsessions">
<button type="submit">Set $_SESSION['excl']= 1</button>
</form>
<form method="post" action="">
<input type="hidden" value="2" name="withsessions">
<button type="submit">Set $_SESSION['excl'] = 555</button>
</form>
<br class="clear"/><br/>
<code>PageCache call demo: $cache->config()->setSessionExcludeKeys(array('excl'));</code>
</fieldset>
<?php
/**
* Print session data if present
*/
if (isset($_SESSION['excl'])) {
var_dump($_SESSION);
}
echo '<br><b>Stored under cache key: </b><fieldset>'
. ($cache->getCurrentKey())
. '</fieldset>';
?>
<br>
<h3 style="color: red">This is a Session Exclude demo PageCache page that is going to be cached.</h3>
<h3>Default cache expiration time for this page is 20 minutes. You can change this value in your <i>conf.php</i>
and passing its file path to PageCache constructor, or by calling <i>setExpiration()</i> method.</h3>
<h3 style="color: green;">This is a dynamic PHP <i>date('H:i:s')</i>
call, note that time doesn't change on refresh: <?php echo date('H:i:s'); ?>.</h3>
<br>
<h4>Check examples/cache/ directory to see cached content.
Erase this file to regenerate cache, or it will automatically be regenerated in 20 minutes.</h4>
</body>
</html>