Tải bản đầy đủ (.pdf) (645 trang)

PHP, MySQL and apache tủ tài liệu bách khoa

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.2 MB, 645 trang )


Teach Yourself
®

PHP, MySQL
and Apache
Julie C. Meloni

All
in
One
800 East 96th Street, Indianapolis, Indiana, 46240 USA


Sams Teach Yourself PHP, MySQL and Apache
All in One

Acquisitions Editor
Shelley Johnston

Copyright © 2004 by Sams Publishing

Development Editor
Chris Newman

All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from
the publisher. No patent liability is assumed with respect to the use of
the information contained herein. Although every precaution has been
taken in the preparation of this book, the publisher and author assume


no responsibility for errors or omissions. Nor is any liability assumed for
damages resulting from the use of the information contained herein.
International Standard Book Number: 0-672-32620-5

Managing Editor
Charlotte Clapp
Project Editor
Dan Knott
Copy Editor
Krista Hansing

Library of Congress Catalog Card Number: 2003109401
Printed in the United States of America
First Printing: December 2003
06

05

04

03

4

3

2

1


Trademarks
All terms mentioned in this book that are known to be trademarks or
service marks have been appropriately capitalized. Sams Publishing
cannot attest to the accuracy of this information. Use of a term in this
book should not be regarded as affecting the validity of any trademark
or service mark.

Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information
provided is on an “as is” basis. The author and the publisher shall have
neither liability nor responsibility to any person or entity with respect to
any loss or damages arising from the information contained in this
book or from the use of the CD or programs accompanying it.

Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in
quantity for bulk purchases or special sales. For more information,
please contact
U.S. Corporate and Government Sales
1-800-382-3419

For sales outside of the U.S., please contact
International Sales
+1-317-428-3341


Indexer
Sharon Shock
Proofreader
Mike Henry

Technical Editor
Chris Newman
Publishing Coordinator
Vanessa Evans
Multimedia Developer
Dan Scherf
Designer
Gary Adair
Page Layout
Julie Parks


Contents at a Glance
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Part I: Laying the Groundwork
CHAPTER 1 Installing and Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Installing and Configuring Apache

......................................

27

3 Installing and Configuring PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Part II: PHP Language Structure
CHAPTER 4 The Building Blocks of PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5 Flow Control Functions in PHP

93
6 Working with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

7 Working with Arrays and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
.............................................

Part III: Getting Involved with the Code
CHAPTER 8 Working with Strings, Dates and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
9 Working with Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
10 Working with User Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
11 Working with Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
12 Working with the Server Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
13 Working with Images

.........................................................

245

Part IV: PHP and MySQL Integration
CHAPTER 14 Learning the Database Design Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
15 Learning Basic SQL Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
16 Interacting with MySQL Using PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321


Part V: Basic Projects
CHAPTER 17 Managing a Simple Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
18 Creating an Online Address Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
19 Creating a Simple Discussion Forum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
20 Creating an Online Storefront. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
21 Creating a Shopping Cart Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
22 Creating a Simple Calendar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
23 Restricting Access to Your Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
24 Logging and Monitoring Web Server Activity . . . . . . . . . . . . . . . . . . . . . . 449

25 Application Localization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467

Part VI: Administration and Fine Tuning
CHAPTER 26 Apache Performance Tuning and Virtual Hosting . . . . . . . . . . . . . . . 481
27 Setting Up a Secure Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
28 Optimizing and Tuning MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
29 Software Upgrades

.............................................................

533

Part VII: Looking Toward the Future
CHAPTER 30 Features and Backwards-Compatibility of PHP 5.0

541
31 Features and Backwards-Compatibility of MySQL 4.1 . . . . . . . . . . 547
..............

Part VIII: Appendix
APPENDIX A Installing MySQL, Apache, and PHP from the CD-ROM . . . . . . 557

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569


Table of Contents
Introduction

1
............................................................


1

..............................................................

2

Who Should Read This Book?
How This Book Is Organized

Conventions Used in This Book

..........................................................

3

Part I: Laying the Groundwork
CHAPTER 1: Installing and Configuring MySQL

7

Current and Future Versions of MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
How to Get MySQL

