The PHP Anthology: 101 Essential Tips, Tricks & Hacks [2 ed.] 1590599128

The PHP Anthology: 101 Essential Tips, Tricks & Hacks , 2nd Edition is a collection of powerful PHP 5 solutions to t

276 71 14KB

English Pages 544 Year 2007

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
The PHP Anthology......Page 1
Table of Contents......Page 7
Preface......Page 15
Who Should Read this Book?......Page 16
What’s Covered in this Book?......Page 17
Running the Code Examples......Page 19
The Code Archive......Page 20
Conventions Used in this Book......Page 21
Code Samples......Page 22
Tips, Notes, and Warnings......Page 23
Introduction......Page 25
RTFM: Read the Fine Manual......Page 26
III. Language Reference......Page 27
VI. Function Reference......Page 28
PHP Extensions......Page 29
Other Resources......Page 31
Solution......Page 33
Classes Explained......Page 34
Encapsulation and Visibility......Page 37
Creating Objects......Page 38
The $this Variable......Page 39
Treating an Object Like a String......Page 40
Inheritance......Page 41
Overriding Methods and Properties......Page 44
Aggregation......Page 47
Composition......Page 49
Using Aggregation and Composition: Benefits and Pitfalls......Page 50
Abstract Classes and Methods......Page 51
Object Interfaces......Page 53
Static Properties and Methods......Page 55
Keeping Configuration Central......Page 57
Recycling and Reuse......Page 58
Portability Essentials......Page 59
Turning register_globals Off......Page 60
Checking for Magic Quotes......Page 61
Summary......Page 62
Using Databases with PDO......Page 63
What is PDO?......Page 64
Solution......Page 65
Discussion......Page 66
The DSN in Detail......Page 67
How do I fetch data from a table?......Page 68
Using the Query Method......Page 69
Using the Prepare and Execute Methods......Page 70
Using Fetch Choices......Page 72
Solutions......Page 73
Using Warning Mode......Page 74
Using Exception Mode......Page 75
Discussion......Page 76
INSERT Data into the Database......Page 77
UPDATE Data in the Database......Page 78
How do I protect my web site from an SQL injection attack?......Page 79
Solution......Page 80
Solution......Page 81
Solutions......Page 83
Discussion......Page 84
Counting the Rows Affected......Page 85
Solution......Page 86
Solution......Page 87
Discussion......Page 88
Solution......Page 89
Discussion......Page 90
Solution......Page 91
Discussion......Page 92
Solution......Page 93
Discussion......Page 95
Catering to Platform Differences......Page 96
Summary......Page 99
Strings......Page 101
Solution......Page 103
Solutions......Page 105
Solution......Page 106
Discussion......Page 107
How do I perform advanced search and replace operations?......Page 108
Solutions......Page 109
Solution......Page 110
Discussion......Page 111
How do I output formatted text?......Page 112
Solution......Page 113
Solution......Page 114
Summary......Page 118
Dates and Times......Page 119
How do I use Unix timestamps?......Page 120
Discussion......Page 121
How do I obtain the current date?......Page 122
Discussion......Page 123
Solution......Page 125
Solution......Page 126
How do I store dates in MySQL?......Page 131
Discussion......Page 132
Solution......Page 133
Solution......Page 135
Summary......Page 136
Forms, Tables, and Pretty URLs......Page 139
How do I build HTML forms with PHP?......Page 140
Solution......Page 141
Solution......Page 151
Solution......Page 154
Solution......Page 158
How do I make “pretty” URLs in PHP?......Page 163
Pretty URLs with MultiViews......Page 164
Pretty URLs with mod_rewrite......Page 165
Handling Pretty URLs......Page 166
Discussion......Page 168
Summary......Page 169
Working with Files......Page 171
Solutions......Page 172
Reading a File as an Array......Page 173
Reading a File as a String......Page 174
Reading a File Directly to the Screen......Page 175
Discussion......Page 176
Handling Small Files......Page 177
Handling Larger Files......Page 178
Solution......Page 179
Discussion......Page 180
Solution......Page 181
Discussion......Page 183
Using the readdir Function......Page 184
Solution......Page 185
How do I store configuration information in a file?......Page 187
Solution......Page 188
Discussion......Page 189
Discussion......Page 190
Using PHP’s Built-in FTP Functions......Page 191
Using the PEAR::Net_FTP Class......Page 192
Discussion......Page 193
Solution......Page 194
Discussion......Page 195
Solutions......Page 196
Compressing Simple Files......Page 197
How do I work with files using the Standard PHP Library in PHP 5?......Page 198
Solution......Page 199
Discussion......Page 200
Summary......Page 201
Solutions......Page 203
Using the PEAR::Mail Package......Page 204
Discussion......Page 205
How do I simplify the generation of complex emails?......Page 206
Discussion......Page 207
Solution......Page 208
Discussion......Page 209
How do I send HTML email?......Page 210
Solution......Page 211
How do I mail a message to a group of people?......Page 212
Solution......Page 213
Discussion......Page 214
Solution......Page 215
Discussion......Page 216
How can I protect my site against email injection attacks?......Page 217
Solution......Page 218
Summary......Page 219
Images......Page 221
Solution......Page 222
Solution......Page 223
Solution......Page 226
Solution......Page 238
Solution......Page 241
Displaying a Text Watermark......Page 244
Displaying a Graphical Watermark......Page 245
Solutions......Page 247
Creating a Bar Graph......Page 248
Creating a Pie Chart......Page 251
Solutions......Page 254
Using PHP Sessions......Page 255
Solution......Page 258
Summary......Page 259
Error Handling......Page 261
What error levels does PHP report?......Page 262
The error_reporting Directive......Page 263
The display_errors Directive......Page 264
Discussion......Page 265
Solution......Page 266
Solution......Page 271
How can I use PHP exceptions for error handling?......Page 272
Solution......Page 273
Discussion......Page 274
How do I create a custom Exception class?......Page 276
Discussion......Page 277
Solution......Page 281
Discussion......Page 282
Solution......Page 284
Solution......Page 285
Discussion......Page 288
Solution......Page 289
Summary......Page 291
Access Control......Page 293
How do I use HTTP authentication?......Page 295
Solution......Page 296
Discussion......Page 297
How do I use sessions?......Page 301
Discussion......Page 302
Session Security......Page 304
Solution......Page 305
The Auth Class......Page 307
The Restricted Area......Page 315
Discussion......Page 319
Room for Improvement......Page 320
Solution......Page 321
The SignUp Class......Page 323
The Signup Page......Page 332
How do I deal with members who forget their passwords?......Page 342
The AccountMaintenance Class......Page 343
The Reset Password Page......Page 349
How do I let users change their passwords?......Page 354
Modifying AccountMaintenance......Page 355
The Change Password Form......Page 357
Discussion......Page 362
Setting Up the Database......Page 363
The User Class......Page 367
The Permissions Test Page......Page 373
Discussion......Page 376
Solution......Page 377
The DatabaseSession Class......Page 378
Using the DatabaseSession Class......Page 385
Summary......Page 386
Caching......Page 387
Using HTTP Headers......Page 389
Discussion......Page 390
Setting a Page Expiry Header......Page 391
Acting on the Browser’s Request Headers......Page 392
Solution......Page 395
Solutions......Page 396
Solution......Page 397
What About Template Caching?......Page 400
How do I cache just the parts of a page that change infrequently?......Page 401
Solution......Page 402
Discussion......Page 405
Solution......Page 406
What configuration options does Cache_Lite support?......Page 409
Solution......Page 410
Discussion......Page 413
Solution......Page 414
Summary......Page 416
XML and Web Services......Page 419
Solution......Page 420
Solutions......Page 422
Parsing XML with XMLReader......Page 423
SimpleXML with Zend_Feed......Page 427
Discussion......Page 428
Solutions......Page 429
Generating XML Using the DOM......Page 431
Discussion......Page 432
How do I search for a node or content in XML?......Page 433
Solution......Page 434
Discussion......Page 435
How can I consume XML-RPC web services?......Page 436
Solution......Page 437
PHP’s Native XML-RPC Extension......Page 438
Solution......Page 440
PHP’s Native XML-RPC Extension......Page 443
How can I consume SOAP web services?......Page 444
Solution......Page 445
Discussion......Page 446
Solution......Page 447
Discussion......Page 448
How can I consume REST services?......Page 449
Solution......Page 450
Using the Zend Framework......Page 454
Solution......Page 455
Summary......Page 457
Best Practices......Page 459
Solution......Page 460
Discussion......Page 461
Solution......Page 462
Discussion......Page 463
Solutions......Page 465
Using OOP......Page 466
Choosing a Namespace......Page 469
Choosing a Coding Standard......Page 470
How can I document my code for later reference by myself or others?......Page 472
Solution......Page 473
Discussion......Page 475
Solutions......Page 478
Testing Using SimpleTest or PHPUnit......Page 480
Testing Using phpt......Page 484
Discussion......Page 485
Solution......Page 487
Solution......Page 491
Using Tags and Symlinks......Page 492
Discussion......Page 494
Summary......Page 495
Configuration Mechanisms......Page 497
Includes and Execution Settings......Page 499
Error-related Settings......Page 504
Miscellaneous Settings......Page 505
Does the host support Linux and Apache?......Page 507
To what degree does the host “overload” the server?......Page 508
Is PHP installed as an Apache module (not the CGI variant)?......Page 509
What’s the host’s knowledge of PHP?......Page 510
Cross-site Scripting (XSS)......Page 513
Insecure Direct Object Reference......Page 515
Information Leakage and Improper Error Handling......Page 517
Broken Authentication and Session Management......Page 518
Failure to Restrict URL Access......Page 519
Appendix D: Working with PEAR......Page 521
Installing PEAR......Page 522
The PEAR Package Manager......Page 525
Installing Packages Manually......Page 527
Alternatives to PEAR......Page 528
Index......Page 529

The PHP Anthology: 101 Essential Tips, Tricks & Hacks [2 ed.]
 1590599128

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers