The PHP anthology : 101 essential tips, tricks & hacks

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Davey Shafik; et al
Edition: 2nd ed
Publisher: SitePoint Pty. Ltd
Year: 2007

Language: English
Pages: 542
City: Collingwood, Vic., Australia

The PHP Anthology......Page 1
Table of Contents......Page 5
Preface......Page 13
Who Should Read this Book?......Page 14
What’s Covered in this Book?......Page 15
Running the Code Examples......Page 17
The Code Archive......Page 18
Conventions Used in this Book......Page 19
Code Samples......Page 20
Tips, Notes, and Warnings......Page 21
Introduction......Page 23
RTFM: Read the Fine Manual......Page 24
III. Language Reference......Page 25
VI. Function Reference......Page 26
PHP Extensions......Page 27
Other Resources......Page 29
Solution......Page 31
Classes Explained......Page 32
Encapsulation and Visibility......Page 35
Creating Objects......Page 36
The $this Variable......Page 37
Treating an Object Like a String......Page 38
Inheritance......Page 39
Overriding Methods and Properties......Page 42
Aggregation......Page 45
Composition......Page 47
Using Aggregation and Composition: Benefits and Pitfalls......Page 48
Abstract Classes and Methods......Page 49
Object Interfaces......Page 51
Static Properties and Methods......Page 53
Keeping Configuration Central......Page 55
Recycling and Reuse......Page 56
Portability Essentials......Page 57
Turning register_globals Off......Page 58
Checking for Magic Quotes......Page 59
Summary......Page 60
Using Databases with PDO......Page 61
What is PDO?......Page 62
Solution......Page 63
Discussion......Page 64
The DSN in Detail......Page 65
How do I fetch data from a table?......Page 66
Using the Query Method......Page 67
Using the Prepare and Execute Methods......Page 68
Using Fetch Choices......Page 70
Solutions......Page 71
Using Warning Mode......Page 72
Using Exception Mode......Page 73
Discussion......Page 74
INSERT Data into the Database......Page 75
UPDATE Data in the Database......Page 76
How do I protect my web site from an SQL injection attack?......Page 77
Solution......Page 78
Solution......Page 79
Solutions......Page 81
Discussion......Page 82
Counting the Rows Affected......Page 83
Solution......Page 84
Solution......Page 85
Discussion......Page 86
Solution......Page 87
Discussion......Page 88
Solution......Page 89
Discussion......Page 90
Solution......Page 91
Discussion......Page 93
Catering to Platform Differences......Page 94
Summary......Page 97
Strings......Page 99
Solution......Page 101
Solutions......Page 103
Solution......Page 104
Discussion......Page 105
How do I perform advanced search and replace operations?......Page 106
Solutions......Page 107
Solution......Page 108
Discussion......Page 109
How do I output formatted text?......Page 110
Solution......Page 111
Solution......Page 112
Summary......Page 116
Dates and Times......Page 117
How do I use Unix timestamps?......Page 118
Discussion......Page 119
How do I obtain the current date?......Page 120
Discussion......Page 121
Solution......Page 123
Solution......Page 124
How do I store dates in MySQL?......Page 129
Discussion......Page 130
Solution......Page 131
Solution......Page 133
Summary......Page 134
Forms, Tables, and Pretty URLs......Page 137
How do I build HTML forms with PHP?......Page 138
Solution......Page 139
Solution......Page 149
Solution......Page 152
Solution......Page 156
How do I make “pretty” URLs in PHP?......Page 161
Pretty URLs with MultiViews......Page 162
Pretty URLs with mod_rewrite......Page 163
Handling Pretty URLs......Page 164
Discussion......Page 166
Summary......Page 167
Working with Files......Page 169
Solutions......Page 170
Reading a File as an Array......Page 171
Reading a File as a String......Page 172
Reading a File Directly to the Screen......Page 173
Discussion......Page 174
Handling Small Files......Page 175
Handling Larger Files......Page 176
Solution......Page 177
Discussion......Page 178
Solution......Page 179
Discussion......Page 181
Using the readdir Function......Page 182
Solution......Page 183
How do I store configuration information in a file?......Page 185
Solution......Page 186
Discussion......Page 187
Discussion......Page 188
Using PHP’s Built-in FTP Functions......Page 189
Using the PEAR::Net_FTP Class......Page 190
Discussion......Page 191
Solution......Page 192
Discussion......Page 193
Solutions......Page 194
Compressing Simple Files......Page 195
How do I work with files using the Standard PHP Library in PHP 5?......Page 196
Solution......Page 197
Discussion......Page 198
Summary......Page 199
Solutions......Page 201
Using the PEAR::Mail Package......Page 202
Discussion......Page 203
How do I simplify the generation of complex emails?......Page 204
Discussion......Page 205
Solution......Page 206
Discussion......Page 207
How do I send HTML email?......Page 208
Solution......Page 209
How do I mail a message to a group of people?......Page 210
Solution......Page 211
Discussion......Page 212
Solution......Page 213
Discussion......Page 214
How can I protect my site against email injection attacks?......Page 215
Solution......Page 216
Summary......Page 217
Images......Page 219
Solution......Page 220
Solution......Page 221
Solution......Page 224
Solution......Page 236
Solution......Page 239
Displaying a Text Watermark......Page 242
Displaying a Graphical Watermark......Page 243
Solutions......Page 245
Creating a Bar Graph......Page 246
Creating a Pie Chart......Page 249
Solutions......Page 252
Using PHP Sessions......Page 253
Solution......Page 256
Summary......Page 257
Error Handling......Page 259
What error levels does PHP report?......Page 260
The error_reporting Directive......Page 261
The display_errors Directive......Page 262
Discussion......Page 263
Solution......Page 264
Solution......Page 269
How can I use PHP exceptions for error handling?......Page 270
Solution......Page 271
Discussion......Page 272
How do I create a custom Exception class?......Page 274
Discussion......Page 275
Solution......Page 279
Discussion......Page 280
Solution......Page 282
Solution......Page 283
Discussion......Page 286
Solution......Page 287
Summary......Page 289
Access Control......Page 291
How do I use HTTP authentication?......Page 293
Solution......Page 294
Discussion......Page 295
How do I use sessions?......Page 299
Discussion......Page 300
Session Security......Page 302
Solution......Page 303
The Auth Class......Page 305
The Restricted Area......Page 313
Discussion......Page 317
Room for Improvement......Page 318
Solution......Page 319
The SignUp Class......Page 321
The Signup Page......Page 330
How do I deal with members who forget their passwords?......Page 340
The AccountMaintenance Class......Page 341
The Reset Password Page......Page 347
How do I let users change their passwords?......Page 352
Modifying AccountMaintenance......Page 353
The Change Password Form......Page 355
Discussion......Page 360
Setting Up the Database......Page 361
The User Class......Page 365
The Permissions Test Page......Page 371
Discussion......Page 374
Solution......Page 375
The DatabaseSession Class......Page 376
Using the DatabaseSession Class......Page 383
Summary......Page 384
Caching......Page 385
Using HTTP Headers......Page 387
Discussion......Page 388
Setting a Page Expiry Header......Page 389
Acting on the Browser’s Request Headers......Page 390
Solution......Page 393
Solutions......Page 394
Solution......Page 395
What About Template Caching?......Page 398
How do I cache just the parts of a page that change infrequently?......Page 399
Solution......Page 400
Discussion......Page 403
Solution......Page 404
What configuration options does Cache_Lite support?......Page 407
Solution......Page 408
Discussion......Page 411
Solution......Page 412
Summary......Page 414
XML and Web Services......Page 417
Solution......Page 418
Solutions......Page 420
Parsing XML with XMLReader......Page 421
SimpleXML with Zend_Feed......Page 425
Discussion......Page 426
Solutions......Page 427
Generating XML Using the DOM......Page 429
Discussion......Page 430
How do I search for a node or content in XML?......Page 431
Solution......Page 432
Discussion......Page 433
How can I consume XML-RPC web services?......Page 434
Solution......Page 435
PHP’s Native XML-RPC Extension......Page 436
Solution......Page 438
PHP’s Native XML-RPC Extension......Page 441
How can I consume SOAP web services?......Page 442
Solution......Page 443
Discussion......Page 444
Solution......Page 445
Discussion......Page 446
How can I consume REST services?......Page 447
Solution......Page 448
Using the Zend Framework......Page 452
Solution......Page 453
Summary......Page 455
Best Practices......Page 457
Solution......Page 458
Discussion......Page 459
Solution......Page 460
Discussion......Page 461
Solutions......Page 463
Using OOP......Page 464
Choosing a Namespace......Page 467
Choosing a Coding Standard......Page 468
How can I document my code for later reference by myself or others?......Page 470
Solution......Page 471
Discussion......Page 473
Solutions......Page 476
Testing Using SimpleTest or PHPUnit......Page 478
Testing Using phpt......Page 482
Discussion......Page 483
Solution......Page 485
Solution......Page 489
Using Tags and Symlinks......Page 490
Discussion......Page 492
Summary......Page 493
Configuration Mechanisms......Page 495
Includes and Execution Settings......Page 497
Error-related Settings......Page 502
Miscellaneous Settings......Page 503
Does the host support Linux and Apache?......Page 505
To what degree does the host “overload” the server?......Page 506
Is PHP installed as an Apache module (not the CGI variant)?......Page 507
What’s the host’s knowledge of PHP?......Page 508
Cross-site Scripting (XSS)......Page 511
Insecure Direct Object Reference......Page 513
Information Leakage and Improper Error Handling......Page 515
Broken Authentication and Session Management......Page 516
Failure to Restrict URL Access......Page 517
Appendix D: Working with PEAR......Page 519
Installing PEAR......Page 520
The PEAR Package Manager......Page 523
Installing Packages Manually......Page 525
Alternatives to PEAR......Page 526
Index......Page 527