-
Notifications
You must be signed in to change notification settings - Fork 25
/
demo-session-support.php
136 lines (115 loc) · 3.94 KB
/
demo-session-support.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
129
130
131
132
133
134
135
136
<?php
/**
* This file is part of the PageCache package.
*
* @author Muhammed Mamedov <[email protected]>
* @copyright 2016
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* This demo demonstrates basic caching functionality of PageCache.
*
* Two versions of this page will be saved.
* If your cache directory is cache, then look for cache/54/3/668.. and cache/97/52/ead.. files.
*
* Despite that URL doesn't change (PageCache DefaultStrategy doesn't support $_POST()), PageCache is able to
* save two different versions of the page based on $_SESSION data.
*
* enableSession() is useful if you are going to cache a dynamic PHP application where you use $_SESSION
* for various things, like user login and etc.
*
*
* NOTE: If you want to cache only URLs before user login or other session manipulations, you could put
* PageCache call inside if(!isset($_SESSION[..])) { //run PageCache only on pages without certain Session variable }
*
*/
require_once __DIR__ . '/../vendor/autoload.php';
/**
* session is started only when "Enable session" button is pressed
*/
if (isset($_POST['withsessions']) && $_POST['withsessions'] == '1') {
session_start();
//sample session data
$_SESSION['demo_session'] = array('my val1', 'my val2');
$_SESSION['user'] = 12;
$_SESSION['login'] = true;
}
$cache = new PageCache\PageCache();
//cache path
$cache->config()->setCachePath(__DIR__ . '/cache/');
//Disable line below and you will see only 1 cache file generated,
// no differentiation between session and without session calls
//
//use session support in cache
//
$cache->config()->setUseSession(true);
//do disable session cache uncomment this line, or comment line above and see
//$cache->config()->setUseSession(false);
//enable log
//$cache->config()->setEnableLog(true);
//$cache->config()->setLogFilePath(__DIR__.'/log/cache.log');
//start cache;
$cache->init();
echo 'Everything below is cached, including this line<hr>';
?>
<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</h1>
Click on the links below to see how PageCache works with sessions. Although page URL doesn't change, PageCache is able
to cache 2 different version of this page based on Session variables.
<br/><br>
<fieldset>
<form method="post" action="">
<input type="hidden" value="1" name="withsessions">
<button type="submit">Enable session cache</button>
</form>
<form method="post" action="">
<input type="hidden" value="0" name="withsessions">
<button type="submit">No session cache</button>
</form>
</fieldset>
<?php
/**
* Print session data if present
*/
if (isset($_SESSION['demo_session'])) {
echo '<b>SESSION IS ENABLED. Session contents:</b> <fieldset><pre>';
print_r($_SESSION);
echo '</pre></fieldset>';
} else {
echo '<fieldset>No session data</fieldset>';
}
?>
<br>
<h3 style="color: red">This is a Session 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>