-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch5.php
266 lines (248 loc) · 5.45 KB
/
ch5.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
*
* Created by PhpStorm.
* User: Tim
* Date: 6-6-2015
* Time: 12:32
*/
include_once('generalIncludes.php');
echo '<input id="chapter" type="hidden" value="5">';
echo ('<h2>Chapter 6 Files, Streams, and Network Programming - Paragraph paragraph Reading files with file handles</h2>');
echo "<h3 id='numbertypes'>Listing 5.1: An HTML form</h3>";
echo "Not really useful since it does not work with eval() (yet)";
showcode(<<<'CODE'
<!--Form submitted with GET-->
<form action="generalIncludes.php" method="GET">
List: <input type="text" name="list" /><br />
Order by:
<select name="orderby">
<option value="name">Name</option>
<option value="city">City</option>
<option value="zip">ZIP Code</option>
</select><br />
Sort order:
<select name="direction">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>
</form>
<!--Form submitted with POST-->
<form action="generalIncludes.php" method="POST">
<input type="hidden" name="login" value="1" />
<input type="text" name="user" />
<input type="password" name="pass" />
</form>
CODE
,4);
echo '<h3>Listing 5.2: An HTML form with array notation</h3>';
showcode(<<<'CODE'
<form method="post">
<p>
Please choose all languages you currently know or
would like to learn in the next 12 months.
</p>
<p>
<label>
<input type="checkbox"
name="languages[]"
value="PHP" />
PHP
</label>
<label>
<input type="checkbox"
name="languages[]"
value="Perl" />
Perl
</label>
<label>
<input type="checkbox"
name="languages[]"
value="Ruby" />
Ruby
</label>
<br />
<input type="submit" value="Send" name="poll" />
</p>
</form>
foreach ($_POST['languages'] as $language) {
switch ($language) {
case 'PHP' :
echo "PHP? Awesome! <br />";
break;
case 'Perl' :
echo "Perl? Ew. Just Ew. <br />";
break;
case 'Ruby' :
echo "Ruby? Can you say... 'bandwagon?' <br />";
break;
default:
echo "Unknown language!";
}
}
CODE
,4);
echo '<h3>Listing 5.7: Custom session handler class before 5.4</h3>';
showcode(<<<'CODE'
class JsonSessionHandler
{
protected $save_path;
protected $file;
public function open($save_path, $session_id)
{
$this->save_path = $save_path;
$this->file = $save_path
. DIRECTORY_SEPARATOR
. $session_id
. '.json';
return is_writable($save_path);
}
public function close()
{
return is_writeable($this->file);
}
public function read($session_id)
{
return json_decode(file_get_contents($this->file));
}
public function write($session_id, $data)
{
return (bool)file_put_contents(json_encode($data));
}
public function destroy($session_id)
{
unlink($this->file);
return !is_file($this->file);
}
public function gc($maxlifetime)
{
$timeout = time() - $maxlifetime;
$files = glob($this->save_path
. DIRECTORY_SEPARATOR
. '*.json');
foreach ($files as $file) {
if (filemtime($file) < $timeout) {
unlink($file);
}
}
}
}
$handler = new JsonSessionHandler;
session_set_save_handler(
[$handler, 'open'],
[$handler, 'close'],
[$handler, 'read'],
[$handler, 'write'],
[$handler, 'destroy'],
[$handler, 'gc']
);
CODE
);
echo '<h3>Listing 5.8: Custom session handler class with 5.4</h3>';
showcode(<<<'CODE'
class JsonSessionHandler implements SessionHandlerInterface
{
protected $save_path;
protected $file;
public function open($save_path, $session_id)
{
$this->save_path = $save_path;
$this->file = $save_path
. DIRECTORY_SEPARATOR
. $session_id
. '.json';
return is_writable($save_path);
}
public function close()
{
return is_writeable($this->file);
}
public function read($session_id)
{
return json_decode(file_get_contents($this->file));
}
public function write($session_id, $data)
{
return (bool)file_put_contents(json_encode($data));
}
public function destroy($session_id)
{
unlink($this->file);
return !is_file($this->file);
}
public function gc($maxlifetime)
{
$timeout = time() - $maxlifetime;
$files = glob($this->save_path
. DIRECTORY_SEPARATOR
. '*.json');
foreach ($files as $file) {
if (filemtime($file) < $timeout) {
unlink($file);
}
}
}
}
$handler = new JsonSessionHandler;
session_set_save_handler($handler);
CODE
);
echo "<h3 id='variableinterpolation'>Variable interpolation</h3>";
showcode(<<<'CODE'
$who = "World";
echo "Hello $who\n";
echo 'Hello $who\n';
CODE
);
showcode(<<<'CODE'
$me = 'Davey';
$names = array('Smith', 'Jones', 'Jackson');
echo "There cannot be more than two {$me}s!";
echo "Citation: {$names[1]}[1987]";
CODE
);
showcode(<<<'CODE'
CODE
);
echo "<h3 id='heredoc'>The Heredoc and Nowdoc Syntax</h3>";
showcode(<<<'CODE'
$who = "World";
echo <<<TEXT
So I said, "Hello $who"
TEXT;
CODE
);
showcode(<<<'CODE'
$who = "World";
echo <<<'TEXT'
So I said, "Hello $who"
TEXT;
CODE
);
showcode(<<<'CODE'
class Hello {
public $greeting = <<<EOT
Hello World
EOT;
}
$hi = new Hello;
echo $hi->greeting;
CODE
);
showcode(<<<'CODE'
CODE
);
echo "<h3 id='escaping'>Escaping literal values</h3>";
showcode(<<<'CODE'
echo 'This is \'my\' string';
CODE
);
showcode(<<<'CODE'
$a = 10;
echo "The value of \$a is \"$a\".";
CODE
);
showcode(<<<'CODE'
echo "Here's an escaped backslash: - \\ -";
CODE
);