PHP Solutions: Dynamic Web Design Made Easy, Third Edition

Book description

This is the third edition of David Powers' highly-respected PHP Solutions: Dynamic Web Design Made Easy. This new edition has been updated by David to incorporate changes to PHP since the second edition and to offer the latest techniques—a classic guide modernized for 21st century PHP techniques, innovations, and best practices.

You want to make your websites more dynamic by adding a feedback form, creating a private area where members can upload images that are automatically resized, or perhaps storing all your content in a database. The problem is, you're not a programmer and the thought of writing code sends a chill up your spine. Or maybe you've dabbled a bit in PHP and MySQL, but you can't get past baby steps. If this describes you, then you've just found the right book. PHP and the MySQL database are deservedly the most popular combination for creating dynamic websites. They're free, easy to use, and provided by many web hosting companies in their standard packages.

Unfortunately, most PHP books either expect you to be an expert already or force you to go through endless exercises of little practical value. In contrast, this book gives you real value right away through a series of practical examples that you can incorporate directly into your sites, optimizing performance and adding functionality such as file uploading, email feedback forms, image galleries, content management systems, and much more. Each solution is created with not only functionality in mind, but also visual design.

But this book doesn't just provide a collection of ready-made scripts: each PHP Solution builds on what's gone before, teaching you the basics of PHP and database design quickly and painlessly. By the end of the book, you'll have the confidence to start writing your own scripts or—if you prefer to leave that task to others—to adapt existing scripts to your own requirements. Right from the start, you're shown how easy it is to protect your sites by adopting secure coding practices.

