-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_cheatsheet1.php
475 lines (384 loc) · 11.5 KB
/
php_cheatsheet1.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/* written by Nick Shin - [email protected]
* the code found in this file is licensed under:
* - Unlicense - http://unlicense.org/
*
* this file is from https://github.com/nickshin/CheatSheets/
*
*
* this file contains some of my most used php snipets
* - predefined variables: _GET _POST _SERVER _FILES
* - binary and file handling
*
* and some uses of:
* - passing by reference
* - classes
*
*
* best viewed in editor with tab stops set to 4
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
echo "<pre>\n";
# test fileIO folder...
$testdir = "test_X"; # make sure this is webgroup writable...
# DUMP EVERYTHING THE CLIENT IS SENDING {{{
# ----------------------------------------
print_r($_SERVER);
print_r($_POST);
print_r($_GET);
print_r($_FILES);
# DUMP EVERYTHING THE CLIENT IS SENDING }}}
# PREDEFINED VARIABLES {{{
# ----------------------------------------
# GET submission:
# http://domain.name/submit_method_get.php?param1=value1¶m2=value2
if ( ! empty($_GET) ) {
$getvalue1 = $_GET['param1']; # 'value1'
$getvalue2 = $_GET['param2']; # 'value2'
echo "GET: $getvalue1 & $getvalue2\n";
}
# POST submission:
# http://domain.name/submit_method_post.php
else if ( ! empty($_POST) ) {
$getvalue1 = $_POST['param1']; # 'value1'
$getvalue2 = $_POST['param2']; # 'value2'
echo "POST: $getvalue1 & $getvalue2\n";
}
else {
# here document
print <<<_END_FORM
Via GET:
<form action="submit_method_get.php" method="get">
Name: <input type="text" name="param1" />
Age: <input type="text" name="param2" />
<input type="submit" />
</form>
Via POST:
<form action="submit_method_post.php" method="post">
Name: <input type="text" name="param1" />
Age: <input type="text" name="param2" />
<input type="submit" />
</form>
_END_FORM;
}
# SERVER variables
# use PHPINFO to find out what's available to look at
#phpinfo();
# such as:
# if ( ! isset($_SERVER['HTTPS']) ) die;
# include( "$_SERVER[DOCUMENT_ROOT]/my_includes.php.inc" );
# PREDEFINED VARIABLES }}}
# SNIPETS {{{
# ----------------------------------------
function filesUpload()
{
if ( empty( $_FILES ) ) {
print <<<_END_FILEFORM
<form action="submit_multipart.php" method="post" enctype="multipart/form-data">
File 1: <input type="file" name="file1"/>
File 2: <input type="file" name="file2"/>
<input type="submit" name="submit" value="Submit" />
</form>
_END_FILEFORM;
return;
}
global $testdir;
for ( $i = 1; $i <= 2; $i++ ) {
if ( ! $_FILES["file$i"] ) continue;
if ( $_FILES["file$i"]["error"] > 0 ) {
echo "Error Code: " . $_FILES["file$i"]["error"] . "\n";
continue;
}
echo "Upload: " . $_FILES["file$i"]["name"] . "\n";
echo "Type: " . $_FILES["file$i"]["type"] . "\n";
echo "Size: " . ($_FILES["file$i"]["size"] / 1024) . " Kb\n";
echo "Temp file: " . $_FILES["file$i"]["tmp_name"] . "\n";
move_uploaded_file( $_FILES["file$i"]["tmp_name"],
$testdir . "/" . $_FILES["file$i"]["name"] );
}
}
function fileIOTests( $filename )
{
global $testdir;
$fname = "$testdir/$filename";
# create/append/write
# $f = fopen( $fname, "wb+" ); # this will zap the file to zero
$f = fopen( $fname, "ab+" );
if( $f ){
fseek( $f, 0, SEEK_END );
$data = pack( 'cV4', 1, 1234, 1234, 1234, 1234 );
fwrite( $f, $data );
fclose( $f);
}
# read
if ( file_exists( $fname ) ) {
if ( $f = fopen( $fname, "rb" ) ) {
$data = 0;
$size = filesize( $fname );
# if ( ! $size ) goto doneread;
if ( $size ) {
echo "dumping: $fname:$size\n";
$data = fread( $f, $size );
}
# if ( ! $data ) goto doneread;
if ( $data ) {
$subsize = 0;
while ( $subsize < $size ) {
$entry = substr( $data, $subsize, 17 );
if ( ! $entry ) { echo ""; break; }
$subsize += 17;
$char1 = substr( $entry, 0, 1 );
$charH = bin2hex( $char1 );
$charD = ord( $char1 );
$char16 = bin2hex( substr( $entry, 1, 16 ) );
echo "\tentry: $char16 hex[$charH] dec[$charD]\n";
}
}
#doneread: #goto dne (til PHP:5.3)
fclose($f);
echo "\n";
}
}
}
function binaryRead()
{ # read Raw Post Data
# php://input is not available with enctype='multipart/form-data'
# see filesUpload() to handle 'multipart/form-data'
# and can only be read once, this stream cannot seek
$data = @file_get_contents('php://input');
if ( $data ) {
$results = unpack( 'V1int32_t/c4int8_t', $data );
print_r( $results );
}
# else
# echo "no data\n";
}
function binarySend()
{ # send raw data to client
header( "Content-type: application/octet-stream" );
echo pack( 'Vc*', 1234, 9, 8, 7, 6 );
}
filesUpload();
fileIOTests( "php_test_fileio.bin" );
binaryRead();
#binarySend(); # this can only be done before writing any text/html
# SNIPETS }}}
# PASSING BY REFERENCE {{{
# ----------------------------------------
# passing by reference
function foo( &$var )
{
$var++;
}
$a = 5;
foo( $a ); # $a is 6
#foo($a = 5); # expression, not variable
#foo(5); # fatal error
function &bar()
{
$a = 5;
return $a;
}
foo(bar()); # valid
function noob() # Note the missing &
{
$a = 5;
return $a;
}
#foo( noob() ); # fatal error since PHP 5.0.5
# unsetting a reference, just breaks the binding between variable name and
# variable content. does not mean that variable content will be destroyed.
$b = &$a;
unset( $a ); # won't unset $b, just $a
# PASSING BY REFERENCE }}}
# CLASSES {{{
# ----------------------------------------
# use with PHP compiler to generate bytecodes via bcompiler
# PHP classes default properties to public
# classes
# with extends: multiple inheritance is not supported.
# with interface: multiple inheritance is supported; however, methods are abstract.
class BaseClass
{
# property declaration
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
# method declaration
function printProperties()
{
echo "\tBaseClass: " . $this->public . "\n";
echo "\tBaseClass: " . $this->protected . "\n";
echo "\tBaseClass: " . $this->private . "\n";
}
# using const
const constant = 'constant value';
protected function printConstant() { echo 'Base: ' . self::constant . "\n"; }
# using static
static $singleton = null;
function printStatic() {
if ( ! self::$singleton )
{ self::$singleton = 'am static'; }
echo 'Base: ' . __CLASS__ . ': ' . self::$singleton . "\n";
}
# using constructors and destructors
function __construct() { print "In BaseClass constructor\n"; }
function __destruct() { print "In BaseClass destructor\n"; }
}
class SubClass extends BaseClass
{
# can redeclare the public and protected method but not private
protected $protected = 'Protected2';
# overrides BaseClass::printProperties()
function printProperties()
{
echo "\tSubClass: " . $this->public . "\n";
echo "\tSubClass: " . $this->protected . "\n";
# echo "\tSubClass: " . $this->private . "\n"; # Undefined - warning messages
}
# override a protected method
function printConstant() { echo parent::printConstant(); }
# using constructors and destructors
function __construct() { print "In SubClass constructor\n"; }
function __destruct() { print "In SubClass destructor\n"; }
}
# testing extends usage
$obj1 = new BaseClass();
echo "\t" . $obj1->public . "\n";
#echo "\t" . $obj1->protected . "\n"; # Fatal Error
#echo "\t" . $obj1->private . "\n"; # Fatal Error
$obj1->printProperties(); # Shows Public, Protected and Private
#$obj1->printConstant(); # Fatal Error
echo $obj1::constant . "\n"; # As of PHP 5.3.0
$obj1->printStatic();
echo "\n";
$obj2 = new SubClass();
echo "\t" . $obj2->public . "\n";
#echo "\t" . $obj2->private . "\n"; # Undefined - warning messages
#echo "\t" . $obj2->protected . "\n"; # Fatal Error
$obj2->printProperties(); # Shows Public, Protected2, Undefined
$obj2->printConstant(); # SubClass::printConstant()
echo $obj2::constant . "\n"; # As of PHP 5.3.0
$obj2->printStatic();
echo $obj2::$singleton . "\n\n";
# cleanup
$obj1 = null; # to see destructor in action
$obj2 = null; # to see destructor in action
echo "\n";
# using named classes
# note: no constructors or destructors
$classname = "BaseClass";
echo $classname::constant."\n\n"; # As of PHP 5.3.0
# ----------------------------------------
# abstract class: requires the use of the abstract keyword on abstract methods.
# note visibility redeclarations
abstract class AbstractClass
{
# Force Extending class to define these methods
abstract protected function getValue();
abstract protected function prefixValue($prefix);
# Common method
public function printOut() { print $this->getValue() . "\n"; }
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() { return "ConcreteClass1"; }
public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; }
}
class ConcreteClass2 extends AbstractClass
{
public function getValue() { return "ConcreteClass2"; }
public function prefixValue($prefix) { return "{$prefix}ConcreteClass2"; }
}
# testing abstract usage
$obj1 = new ConcreteClass1;
$obj1->printOut();
echo $obj1->prefixValue('FOO_') ."\n";
$obj2 = new ConcreteClass2;
$obj2->printOut();
echo $obj2->prefixValue('FOO_') ."\n\n";
# cleanup
$obj1 = null;
$obj2 = null;
# ----------------------------------------
# interface: for both extends and implements, multiple inheritance is supported.
#
# all interface methods must be public AND ABSTRACT.
#
# a class cannot implement two interfaces that share function names, since it
# would cause ambiguity.
interface iface1
{
function printIFace1();
}
interface iface2
{
function printIFace2();
}
interface iface3 extends iface1, iface2
{
function printIFace3();
}
interface iface4
{
function printIFace4();
}
# remember, classes can only extend a single class,
# but can implement more than one interface
class DerivedClass extends BaseClass implements iface3, iface4
{
function printIFace1() { echo "IFace1\n"; }
function printIFace2() { echo "IFace2\n"; }
function printIFace3() {
echo "IFace3\n";
# printIFace2(); # Fatal Error
$this->printIFace1();
self::printIFace4();
}
function printIFace4() { echo "IFace4\n"; }
}
$obj1 = new DerivedClass;
$obj1->printIFace2();
$obj1->printIFace3();
$obj1->printProperties(); # Shows Public, Protected and Private
# cleanup
$obj1 = null; # to see destructor in action
echo "\n";
# ----------------------------------------
# final keyword
class BaseClass1
{
function test() { echo "Base1: test()\n"; }
final function moreTesting() { echo "Base1: moreTesting()\n"; }
}
class SubClass1 extends BaseClass1
{
function test() { echo "Sub1: test()\n"; }
# the following results in Fatal error:
# Cannot override final method BaseClass1::moreTesting()
# function moreTesting() { echo "Child1: moreTesting()\n"; }
}
final class BaseClass2
{
function test() { echo "Base2: test()\n"; }
# use of final keyword here is redundant
final function moreTesting() { echo "Base2: moreTesting()\n"; }
}
# Results in Fatal error: Class SubClass2 may not inherit from final class (BaseClass2)
#class SubClass2 extends BaseClass2 { }
$obj1 = new SubClass1;
$obj1->test();
$obj1->moreTesting();
# cleanup
$obj1 = null; # to see destructor in action
echo "\n";
# ----------------------------------------
# # This example attempts to load the classes AnotherClass1 and AnotherClass2
# # from the files AnotherClass1.php and AnotherClass2.php respectively.
# function __autoload($class_name) { require_once $class_name . '.php'; }
# $obj1 = new AnotherClass1();
# $obj2 = new AnotherClass2();
# CLASSES }}}
# ----------------------------------------
echo "</pre>\n";
?>