............................................................................

8

Installing MySQL on Linux/Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Installing MySQL on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

....................................................

16

..................................................................

16

........................................................................

17

Troubleshooting Your Installation
Basic Security Guidelines
Starting MySQL

Securing Your MySQL Connection

............................................

Introducing the MySQL Privilege System

..........................................

18

........................................

19


............................................................

20

............................................................................

21

The Two-Step Authentication Process
Working with User Privileges
Adding Users

18

Removing Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
........................................................................................

23

................................................................................................

25

Summary
Q&A

Workshop

........................................................................................


25

Quiz

........................................................................................

25

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Activities

..................................................................................

26


vi

Sams Teach Yourself PHP, MySQL and Apache All in One

CHAPTER 2: Installing and Configuring Apache

27

Current and Future Versions of Apache

............................................

27


Choosing an Installation Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Building from Source

................................................................

28

Installing a Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Installing Apache on Linux/Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Downloading the Apache Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Uncompressing the Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Preparing to Build Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
................................................

31

........................................................

32

Building and Installing Apache
Installing Apache on Windows

Apache Configuration File Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Conditional Evaluation

............................................................


38

ServerRoot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
..............................................

40

..............................................................................

41

................................................................................

41

..................................................................................

41

Per-Directory Configuration Files
Apache Log Files
access_log
error_log

Additional Files

........................................................................

42


Apache-Related Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Apache Server Binary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Apache Control Script

..............................................................

Starting Apache for the First Time

....................................................

Check Your Configuration File

..................................................

43
44
44

Starting Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Already an Existing Web Server

................................................

46

No Permission to Bind to Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Access Denied

..........................................................................

..............................................................

47

........................................................................................

47

Wrong Group Settings
Summary

46


Contents

Q&A

................................................................................................

48

Workshop

........................................................................................

48

Quiz


........................................................................................

48

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Activities

..................................................................................

CHAPTER 3: Installing and Configuring PHP

49
51

Current and Future Versions of PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Building PHP on Linux/Unix with Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Additional Configuration Options

............................................
............................

54

......................................................

55

Integrating PHP with Apache on Linux/Unix
Installing PHP Files on Windows


53

Integrating PHP with Apache on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
php.ini Basics

..................................................................................

Testing Your Installation

..................................................................

56
57

Getting Installation Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
The Basics of PHP Scripts

..................................................................
....................

60

..................................

62

......................................................

63


Beginning and Ending a Block of PHP Statements
The echo Statement and print() Function
Combining HTML and PHP

59

Adding Comments to PHP Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
........................................................................................

65

................................................................................................

66

Summary
Q&A

Workshop

........................................................................................

66

Quiz

........................................................................................

66


Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Activities

..................................................................................

67

Part II: PHP Language Structure
CHAPTER 4: The Building Blocks of PHP

71

Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Globals and Superglobals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

vii


viii

Sams Teach Yourself PHP, MySQL and Apache All in One

Data Types

......................................................................................

74

Changing Type with settype() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Changing Type by Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

Why Test Type?

........................................................................

79

Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
The Assignment Operator

........................................................

80

Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Concatenation Operator

....................................................

81

Combined Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Automatically Incrementing and Decrementing an Integer
Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Creating More Complex Test Expressions with the Logical
Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
................................................................

86


........................................................................................

87

Operator Precedence
Constants

................................................................

89

........................................................................................

89

................................................................................................

90

Predefined Constants
Summary
Q&A

Workshop

........................................................................................

90

Quiz


........................................................................................

90

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Activities

..................................................................................

CHAPTER 5: Flow Control Functions in PHP

92
93

Switching Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The if Statement

......................................................................

Using the else Clause with the if Statement

..............................

94
95

Using the else if Clause with the if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Using the ? Operator


................................................................

99


Contents

Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
The while Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
The do...while Statement

........................................................

101

The for Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Breaking Out of Loops with the break Statement

....................

104

Skipping an Iteration with the continue Statement . . . . . . . . . . . . . . . . . . 106
Nesting Loops

........................................................................

Code Blocks and Browser Output


....................................................

107
109

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Q&A

..............................................................................................

112

Workshop

......................................................................................