Table of contents

  1. Cover
  2. Title
  3. Copyright
  4. Contents at a Glance
  5. Contents
  6. About the Author
  7. About the Technical Reviewer
  8. Acknowledgments
  9. Introduction
  10. Chapter 1: What Is PHP—And Why Should I Care?
    1. How PHP Has Grown
    2. How PHP Makes Pages Dynamic
      1. Creating Pages That Think for Themselves
    3. How Hard Is PHP to Use and Learn?
      1. Can I Just Copy and Paste the Code?
      2. How Safe Is PHP?
    4. What Software Do I Need to Write PHP?
      1. What to Look for When Choosing a PHP Editor
    5. So, Let’s Get on with It . . .
  11. Chapter 2: Getting Ready to Work with PHP
    1. Checking Whether Your Website Supports PHP
      1. Deciding Where to Test Your Pages
    2. What You Need for a Local Test Environment
      1. Individual Programs or an All-in-one Package?
    3. Setting Up on Windows
      1. Getting Windows to Display Filename Extensions
      2. Choosing a Web Server
      3. Installing an All-in-one Package on Windows
    4. Setting Up on Mac OS X
      1. Installing MAMP
      2. Testing and configuring MAMP
    5. Where to Locate Your PHP Files (Windows & Mac)
      1. Using Virtual Hosts
    6. Checking Your PHP Settings
      1. Displaying the Server Configuration with phpinfo()
      2. Editing php.ini
    7. What’s Next?
  12. Chapter 3: How to Write PHP Scripts
    1. PHP: The Big Picture
      1. Telling the Server to Process PHP
      2. Embedding PHP in a Webpage
      3. Storing PHP in an ExternalFile
      4. Using Variables to Represent ChangingValues
      5. Ending Commands With aSemicolon
      6. Commenting Scripts
      7. Using Arrays to Store Multiple Values
      8. PHP’s Built-inSuperglobal Arrays
      9. Understanding When to Use Quotes
      10. MakingDecisions
      11. Making Comparisons
      12. Using Indenting and Whitespace for Clarity
      13. Using Loops for Repetitive Tasks
      14. Using Functions for Preset Tasks
      15. Understanding PHP Classes and Objects
      16. Displaying PHP Output
      17. Understanding PHP ErrorMessages
    2. PHP: A Quick Reference
      1. Using PHP in an ExistingWebsite
      2. Data Types in PHP
      3. Doing Calculations with PHP
      4. Adding to an existing string
      5. All You Ever Wanted to Know About Quotes—and More
      6. Creating Arrays
      7. The Truth According to PHP
      8. Creating Loops
      9. Modularizing Code with Functions
      10. Creating New Variables Dynamically
    3. PHPQuick Checklist
  13. Chapter 4: Lightening Your Workload with Includes
    1. Including Code from External Files
      1. Introducing the PHP Include Commands
      2. Where PHP Looks for Include Files
      3. Choosing the Right Filename Extension forIncludes
      4. Creating Pages with Changing Content
      5. Preventing Errors with Include Files
      6. Suppressing Error Messages on a Live Website
      7. Choosing where to Locate your Include Files
      8. Adjusting your include_path
      9. Why can’t I Use Site-root-relative Links with PHP Includes?
      10. Security Considerations with Includes
    2. Chapter Review
  14. Chapter 5: Bringing Forms to Life
    1. How PHP Gathers Information from a Form
      1. Understanding the Difference Between Post and get
      2. Getting form Data with PHP Superglobals
    2. Processing and Validating User Input
      1. Creating a Reusable Script
      2. Preserving User Input when a Form is Incomplete
      3. Filtering Out Potential Attacks
    3. Sending Email
      1. Using Additional Email Headers Safely
    4. Handling Multiple-Choice Form Elements
    5. Chapter Review
  15. Chapter 6: Uploading Files
    1. How PHP Handles File Uploads
      1. Checking whether your server supports uploads
      2. Adding a file upload field to a form
      3. Understanding the $_FILES array
      4. Establishing an upload directory
    2. Uploading Files
      1. Moving the temporary file to the upload folder
    3. Creating a PHP File Upload Class
      1. Defining a PHP class
      2. Checking upload errors
      3. Changing protected properties
      4. Explicitly changing a data type
      5. Neutralizing potentially dangerous files
      6. Preventing files from being overwritten
    4. Uploading Multiple Files
      1. How the $_FILES array handles multiple files
    5. Using the Upload Class
    6. Points to Watch with File Uploads
    7. Chapter Review
  16. Chapter 7: Using PHP to Manage Files
    1. Checking that PHP Can Open a File
      1. Configuration Settings that Affect File Access
      2. Creating a File Storage Folder for Local Testing
    2. Reading and Writing Files
      1. Reading Files in a Single Operation
      2. Opening and Closing Files for Read/Write Operations
    3. Exploring the File System
      1. Inspecting a Folder with Scandir( )
      2. Inspecting the Contents of a Folder with FilesystemIterator
      3. Restricting File Types with the RegexIterator
    4. Accessing Remote Files
      1. Consuming News and Other RSS Feeds
      2. Using SimpleXML
    5. Creating a Download Link
      1. PHP Solution 7-6: Prompting a User to Download an Image
    6. Chapter Review
  17. Chapter 8: Generating Thumbnail Images
    1. Checking Your Server’s Capabilities
    2. Manipulating Images Dynamically
      1. Making a Smaller Copy of an Image
    3. Resizing an Image Automatically on Upload
      1. Extending a Class
      2. Using the ThumbnailUpload Class
    4. Chapter Review
  18. Chapter 9: Pages That Remember: Simple Login and Multipage Forms
    1. What Sessions Are and How They Work
      1. Creating PHP Sessions
      2. Creating and Destroying Session Variables
      3. Destroying a Session
      4. Regenerating the Session ID
      5. The “Headers Already Sent” Error
    2. Using Sessions to Restrict Access
      1. PHP Solution 9-1: A Simple Session Example
      2. PHP Solution 9-2: Buffering the Output with ob_start( )
      3. Using File-based Authentication
      4. PHP Solution 9-3: Building the Login Page
      5. PHP Solution 9-4: Restricting Access to a Page with a Session
      6. PHP Solution 9-5: Creating a Reusable Logout Button
      7. Making Passwords More Secure
      8. PHP Solution 9-6: Creating a Password-strength Checker
      9. PHP Solution 9-7: Creating a File-based User Registration System
      10. PHP Solution 9-8: Using an Encrypted Login
    3. Setting a Time Limit on Sessions
      1. PHP Solution 9-9: Ending a Session after a Period of Inactivity
    4. Passing Information Through Multipage Forms
      1. PHP Solution 9-10: Using Sessions for a Multipage Form
    5. Chapter Review
  19. Chapter 10: Getting Started with a Database
    1. Which Database Should You Choose?
      1. Compatibility of MariaDB and MySQL
    2. How a Database Stores Information
      1. How primary keys work
      2. Linking tables with primary and foreign keys
      3. Breaking down information into small chunks
      4. Checkpoints for good database design
    3. Using a Graphical Interface
      1. Launching phpMyAdmin
    4. Setting Up the phpsols Database
      1. MySQL naming rules
      2. Using phpMyAdmin to create a new database
      3. Creating database-specific user accounts
      4. Creating a database table
      5. Inserting records into a table
      6. Creating an SQL file for backup and data transfer
    5. Choosing the Right Data Type in MySQL
      1. Storing text
      2. Storing numbers
      3. Storing dates and times
      4. Storing predefined lists
      5. Storing binary data
    6. Chapter Review
  20. Chapter 11: Connecting to a Database with PHP and SQL
    1. Checking Your Remote Server Setup
    2. How PHP Communicates with a Database
      1. Connecting with the MySQL Improved extension
      2. Connecting with PDO
      3. PHP Solution 11-1: Making a reusable database connector
      4. Querying the database and displaying the results
    3. Using SQL to Interact with a Database
      1. Writing SQL queries
      2. Refining the data retrieved by a SELECT query
    4. Understanding the Danger of SQL Injection
    5. Using Prepared Statements for User Input
      1. Embedding variables in MySQLi prepared statements
      2. Embedding variables in PDO prepared statements
    6. Chapter Review
  21. Chapter 12: Creating a Dynamic Photo Gallery
    1. Why Not Store Images in a Database?
    2. Planning the Gallery
    3. Converting the Gallery Elements to PHP
      1. PHP Solution 12-1: Displaying the First Image
    4. Building the Dynamic Elements
      1. Passing Information Through a Query String
      2. Creating a Multicolumn table
      3. Paging Through a Long set of Records
    5. Chapter Review
  22. Chapter 13: Managing Content
    1. Setting Up a Content Management System
      1. Creating the Blog Database Table
      2. Creating the Basic Insert and Update Form
      3. Inserting New Records
      4. Linking to the Update and Delete Pages
      5. Updating Records
      6. Deleting Records
    2. Reviewing the Four Essential SQL Commands
      1. SELECT
      2. INSERT
      3. UPDATE
      4. DELETE
    3. Security and Error Messages
    4. Chapter Review
  23. Chapter 14: Formatting Text and Dates
    1. Displaying a Text Extract
      1. Extracting a Fixed Number of Characters
      2. Ending an Extract on a Complete Word
      3. Extracting the First Paragraph
      4. Displaying Paragraphs
      5. Extracting Complete Sentences
    2. Let’s Make a Date
      1. How MySQL Handles Dates
      2. Inserting Dates into MySQL
      3. Working with Dates in PHP
    3. Chapter Review
  24. Chapter 15: Pulling Data from Multiple Tables
    1. Understanding Table Relationships
    2. Linking an Image to an Article
      1. Altering the Structure of an Existing Table
      2. Inserting a Foreign Key in a Table
      3. Selecting Records from Multiple Tables
      4. Finding Records that don’t have a Matching Foreign Key
      5. Creating an Intelligent Link
    3. Chapter Review
  25. Chapter 16: Managing Multiple Database Tables
    1. Maintaining Referential Integrity
      1. Support for foreign-key constraints
      2. Inserting records into multiple tables
      3. Creating a cross-reference table
      4. Getting the filename of an uploaded image
      5. Adapting the insert form to deal with multiple tables
    2. Updating and Deleting Records in Multiple Tables
      1. Updating records in a cross-reference table
      2. Preserving referential integrity on deletion
      3. Creating delete scripts with foreign-key constraints
      4. Creating delete scripts without foreign-key constraints
    3. Chapter Review
  26. Chapter 17: Authenticating Users with a Database
    1. Choosing an Encryption Method
    2. Using One-Way Encryption
      1. Creating a Table to Store Users’ Details
      2. Registering New Usersin the Database
    3. Using Two-Way Encryption
      1. Creating the table to store users’ details
      2. Registering new users
      3. User authentication with two-way encryption
      4. Decrypting a password
    4. Updating User Details
    5. Where Next?
  27. Index

Product information

  • Title: PHP Solutions: Dynamic Web Design Made Easy, Third Edition
  • Author(s):
  • Release date: December 2014
  • Publisher(s): Apress
  • ISBN: 9781484206355