Home :
What you'll learn from each chapter
1. What is PHP—And Why Should I Care ?
A bird's eye view of what PHP is and how it can transform your website by sending feedback from online forms, displaying and updating information dynamically, making websites searcheable and much more. In spite of its power, PHP is easy to learn.
2. Getting Ready to Work with PHP
Although you can do all your development on a live website, it's usually more efficient to set up a local testing environment on your own computer. This chapter leads you step-by-step through the setup process—with separate instructions for Windows and Mac OS X. There's also advice on choosing a good script editor for working with PHP.
3. How to Write PHP Scripts
This chapter is aimed mainly at complete newcomers to PHP. It's divided into two sections. The first gives you an overview of the structure of PHP and how it's written. The second half of the chapter is intended to be a reference that you can come back to when you need a deeper understanding of a particular technique.
4. Lightening your Workload with Includes
This is where the fun begins—putting PHP immediately to practical use. It takes a static website and shows you how to save work by moving elements common to every page—such as a copyright notice and navigation menu—into include files. This way, you can update common elements throughout the site by changing just a single page. (View the case study site)
The chapter also shows you how to automate the content of each page's <title> element using the page's filename, as well as how to display a random image with an appropriate caption. Using PHP to display random images has the advantage that it always works, even if visitors to your site have disabled JavaScript in their browser. Since the book focuses on using PHP safely, you also learn how to prevent sensitive information in include files from being accidentally exposed, and how to suppress ugly error messages when an include file cannot be found.
5. Bringing Forms to Life
Forms lie at the very heart of working with PHP (or any other server-side language). This chapter shows you how to wire up an online feedback form and send the contents to your mail inbox. It covers every type of form element, including radio buttons, check boxes, and multiple choice menus. You also learn how to validate user input, ensuring that all required fields are filled in. When a required item is missing, the form preserves the existing user input and displays an appropriate error message.
The chapter's focus on security shows you how to prevent email header injection attacks.
6. Uploading Files
PHP makes it easy for visitors to upload images and other files to your website—a very useful feature, but one that also exposes the unwary to considerable dangers. In addition to the mechanics of file uploads, this chapter shows you how to restrict the type and size of uploads, making sure that strangers don't get unauthorized access to sensitive files. You also learn a simple technique using the current time to prevent existing files from being overwritten.
7. Using PHP to Manage Files
PHP's file management functions let you read the content of files, write to them, and inspect the content of folders on your computer or web server. This makes it possible to display changing content such as sports statistics on your website without the need for a database. This chapter sorts out the subtleties of PHP read/write modes in an easy-to-follow way, and then shows you how to build a drop-down menu displaying the names of all the images in a folder. Another PHP Solution inspects the contents of a folder and automatically adds the next available number to the filename of an upload.
8. Generating Thumbnail Images
PHP isn't just for handling text and databases. It's also got impressive image manipulation functions. This chapter begins by showing you how to generate a scaled down version of an image. It then expands the project by applying it to an uploaded image and giving the thumbnail a unique name, drawing together elements from Chapters 4, 6 & 7.
9. Pages that Remember: Simple Login and Multipage Forms
After learning how PHP sessions can be used to control access to individual pages on a website, you develop a simple user registration and authentication system that doesn't require a database. The chapter finishes with a demonstration of how session variables can be used to store information gathered from a multipage form.
10. Setting up MySQL and phpMyAdmin
PHP really begins to shine when used in conjunction with a database. MySQL is the most popular open source relational database system, and it's frequently offered as standard by many hosting companies. Everything you need to know about installing and configuring MySQL on both Windows and Mac OS X is covered, together with installation instructions for phpMyAdmin, the PHP-based graphical interface to MySQL.
11. Getting Started with a Database
Your first steps creating a database can be confusing, so this chapter covers the basic essentials, such as how databases store records in tables, and the most commonly used data types. You also learn how to create MySQL user accounts before creating a simple database to store details of images and their associated captions. The chapter finishes by showing you how to use PHP to retrieve the details from the database.
12. Creating a Dynamic Online Gallery
Now that you know how to retrieve a list of image filenames from a database, it's time to build something really useful—an online photo gallery. This chapter covers many of the most important techniques for working with a database: passing information through a URL query string, displaying database results in columns as well as rows, and navigating through a long set of results.
13. Managing Content
This covers the bread-and-butter aspects of working with an online database: inserting new records, updating existing ones, and deleting those no longer required. Great emphasis is placed on security—making sure that your code is protected from malicious attacks through SQL injection. All solutions offer you the choice of using the original MySQL extension in PHP, MySQL Improved, or the new PHP Data Objects (PDO).
14. Solutions to Common PHP/MySQL Problems
This chapter addresses some of the most frequently asked questions about working with PHP and MySQL: how to extract the first section of text from a much longer record, working with dates, and building a database that stores data in multiple tables. It teaches you techniques, such as finding records that don't have a match in every table using a left join, and using a lookup table to resolve many-to-many relationships. You also learn how to prevent accidentally orphaning records by making sure that dependencies are cleared before a record is deleted.
15. Keeping Intruders at Bay
This chapter develops the user authentication and login solutions from Chapter 9 by applying them in conjunction with a database. It shows you how to use one-way and two-way encryption, and discusses the pros and cons of each method.