&n> Who Should Read This Book? ... 1 How This Book Is Organized... 2 About the Book's Source Code... 3 Conventions Used in This Book... 3 Part I: Getting Up and Running 1: Installation QuickStart Guide Linux/UNIX Installation ... 7 Windows Installation ... 10 Mac OS X Installation ... 16 Troubleshooting ... 20 2: Installing and Configuring MySQL Current and Future Versions of MySQL ... 21 How to Get MySQL ... 22 Installing MySQL on Linux/UNIX ... 22 Installing MySQL on Mac OS X ... 24 Installing MySQL on Windows ... 26 Troubleshooting Your Installation ... 32 Basic Security Guidelines ... 33 Introducing the MySQL Privilege System... 35 Working with User Privileges... 37 Summary ... 39 Q&A... 40 Workshop ... 40 3: Installing and Configuring Apache Current and Future Versions of Apache ... 43 Choosing the Appropriate Installation Method ... 44 Installing Apache on Linux/UNIX ... 45 Installing Apache on Mac OS X ... 47 Installing Apache on Windows ... 48 Apache Configuration File Structure ... 50 Apache Log Files... 56 Apache-Related Commands ... 57 Starting Apache for the First Time ... 58 Troubleshooting ... 60 Summary ... 62 Q&A... 62 Workshop ... 63 4: Installing and Configuring PHP Current and Future Versions of PHP ... 65 Building PHP on Linux/UNIX with Apache ... 66 Installing PHP on Mac OS X ... 69 Installing PHP on Windows ... 71 php.ini Basics ... 73 Testing Your Installation ... 74 Getting Installation Help ... 74 The Basics of PHP Scripts ... 76 Summary ... 81 Q&A... 81 Workshop ... 82 Part II: PHP Language Structure 5: The Building Blocks of PHP Variables ... 87 Data Types ... 90 Operators and Expressions ... 96 Constants ... 105 Summary... 107 Q&A... 107 Workshop ... 108 6: Flow Control Functions in PHP Switching Flow ... 111 Loops ... 117 Code Blocks and Browser Output ... 125 Summary... 127 Q&A... 128 Workshop ... 128 7: Working with Functions What Is a Function? ... 131 Calling Functions ... 132 Defining a Function... 133 Returning Values from User-Defined Functions... 136 Variable Scope ... 137 Saving State Between Function Calls with the static Statement ... 140 More About Arguments ... 142 Testing for the Existence of a Function ... 145 Summary... 146 Q&A... 147 Workshop ... 147 8: Working with Arrays 149 What Are Arrays? ... 149 Creating Arrays ... 150 Some Array-Related Functions... 154 Summary... 156 Q&A... 156 Workshop ... 156 9: Working with Objects 159 Creating an Object ... 159 Object Inheritance... 164 Summary... 166 Q&A... 166 Workshop ... 167 Part III: Getting Involved with the Code 10: Working with Strings, Dates, and Time Formatting Strings with PHP ... 172 Investigating Strings in PHP ... 180 Manipulating Strings with PHP ... 184 Using Date and Time Functions in PHP... 191 Other String, Date, and Time Functions ... 197 Summary... 197 Workshop ... 198 11: Working with Forms Creating a Simple Input Form... 201 Accessing Form Input with User-Defined Arrays ... 203 Combining HTML and PHP Code on a Single Page ... 206 Using Hidden Fields to Save State... 208 Redirecting the User... 209 Sending Mail on Form Submission ... 211 Working with File Uploads ... 217 Summary... 221 Workshop ... 221 12: Working with Cookies and User Sessions Introducing Cookies ... 223 Setting a Cookie with PHP ... 225 Deleting a Cookie with PHP ... 227 Session Function Overview ... 227 Starting a Session ... 228 Working with Session Variables ... 229 Passing Session IDs in the Query String ... 233 Destroying Sessions and Unsetting Variables ... 234 Using Sessions in an Environment with Registered Users ... 234 Summary... 235 Q&A... 236 Workshop ... 236 13: Working with Files and Directories Including Files with include() ... 239 Validating Files ... 244 Creating and Deleting Files... 248 Opening a File for Writing, Reading, or Appending ... 249 Reading from Files... 250 Writing or Appending to a File ... 255 Working with Directories ... 257 Opening Pipes to and from Processes Using popen() ... 260 Running Commands with exec() ... 262 Running Commands with system() or passthru() ... 263 Summary... 265 Q&A... 266 Workshop ... 266 14: Working with Images Understanding the Image-Creation Process ... 269 Necessary Modifications to PHP ... 270 Drawing a New Image ... 271 Getting Fancy with Pie Charts ... 275 Modifying Existing Images ... 279 Image Creation from User Input ... 282 Using Images Created by Scripts ... 286 Summary... 288 Q&A... 289 Workshop ... 289 Part IV: PHP and MySQL Integration 15: Understanding the Database Design Process The Importance of Good Database Design ... 293 Types of Table Relationships ... 294 Understanding Normalization... 298 Following the Design Process... 301 Summary... 303 Q&A... 303 Workshop ... 303 16: Learning Basic SQL Commands Learning the MySQL Data Types ... 306 Learning the Table Creation Syntax ... 309 Using the INSERT Command ... 310 Using the SELECT Command ... 312 Using WHERE in Your Queries ... 315 Selecting from Multiple Tables... 317 Using the UPDATE Command to Modify Records ... 323 Using the REPLACE Command ... 325 Using the DELETE Command ... 326 Frequently Used String Functions in MySQL... 328 Using Date and Time Functions in MySQL ... 336 Summary... 348 Q&A... 349 Workshop ... 350 17: Using Transactions and Stored Procedures in MySQL What Are Transactions? ... 353 What Are Stored Procedures?... 357 Summary... 359 Q&A... 359 Workshop ... 359 18: Interacting with MySQL Using PHP MySQL Versus MySQLi Functions ... 361 Connecting to MySQL with PHP... 361 Working with MySQL Data ... 365 Summary... 371 Q&A... 372 Workshop ... 372 Part V: Basic Projects 19: Managing a Simple Mailing List Developing the Subscription Mechanism... 375 Developing the Mailing Mechanism... 383 Summary... 386 Q&A... 387 Workshop ... 387 20: Creating an Online Address Book Planning and Creating the Database Tables ... 389 Creating an Include File for Common Functions ... 392 Creating a Menu... 393 Creating the Record Addition Mechanism ... 394 Viewing Records ... 398 Creating the Record Deletion Mechanism ... 405 Adding Subentries to a Record... 407 Summary... 412 Q&A... 413 Workshop ... 413 21: Creating a Simple Discussion Forum Designing the Database Tables ... 415 Creating an Include File for Common Functions ... 416 Creating the Input Forms and Scripts ... 417 Displaying the Topic List ... 421 Displaying the Posts in a Topic ... 424 Adding Posts to a Topic ... 428 Summary... 431 Q&A... 431 Workshop ... 432 22: Creating an Online Storefront Planning and Creating the Database Tables ... 433 Displaying Categories of Items ... 437 Displaying Items... 441 Summary... 444 Workshop ... 444 23: Creating a Shopping Cart Mechanism Planning and Creating the Database Tables ... 445 Integrating the Cart with Your Storefront ... 447 Payment Methods and the Checkout Sequence... 455 Summary... 457 Workshop ... 458 24: Creating a Simple Calendar Building a Simple Display Calendar... 459 Creating a Calendar Library ... 473 Summary... 479 Q&A... 479 Workshop ... 480 25: Restricting Access to Your Applications Authentication Overview ... 481 Apache Authentication Module Functionality ... 483 Using Apache for Access Control ... 488 Combining Apache Access Methods ... 490 Limiting Access Based on HTTP Methods... 491 Restricting Access Based on Cookie Values... 492 Summary... 497 Q&A... 498 Workshop ... 498 26: Logging and Monitoring Web Server Activity Standard Apache Access Logging... 501 Standard Apache Error Logging... 507 Managing Apache Logs ... 509 Logging Custom Information to a Database ... 511 Summary... 515 Q&A... 516 Workshop ... 516 27: Application Localization About Internationalization and Localization ... 517 About Character Sets ... 518 Environment Modifications... 519 Creating a Localized Page Structure ... 521 Summary... 526 Q&A... 526 Workshop ... 527 28: Working with XML What Is XML?... 529 Accessing XML in PHP Using DOM Functions ... 532 Accessing XML in PHP Using SimpleXML Functions ... 535 Summary... 538 Q&A... 538 Workshop ... 539 Part VI: Administration and Fine-Tuning 29: Apache Performance Tuning and Virtual Hosting Scalability Issues ... 543 Load Testing with ApacheBench ... 547 Proactive Performance Tuning... 549 Preventing Abuse ... 551 Implementing Virtual Hosting... 552 Summary... 557 Q&A... 557 Workshop ... 558 30: Setting Up a Secure Web Server The Need for Security... 561 The SSL Protocol ... 562 Obtaining and Installing SSL Tools ... 567 Managing Certificates ... 570 SSL Configuration ... 572 Summary... 573 Q&A... 574 Workshop ... 574 31: Optimizing and Tuning MySQL Building an Optimized Platform ... 575 MySQL Startup Options ... 577 Optimizing Your Table Structure ... 579 Optimizing Your Queries ... 580 Using the FLUSH Command... 581 Using the SHOW Command ... 582 Summary... 588 Q&A... 589 Workshop ... 589 32: Software Upgrades Staying in the Loop ... 591 Upgrading MySQL... 593 Upgrading Apache ... 593 Upgrading PHP... 595 Summary... 595 Workshop ... 596 Index 597