You are previewing Sams Teach Yourself PHP, MySQL and Apache: All in One, Third Edition.
O'Reilly logo
Sams Teach Yourself PHP, MySQL and Apache: All in One, Third Edition

Book Description

A new edition of this title is available, ISBN-10: 067232976X ISBN-13: 9780672329760

You own your own business. You have also created a website for your business that details the products or services that you offer, but it doesn't allow potential customers to purchase anything online. Don't risk losing business-learn to create a dynamic online environment using only three programs. PHP, MySQL and Apache are three popular open-source tools that can work together to help you create a dynamic website, such as an online shopping experience. Sams Teach Yourself PHP, MySQL and Apache All in One is a complete reference manual for all three development tools. You will learn how to install, configure and set up the PHP scripting language, use the MySQL database system, and work with the Apache Web server. Then you'll take it a step further and discover how they work together to create a dynamic website. Use the book and the included CD to create a simple website, as well as a mailing list, online address book, shopping cart and storefront. Updated to reflect the most recent developments in PHP and MySQL, including the final stable release of MySQL 5.0, you will open your website to limitless possibilities with Sams Teach Yourself PHP, MySQL and Apache All in One.

Table of Contents

  1. Copyright
  2. About the Author
  3. Acknowledgements
  4. We Want to Hear from You!
    1. Reader Services
  5. Introduction
    1. Who Should Read This Book?
    2. How This Book Is Organized
    3. Conventions Used in This Book
  6. I. Laying the Groundwork
    1. 1. Installation QuickStart Guide
      1. Linux/UNIX Installation
        1. Installing MySQL
        2. Installing Apache
        3. Installing PHP
      2. Windows Installation
        1. Installing MySQL
        2. Installing Apache
        3. Installing PHP
      3. Mac OS X Installation
        1. Installing MySQL
        2. Installing PHP
      4. Troubleshooting
    2. 2. Installing and Configuring MySQL
      1. Current and Future Versions of MySQL
      2. How to Get MySQL MySQL
      3. Installing MySQL on Linux/UNIX
      4. Installing MySQL on Mac OS X
      5. Installing MySQL on Windows
      6. Troubleshooting Your Installation
      7. Basic Security Guidelines
        1. Starting MySQL
        2. Securing Your MySQL Connection
      8. Introducing the MySQL Privilege System
        1. The Two-Step Authentication Process
      9. Working with User Privileges
        1. Adding Users
        2. Removing Privileges
      10. Summary
      11. Q&A
      12. Workshop
        1. Quiz
        2. Answer
        3. Activities
    3. 3. Installing and Configuring Apache
      1. Current and Future Versions of Apache
      2. Choosing the Appropriate Installation Method
        1. Building from Source
        2. Installing a Binary
      3. Installing Apache on Linux/UNIX
        1. Downloading the Apache Source Code
        2. Uncompressing the Source Code
        3. Preparing to Build Apache
        4. Building and Installing Apache
      4. Installing Apache on Mac OS X
      5. Installing Apache on Windows
      6. Apache Configuration File Structure
        1. Directives
        2. Containers
        3. Conditional Evaluation
        4. The ServerRoot Directive
        5. Per-Directory Configuration Files
      7. Apache Log Files
        1. The access_log File
        2. The error_log File
        3. Additional Files
      8. Apache-Related Commands
        1. Apache Server Binary
        2. Apache Control Script
      9. Starting Apache for the First Time
        1. Check Your Configuration File
        2. Starting Apache
      10. Troubleshooting
        1. Already an Existing Web Server
        2. No Permission to Bind to Port
        3. Access Denied
        4. Wrong Group Settings
      11. Summary
      12. Q&A
      13. Workshop
        1. Quiz
        2. Answers
        3. Activities
    4. 4. Installing and Configuring PHP
      1. Current and Future Versions of PHP
      2. Building PHP on Linux/UNIX with Apache
        1. Additional Linux/UNIX Configuration Options
        2. Integrating PHP with Apache on Linux/UNIX
      3. Installing PHP on Mac OS X
      4. Installing PHP on Windows
        1. Integrating PHP with Apache on Windows
      5. php.ini Basics
      6. Testing Your Installation
      7. Getting Installation Help
      8. The Basics of PHP Scripts
        1. Beginning and Ending a Block of PHP Statements
        2. The echo Statement and print() Function
        3. Combining HTML and PHP
        4. Adding Comments to PHP Code
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Quiz Answers
        3. Activities
  7. II. PHP Language Structure
    1. 5. The Building Blocks of PHP
      1. Variables
        1. Globals and Superglobals
      2. Data Types
        1. Changing Type with settype()
        2. Changing Type by Casting
        3. Why Test Type?
      3. Operators and Expressions
        1. The Assignment Operator
        2. Arithmetic Operators
        3. The Concatenation Operator
        4. Combined Assignment Operators
        5. Automatically Incrementing and Decrementing an Integer Variable
        6. Comparison Operators
        7. Creating Complex Test Expressions with the Logical Operators
        8. Operator Precedence
      4. Constants
        1. Predefined Constants
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Answers
        3. Activities
    2. 6. Flow Control Functions in PHP
      1. Switching Flow
        1. The if Statement
        2. Using the else Clause with the if Statement
        3. Using the elseif Clause with the if Statement
        4. The switch Statement
        5. Using the ? Operator
      2. Loops
        1. The while Statement
        2. The do...while Statement
        3. The for statement
        4. Breaking Out of Loops with the break Statement
        5. Skipping an Iteration with the continue Statement
        6. Nesting Loops
      3. Code Blocks and Browser Output
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Answers
        3. Activity
    3. 7. Working with Functions
      1. What Is a Function?
      2. Calling Functions
      3. Defining a Function
      4. Returning Values from User-Defined Functions
      5. Variable Scope
        1. Accessing Variables with the global Statement
      6. Saving State Between Function Calls with the static Statement
      7. More About Arguments
        1. Setting Default Values for Arguments
        2. Passing Variable References to Functions
      8. Testing for the Existence of a Function
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Answers
        3. Activity
    4. 8. Working with Arrays
      1. What Are Arrays?
      2. Creating Arrays
        1. Creating Associative Arrays
        2. Creating Multidimensional Arrays
      3. Some Array-Related Functions
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Answers
        3. Activities
    5. 9. Working with Objects
      1. Creating an Object
        1. Properties of Objects
        2. Object Methods
        3. Constructors
      2. Object Inheritance
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
        3. Activity
  8. III. Getting Involved with the Code
    1. 10. Working with Strings, Dates, and Time
      1. Formatting Strings with PHP
        1. Working with printf()
          1. printf() and Type Specifiers
          2. Padding Output with the Padding Specifier
        2. Specifying a Field Width
          1. Specifying Precision
          2. Conversion Specifications: A Recap
        3. Argument Swapping
        4. Storing a Formatted String
      2. Investigating Strings in PHP
        1. A Note About Indexing Strings
        2. Finding the Length of a String with strlen()
        3. Finding a Substring Within a String with strstr()
        4. Finding the Position of a Substring with strpos()
        5. Extracting Part of a String with substr()
        6. Tokenizing a String with strtok()
      3. Manipulating Strings with PHP
        1. Cleaning Up a String with trim(), ltrim(), and strip_tags()
        2. Replacing a Portion of a String Using substr_replace()
        3. Replacing Substrings Using str_replace
        4. Converting Case
        5. Wrapping Text with wordwrap() and nl2br()
        6. Breaking Strings into Arrays with explode()
      4. Using Date and Time Functions in PHP
        1. Getting the Date with time()
        2. Converting a Time Stamp with getdate()
        3. Converting a Time Stamp with date()
        4. Creating Time Stamps with mktime()
        5. Testing a Date with checkdate()
      5. Other String, Date, and Time Functions
      6. Summary
      7. Workshop
        1. Q&A
        2. Quiz
        3. Answer
        4. Activities
    2. 11. Working with Forms
      1. Creating a Simple Input Form
      2. Accessing Form Input with User-Defined Arrays
      3. Combining HTML and PHP Code on a Single Page
      4. Using Hidden Fields to Save State
      5. Redirecting the User
      6. Sending Mail on Form Submission
        1. System Configuration for the mail() Function
        2. Creating the Form
        3. Creating the Script to Send the Mail
        4. Formatting Your Mail with HTML
      7. Working with File Uploads
        1. Creating the File Upload Form
        2. Creating the File Upload Script
      8. Summary
      9. Workshop
        1. Quiz
        2. Answers
        3. Activities
    3. 12. Working with Cookies and User Sessions
      1. Introducing Cookies
        1. The Anatomy of a Cookie
      2. Setting a Cookie with PHP
        1. Deleting a Cookie
      3. Session Function Overview
      4. Starting a Session
      5. Working with Session Variables
      6. Passing Session IDs in the Query String
      7. Destroying Sessions and Unsetting Variables
      8. Using Sessions in an Environment with Registered Users
        1. Working with Registered Users
        2. Working with User Preferences
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Quiz Answers
        3. Activity
    4. 13. Working with Files and Directories
      1. Including Files with include()
        1. Returning a Value from an Included Document
        2. Using include() Within Control Structures
        3. Using include_once()
        4. The include_path Directive
      2. Validating Files
        1. Checking for Existence with file_exists()
        2. A File or a Directory?
        3. Checking the Status of a File
        4. Determining File Size with filesize()
        5. Getting Date Information About a File
      3. Creating a Function That Performs Multiple File Tests
      4. Creating and Deleting Files
      5. Opening a File for Writing, Reading, or Appending
      6. Reading from Files
        1. Reading Lines from a File with fgets() and feof()
        2. Reading Arbitrary Amounts of Data from a File with fread()
        3. Reading Characters from a File with fgetc()
      7. Writing or Appending to a File
        1. Writing to a File with fwrite() or fputs()
        2. Locking Files with flock()
      8. Working with Directories
        1. Creating Directories with mkdir()
        2. Removing a Directory with rmdir()
        3. Opening a Directory for Reading with opendir()
        4. Reading the Contents of a Directory with readdir()
      9. Opening Pipes to and from Processes Using popen()
      10. Running Commands with exec()
      11. Running Commands with system() or passthru()
      12. Summary
      13. Q&A
      14. Workshop
        1. Quiz
        2. Answers
        3. Activities
    5. 14. Working with Images
      1. Understanding the Image-Creation Process
        1. A Word About Color
      2. Necessary Modifications to PHP
        1. Obtaining Additional Libraries
      3. Drawing a New Image
        1. Drawing Shapes and Lines
        2. Using a Color Fill
      4. Getting Fancy with Pie Charts
      5. Modifying Existing Images
      6. Image Creation from User Input
      7. Summary
      8. Q&A
      9. Workshop
        1. Quiz
        2. Answers
        3. Activity
  9. IV. PHP and MySQL Integration
    1. 15. Understanding the Database Design Process
      1. The Importance of Good Database Design
      2. Types of Table Relationships
        1. One-to-One Relationships
        2. One-to-Many Relationships
        3. Many-to-Many Relationships
      3. Understanding Normalization
        1. Problems with the Flat Table
        2. First Normal Form
        3. Second Normal Form
        4. Third Normal Form
      4. Following the Design Process
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
        3. Activity
    2. 16. Learning Basic SQL Commands
      1. Learning the MySQL Data Types
        1. Numeric Data Types
        2. Date and Time Types
        3. String Types
      2. Learning the Table Creation Syntax
      3. Using the INSERT Command
        1. A Closer Look at INSERT
      4. Using the SELECT Command
        1. Ordering SELECT Results
        2. Limiting Your Results
      5. Using WHERE in Your Queries
        1. Using Operators in WHERE Clauses
        2. String Comparison Using LIKE
      6. Selecting from Multiple Tables
        1. Using JOIN
        2. Using Subqueries
      7. Using the UPDATE Command to Modify Records
        1. Conditional UPDATEs
        2. Using Existing Column Values with UPDATE
      8. Using the REPLACE Command
      9. Using the DELETE Command
        1. Conditional DELETE
      10. Frequently Used String Functions in MySQL
        1. Length and Concatenation Functions
        2. Trimming and Padding Functions
        3. Location and Position Functions
        4. Substring Functions
        5. String Modification Functions
      11. Using Date and Time Functions in MySQL
        1. Working with Days
        2. Working with Months and Years
        3. Working with Weeks
        4. Working with Hours, Minutes, and Seconds
        5. Formatting Dates and Times with MySQL
        6. Performing Date Arithmetic with MySQL
        7. Special Functions and Conversion Features
      12. Summary
      13. Q&A
      14. Workshop
        1. Quiz
        2. Answers
        3. Activity
    3. 17. Using Transactions and Stored Procedures in MySQL
      1. What Are Transactions?
        1. Basic Syntax Used in Transactions
        2. Working Example Using Transactions
      2. What Are Stored Procedures?
        1. Basic Syntax of Stored Procedures
      3. Summary
      4. Workshop
        1. Quiz
        2. Answers
    4. 18. Interacting with MySQL Using PHP
      1. MySQL Versus MySQLi Functions
      2. Connecting to MySQL with PHP
        1. Making a Connection
        2. Executing Queries
        3. Retrieving Error Messages
      3. Working with MySQL Data
        1. Inserting Data with PHP
        2. Retrieving Data with PHP
        3. Additional MySQL Functions in PHP
      4. Summary
      5. Workshop
        1. Quiz
        2. Answers
  10. V. Basic Projects
    1. 19. Managing a Simple Mailing List
      1. Developing the Subscription Mechanism
        1. Creating the subscribers Table
        2. Creating the Subscription Form
      2. Developing the Mailing Mechanism
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Answers
    2. 20. Creating an Online Address Book
      1. Planning and Creating the Database Tables
      2. Creating a Menu
      3. Creating the Record Addition Mechanism
      4. Viewing Records
      5. Creating the Record Deletion Mechanism
      6. Adding Subentries to a Record
      7. Summary
      8. Workshop
        1. Quiz
        2. Answers
        3. Activities
    3. 21. Creating a Simple Discussion Forum
      1. Designing the Database Tables
      2. Creating the Input Forms and Scripts
      3. Displaying the Topic List
      4. Displaying the Posts in a Topic
      5. Adding Posts to a Topic
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
        3. Activity
    4. 22. Creating an Online Storefront
      1. Planning and Creating the Database Tables
        1. Inserting Records into the store_categories Table
        2. Inserting Records into the store_items Table
        3. Inserting Records into the store_item_size Table
        4. Inserting Records into the store_item_color Table
      2. Displaying Categories of Items
      3. Displaying Items
      4. Summary
      5. Workshop
        1. Quiz
        2. Answers
    5. 23. Creating a Shopping Cart Mechanism
      1. Planning and Creating the Database Tables
      2. Integrating the Cart with Your Storefront
        1. Adding Items to Your Cart
        2. Viewing the Cart
        3. Removing Items from Your Cart
      3. Payment Methods and the Checkout Sequence
        1. Creating the Checkout Form
        2. Performing the Checkout Actions
      4. Summary
      5. Workshop
        1. Quiz
        2. Answers
    6. 24. Creating a Simple Calendar
      1. Building a Simple Display Calendar
        1. Checking User Input
        2. Building the HTML Form
        3. Creating the Calendar Table
        4. Adding Events to the Calendar
      2. Creating a Calendar Library
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Quiz Answers
        3. Activity
    7. 25. Restricting Access to Your Applications
      1. Authentication Overview
        1. Client Authentication
          1. User Management Methods
      2. Apache Authentication Module Functionality
        1. File-Based Authentication
          1. Back-End Storage
          2. User Management
          3. Authoritative
          4. Using mod_auth
        2. Database File-Based Access Control
          1. Back-End Storage
          2. User Management
      3. Using Apache for Access Control
        1. Implementing Access Rules
          1. Allow/Deny Access by IP Addresses
          2. Allow/Deny Access by Domain Name
          3. Allow/Deny Access Based on Environment Variables
          4. Allow/Deny Access to All Clients
        2. Evaluating Access Rules
      4. Combining Apache Access Methods
      5. Limiting Access Based on HTTP Methods
      6. Restricting Access Based on Cookie Values
        1. Creating the Authorized Users Table
        2. Creating the Login Form and Script
        3. Testing for the auth Cookie
      7. Summary
      8. Q&A
      9. Workshop
        1. Quiz
        2. Quiz Answers
        3. Activity
    8. 26. Logging and Monitoring Web Server Activity
      1. Standard Apache Access Logging
        1. Deciding What to Log
          1. The HostNameLookups Directive
          2. The IdentityCheck Directive
          3. Status Code
        2. Logging Accesses to Files
          1. Logging Environment Variables with CustomLog
        3. Logging Accesses to a Program
      2. Standard Apache Error Logging
        1. Logging Errors to a File
        2. Logging Errors to a Program
        3. The syslog Daemon Argument
        4. The LogLevel Directive
      3. Managing Apache Logs
        1. Resolving Hostnames
        2. Log Rotation
        3. Merging and Splitting Logs
        4. Log Analysis
        5. Monitoring Error Logs
      4. Logging Custom Information to a Database
        1. Creating the Database Table
        2. Creating the PHP Code Snippet
        3. Creating Sample Reports
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
    9. 27. Application Localization
      1. About Internationalization and Localization
      2. About Character Sets
      3. Environment Modifications
        1. Configuration Changes to Apache
        2. Configuration Changes to PHP
        3. Configuration Changes to MySQL
      4. Creating a Localized Page Structure
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Answers
    10. 28. Working with XML
      1. What Is XML?
        1. Basic XML Document Structure
      2. Accessing XML in PHP Using DOM Functions
      3. Accessing XML in PHP Using SimpleXML Functions
      4. Summary
      5. Workshop
        1. Quiz
        2. Answers
  11. VI. Administration and Fine-tuning
    1. 29. Apache Performance Tuning and Virtual Hosting
      1. Scalability Issues
        1. Operating System Limits
          1. Processes
          2. File Descriptors
          3. Controlling External Processes
        2. Performance-Related Apache Settings
          1. File System Access
          2. Network and Status Settings
      2. Load Testing with ApacheBench
      3. Proactive Performance Tuning
        1. Mapping Files to Memory
        2. Distributing the Load
        3. Caching
        4. Reduce Transmitted Data
        5. Network Settings
      4. Preventing Abuse
        1. Robots
      5. Implementing Virtual Hosting
        1. IP-Based Virtual Hosting
        2. Name-Based Virtual Hosts
        3. Mass Virtual Hosting
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
    2. 30. Setting Up a Secure Web Server
      1. The Need for Security
      2. The SSL Protocol
        1. Addressing the Need for Confidentiality
          1. Symmetric Cryptography
          2. Public Key Cryptography
        2. Addressing the Need for Integrity
        3. Addressing the Need for Authentication
          1. SSL and Certificates
          2. SSL Protocol Summary
      3. Obtaining and Installing SSL Tools
        1. OpenSSL
          1. Installation for Windows Users
          2. Installation for UNIX/Linux Users
        2. The mod_ssl Apache Module
          1. For Windows Users
          2. For UNIX/Linux Users
      4. Managing Certificates
        1. Creating a Key Pair
        2. Creating a Certificate Signing Request
        3. Creating a Self-Signed Certificate
      5. SSL Configuration
        1. Starting the Server
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
    3. 31. Optimizing and Tuning MySQL
      1. Building an Optimized Platform
        1. Using the benchmark() Function
      2. MySQL Startup Options
        1. Key Startup Parameters
      3. Optimizing Your Table Structure
      4. Optimizing Your Queries
      5. Using the FLUSH Command
      6. Using the SHOW Command
        1. Retrieving Information About Databases and Tables
        2. Retrieving Table Structure Information
        3. Retrieving System Status
      7. Summary
      8. Q&A
      9. Workshop
        1. Quiz
        2. Answers
        3. Activities
    4. 32. Software Upgrades
      1. Staying in the Loop
        1. When to Upgrade
      2. Upgrading MySQL
      3. Upgrading Apache
        1. Modifying Apache Without Upgrading
      4. Upgrading PHP
        1. Using PECL and PEAR to Extend PHP
      5. Summary
      6. Workshop
        1. Quiz
        2. Answers