112

Quiz

......................................................................................

112

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Activity

..................................................................................

CHAPTER 6: Working with Functions


113
115

What Is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Defining a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Returning Values from User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Accessing Variables with the global Statement

........................
........

124

..................................................................

126

Saving State Between Function Calls with the static Statement
More About Arguments

122

Setting Default Values for Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
................................

128


............................................

130

Passing Variable References to Functions
Testing for the Existence of a Function

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Q&A

..............................................................................................

133

Workshop

......................................................................................

133

Quiz

......................................................................................

133

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Activity

..................................................................................


134

ix


x

Sams Teach Yourself PHP, MySQL and Apache All in One

CHAPTER 7: Working with Arrays and Objects

135

What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Creating Associative Arrays

....................................................
..........................................

137

........................................................

139

........................................................................

140


Creating Multidimensional Arrays
Some Array-Related Functions
Creating an Object

137

Properties of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Object Methods

......................................................................

143

Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Object Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Q&A

..............................................................................................

148

Workshop

......................................................................................

148

Quiz


......................................................................................

148

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Activities

................................................................................

149

Part III: Getting Involved with the Code
CHAPTER 8: Working with Strings, Dates, and Times

Formatting Strings with PHP

153

..........................................................

154

Working with printf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Specifying a Field Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Argument Swapping

..............................................................

Storing a Formatted String


......................................................

161
162

Investigating Strings in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
A Note About Indexing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Finding the Length of a String with strlen(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Finding a Substring Within a String with strstr() . . . . . . . . . . . . . . . . . . . . . . 163
Finding the Position of a Substring with strpos() . . . . . . . . . . . . . . . . . . . . . . 163
Extracting Part of a String with substr()

..................................

164

Tokenizing a String with strtok() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165


Contents

Manipulating Strings with PHP

......................................................

Cleaning Up a String with trim() and ltrim() and strip_tags()

..


166
166

Replacing a Portion of a String Using substr_replace(). . . . . . . . . . . . . . 168
Replacing Substrings Using str_replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Converting Case. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Wrapping Text with wordwrap() and nl2br(). . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
............................

172

..........................................

172

Breaking Strings into Arrays with explode()
Using Date and Time Functions in PHP

Getting the Date with time() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
..................................

173

......................................

174

Converting a Timestamp with getdate()
Converting a Timestamp with date()


Creating Timestamps with mktime(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Testing a Date with checkdate()

..............................................

178

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Workshop

......................................................................................

179

Q&A

......................................................................................

179

Quiz

......................................................................................

179

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Activities

................................................................................


CHAPTER 9: Working with Forms

Creating a Simple Input Form

180
181

........................................................

Accessing Form Input with User-Defined Arrays

..............................

181
183

Combining HTML and PHP Code on a Single Page . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using Hidden Fields to Save State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Redirecting the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Sending Mail on Form Submission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
System Configuration for the mail() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Creating the Form

..................................................................

193

Creating the Script to Send the Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Formatting Your Mail with HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196


xi


xii

Sams Teach Yourself PHP, MySQL and Apache All in One

Working with File Uploads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Creating the File Upload Form

................................................

198

Creating the File Upload Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Workshop

......................................................................................

202

Quiz

......................................................................................

202

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

Activities

................................................................................

CHAPTER 10: Working with User Sessions

202
203

Session Function Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Starting a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Working with Session Variables

......................................................

Passing Session IDs in the Query String

..........................................

205
209

Destroying Sessions and Unsetting Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Q&A

..............................................................................................

212


Workshop

......................................................................................

212

Quiz

......................................................................................

212

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Activity

..................................................................................

CHAPTER 11: Working with Files and Directories

212
213

Including Files with include() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Returning a Value from an Included Document

......................

215

Using include() Within Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

include_once()

........................................................................

217

The include_path Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Testing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
..................................

218

..............................................................

218

Checking for Existence with file_exists()
A File or a Directory?

Checking the Status of a File

..................................................

Determining File Size with filesize()

........................................

219
219



Contents

Getting Date Information About a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Creating a Function That Performs Multiple File Tests . . . . . . . . . . . . . . 220
Creating and Deleting Files

............................................................

Opening a File for Writing, Reading, or Appending

........................

222
222

Reading from Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Reading Lines from a File with fgets() and feof() . . . . . . . . . . . . . . . . . . . . . . 224
Reading Arbitrary Amounts of Data from a File with fread() . . . . 225
Reading Characters from a File with fgetc()

............................

227

Writing or Appending to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Writing to a File with fwrite() or fputs(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
........................................................

230


................................................................

231

Locking Files with flock()
Working with Directories

Creating Directories with mkdir()

............................................

231

Removing a Directory with rmdir() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Opening a Directory for Reading with opendir() . . . . . . . . . . . . . . . . . . . . . . 232
Reading the Contents of a Directory with readdir() . . . . . . . . . . . . . . . . . . 232
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Q&A

..............................................................................................

235

Workshop

......................................................................................

235


Quiz

......................................................................................

235

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Activities

................................................................................

CHAPTER 12: Working with the Server Environment

236
237

Opening Pipes to and from Processes Using popen() . . . . . . . . . . . . . . . . . . . . . . . . 237
Running Commands with exec()

....................................................

Running Commands with system() or passthru()

............................

239
241

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Q&A


..............................................................................................

243

Workshop

......................................................................................

243

Quiz

......................................................................................

243

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

xiii


xiv

Sams Teach Yourself PHP, MySQL and Apache All in One

CHAPTER 13: Working with Images

245


Understanding the Image-Creation Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
A Word About Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Necessary Modifications to PHP

......................................................

246

Obtaining Additional Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Drawing a New Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Drawing Shapes and Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Using a Color Fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Getting Fancy with Pie Charts

........................................................

251

Modifying Existing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Q&A

..............................................................................................

258

Workshop

......................................................................................


258

Quiz

......................................................................................

258

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Activity

..................................................................................

258

Part IV: PHP and MySQL Integration
CHAPTER 14: Learning the Database Design Process

The Importance of Good Database Design

......................................

261
261

Types of Table Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
One-to-One Relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
One-to-Many Relationships

....................................................


264

Many-to-Many Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Understanding Normalization

........................................................
..................................................

267

..................................................................

268

Problems with the Flat Table
First Normal Form

266

..............................................................

268

................................................................

269

Second Normal Form
Third Normal Form


Following the Design Process

..........................................................

270

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271


Contents

Q&A

..............................................................................................

272

Workshop

......................................................................................

272

Quiz

......................................................................................

272


Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Activity

..................................................................................

272

CHAPTER 15: Learning Basic SQL Commands

273

Learning the MySQL Data Types

273

....................................................

Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Date and Time Types

..............................................................

275

String Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Learning the Table Creation Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Using the INSERT Command

..........................................................


278

A Closer Look at INSERT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Using the SELECT Command

..........................................................

280

Ordering SELECT Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Limiting Your Results

..............................................................

Using WHERE in Your Queries

........................................................

283

........................................

283

................................................

284

........................................................


285

Using Operators in WHERE Clauses
String Comparison Using LIKE
Selecting from Multiple Tables

282

Using JOIN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Using the UPDATE Command to Modify Records

............................

289

Conditional UPDATEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Using Existing Column Values with UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Using the REPLACE Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Using the DELETE Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Conditional DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Frequently Used String Functions in MySQL

....................................

296

Length and Concatenation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Trimming and Padding Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Location and Position Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300


xv


xvi

Sams Teach Yourself PHP, MySQL and Apache All in One

Substring Functions

................................................................

300

String Modification Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
......................................

303

................................................................

303

Using Date and Time Functions in MySQL
Working with Days

Working with Months and Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Working with Weeks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Working with Hours, Minutes, and Seconds

............................


308

Formatting Dates and Times with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Performing Date Arithmetic with MySQL

................................

311

Special Functions and Conversion Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Q&A

..............................................................................................

317

Workshop

......................................................................................

318

Quiz

......................................................................................

318


Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Activity

..................................................................................

CHAPTER 16: Interacting with MySQL Using PHP

319
321

Connecting to MySQL with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Using mysql_connect() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Executing Queries

..................................................................

323

Retrieving Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Working with MySQL Data

............................................................

325

Inserting Data with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Retrieving Data with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Additional MySQL Functions in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Workshop


......................................................................................

332

Quiz

......................................................................................

332

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332


Contents

Part V: Basic Projects
CHAPTER 17: Managing a Simple Mailing List
........................................

335

................................................

336

Developing the Subscription Mechanism
Creating the subscribers Table

335


Creating the Subscription Form

..............................................

336

Developing the Mailing Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Q&A

..............................................................................................

346

Workshop

......................................................................................

346

Quiz

......................................................................................

346

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
CHAPTER 18: Creating an Online Address Book


347

Planning and Creating the Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Creating a Menu

............................................................................

Creating the Record Addition Mechanism

......................................

350
351

Viewing Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Creating the Record Deletion Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Adding Subentries to a Record

........................................................

362

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Workshop

......................................................................................

369

Quiz


......................................................................................

369

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Activities

................................................................................

CHAPTER 19: Creating a Simple Discussion Forum

369
371

Designing the Database Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Creating the Input Forms and Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Displaying the Topic List

................................................................

375

Displaying the Posts in a Topic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Adding Posts to a Topic

..................................................................

382


Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

xvii


xviii

Sams Teach Yourself PHP, MySQL and Apache All in One

CHAPTER 20: Creating an Online Storefront

387

Planning and Creating the Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Inserting Records into the store_categories Table . . . . . . . . . . . . . . . . . . . . . . 389
Inserting Records into the store_items Table

............................

Inserting Records into the store_item_size Table

......................

Inserting Records into the store_item_color Table

....................

390
391
391


Displaying Categories of Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Displaying Items

............................................................................

395

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Workshop

......................................................................................

398

Quiz

......................................................................................

398

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
CHAPTER 21: Creating a Shopping Cart Mechanism

399

Planning and Creating the Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
........................................

401


......................................................

404

....................................................................

405

Integrating the Cart with Your Storefront
Adding Items to Your Cart
Viewing the Cart

Removing Items from Your Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Payment Methods and the Checkout Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Creating the Checkout Form

..................................................

409

Performing the Checkout Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Workshop

......................................................................................

412

Quiz


......................................................................................

412

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
CHAPTER 22: Creating a Simple Calendar

413

Building a Simple Display Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Checking User Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Building the HTML Form

........................................................

414

Creating the Calendar Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Creating a Calendar library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420


Contents

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Q&A

..............................................................................................

426


Workshop

......................................................................................

426

Quiz

......................................................................................

426

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Activity

..................................................................................

CHAPTER 23: Restricting Access to Your Applications

426
427

Authentication Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Client Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Apache Authentication Module Functionality

................................

429


File-Based Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
........................................

432

Using Apache for Access Control

....................................................

433

Implementing Access Rules

....................................................

434

..........................................................

435

Database File-Based Access Control

Evaluating Access Rules

Combining Apache Access Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Limiting Access Based on HTTP Methods

........................................


437

Introducing Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
......................................................

438

..............................................................

439

The Anatomy of a Cookie
Setting a Cookie with PHP

Deleting a Cookie. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Restricting Access Based on Cookie Values

......................................

441

Creating the Authorized Users Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
........................................

443

....................................................

445


Creating the Login Form and Script
Testing for the auth Cookie

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Q&A

..............................................................................................

447

Workshop

......................................................................................

447

Quiz

......................................................................................

447

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Activity

..................................................................................

448


xix


xx

Sams Teach Yourself PHP, MySQL and Apache All in One

CHAPTER 24: Logging and Monitoring Web Server Activity

449

Standard Apache Access Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Deciding What to Log

............................................................

Logging Accesses to Files

........................................................

450
454

Logging Accesses to a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Standard Apache Error Logging

......................................................

455


Logging Errors to a File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Logging Errors to a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
The syslog Daemon Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
The LogLevel Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Managing Apache Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Resolving Hostnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Log Rotation

..........................................................................
....................................................

459

..........................................................................

459

Merging and Splitting Logs
Log Analysis

458

Monitoring Error Logs

............................................................

459

Logging Custom Information to a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Creating the Database Table


..................................................
..............................................

460

........................................................

461

Creating the PHP Code Snippet
Creating Sample Reports

460

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Q&A

..............................................................................................

465

Workshop

......................................................................................

465

Quiz


......................................................................................

465

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
CHAPTER 25: Application Localization

467

About Internationalization and Localization

..................................

467

About Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Environment Modifications

............................................................

Configuration Changes to Apache

..........................................

469
470

Configuration Changes to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Configuration Changes to MySQL


..........................................

471


Contents

Creating a Localized Page Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Q&A

..............................................................................................

477

Workshop

......................................................................................

477

Quiz

......................................................................................

477

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Part VI: Administration and Fine Tuning
CHAPTER 26: Apache Performance Tuning and Virtual Hosting


Scalability Issues

............................................................................

Operating System Limits

........................................................

481
481
482

Performance-Related Apache Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Load Testing with ApacheBench . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Proactive Performance Tuning

........................................................

487

Mapping Files to Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Distributing the Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Reduce Transmitted Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Network Settings

....................................................................

489


Preventing Abuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Robots

....................................................................................

Implementing Virtual Hosting

........................................................

489
490

IP-Based Virtual Hosting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
......................................................

491

..............................................................

493

Name-Based Virtual Hosts
Mass Virtual Hosting

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Q&A

..............................................................................................


Quiz

......................................................................................

496
497

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497

xxi


xxii

Sams Teach Yourself PHP, MySQL and Apache All in One

CHAPTER 27: Setting Up a Secure Web Server

499

The Need for Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Confidentiality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
The SSL Protocol

............................................................................

Addressing the Need for Confidentiality


..................................

500
500

Addressing the Need for Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Addressing the Need for Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Obtaining and Installing SSL Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
................................................................................

505

..................................................................................

506

OpenSSL
mod_ssl

Managing Certificates

....................................................................

Creating a Key Pair

................................................................

508

....................................


509

............................................

510

..........................................................................

511

Creating a Certificate Signing Request
Creating a Self-Signed Certificate
SSL Configuration

508

Starting the Server

..................................................................

511

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Q&A

..............................................................................................

Quiz


......................................................................................

513
513

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
CHAPTER 28: Optimizing and Tuning MySQL

Building an Optimized Platform

....................................................

515

............................................

516

..................................................................

517

Using the benchmark() Function
MySQL Startup Options

515

Key Startup Parameters

..........................................................


518

Optimizing Your Table Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Optimizing Your Queries

................................................................

520

Using the FLUSH Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521


Contents

Using the SHOW Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Retrieving Information About Databases and Tables. . . . . . . . . . . . . . . . 523
Retrieving Table Structure Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Retrieving System Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Q&A

..............................................................................................

530

Workshop

......................................................................................


530

Quiz

......................................................................................

530

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Activities

................................................................................

531

CHAPTER 29: Software Upgrades

533

Staying in the Loop

533

........................................................................

When to Upgrade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Upgrading MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Upgrading Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Modifying Apache Without Upgrading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Upgrading PHP


..............................................................................

536

Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Workshop

......................................................................................

538

Quiz

......................................................................................

538

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Part VII: Looking Toward the Future
CHAPTER 30: Features and Backward Compatibility of PHP 5.0

What’s Wrong With PHP 4?

............................................................

541
541

The New Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542

................................................................

544

....................................................................................

544

Additional New Features
SQLite

XML Support

..........................................................................

So, When Should I Upgrade to PHP 5?

............................................

544
545

Backward Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545

xxiii


Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Q&A


..............................................................................................

CHAPTER 31: Features and Backward Compatibility of MySQL 4.1

Using Subqueries

............................................................................

546
547
547

Subquery Example for Time Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Internationalization Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Additional New Functionality

........................................................

551

Looking Further Ahead to MySQL 5.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Q&A

..............................................................................................

553

Workshop


......................................................................................

553

Quiz

......................................................................................

553

Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Part VIII: Appendix
APPENDIX A: Installing MySQL, Apache, and PHP from the CD-ROM

557

Linux/Unix Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Installing Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Installing PHP

........................................................................

559

Windows Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Installing Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Installing PHP


........................................................................

565

Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Index

569


×