Table Of ContentWeb Application Design
and Implementation
••e T ..
~I ~
t 807
=1~WlLEY:
.
:12007 ;
•-;' r
.le...T.....I...
THE WILEY BICENTENNIAL-KNOWLEDGE FOR GENERATIONS
<S
achgenerationhasitsuniqueneedsandaspirations.WhenCharlesWileyfirst
openedhissmallprintingshopinlowerManhattanin1807,itwasageneration
ofboundlesspotentialsearchingforanidentity.Andwewerethere,helpingto
defineanewAmericanliterarytradition.Overhalfacenturylater,inthemidst
oftheSecondIndustrialRevolution,itwasagenerationfocusedonbuildingthe
future.Onceagain,wewerethere,supplyingthecriticalscientific,technical,and
engineeringknowledge that helped frame the'world.Throughoutthe 20th
Century,andintothe:newmillennium, nationsbegantoreachoutbeyondtheir
ownbordersanda newinternational community wasborn. Wileywasthere,
expandingitsoperationsaroundtheworldtoenableaglobalexchangeofideas,
opinions,andknow-how.
For200 years,Wiley has beenan integralpart of eachgeneration'sjourney,
enablingtheflowofinformationandunderstandingnecessarytomeettheirneeds
andfulfilltheiraspirations.Today,boldnewtechnologiesarechangingtheway
weliveandlearn.Wileywillbethere,providingyouthemust-haveknowledge
youneedtoimaginenewworlds,newpossibilities,andnewopportunities.
Generationscomeandgo,butyoucanalwayscountonWileytoprovideyouthe
knowledgeyouneed,whenandwhereyouneedit!
Lu~';'~~.~ ~~u~
WILLIAM .... PESCE PETER BaaTH WILEY
PRESIDENTANDCHIEF'EXECUTIVEDFFlCER CHAIRMANOF"THE BOARD
Web Application Design
and Implementation
Apache 2, PHP5, MySQL,
JavaScript, and Linux/UNIX
Steven A. Gabarro
Stevens Institute ofTechnology
Hoboken, NewJersey
IEEE
~computer
SOciety
60TH anniversary
.
"'BICENTENNIAL
it807~
=~WILEY=
.z z
:- 2007~
,~.
BICENTENNIAL
WILEY-INTERSCIENCE
A John Wiley &Sons, Inc., Publication
Copyright©2007 byJohnWiley & Sons, Inc. All rightsreserved.
PublishedbyJohnWiley & Sons, Inc., Hoboken,NewJersey.
Publishedsimultaneouslyin Canada.
No partofthis publicationmay be reproduced,storedinaretrievalsystem,or transmittedin
any form or byany means,electronic,mechanical,photocopying,recording,scanning,or
otherwise,exceptaspermittedunderSection 107or 108ofthe 1976UnitedStatesCopyright
Act, withouteitherthe priorwrittenpermissionofthe Publisher,or authorization through
paymentofthe appropriateper-copyfee to the CopyrightClearanceCenter,Inc., 222
RosewoodDrive,Danvers,M,A01923, (978) 750-8400,fax(978) 750-4470, or on the web at
www.copyright.com. Requeststo the Publisherfor permissionshouldbe addressedto the
PermissionsDepartment,JohnWiley & Sons, Inc., 111RiverStreet,Hoboken,NJ07030,
(201)748-6011,fax (201) 748-6008,or onlineat http://www.wiley.com/go/permission.
LimitofLiability/DisclaimerofWarranty:While the publisherand authorhave used their
besteffortsinpreparingthis book,they make no representationsor warrantieswith respect
to the accuracyor completenessofthe contentsofthis bookand specificallydisclaimany
impliedwarrantiesofmerchantabilityor fitness for aparticularpurpose.No warrantymaybe
createdor extendedbysales representativesor writtensales materials.Theadviceand
strategiescontainedhereinmay not be suitablefor your situation.Youshouldconsultwith a
professionalwhere appropriate.Neitherthe publishernor authorshall be liablefor any loss
ofprofit or any othercommercialdamages,includingbut not limitedto special,incidental,
consequential,or otherdamages.
For generalinformationon ourotherproductsand servicesor for technicalsupport,please
contactourCustomerCareDepartmentwithin the UnitedStatesat (800)762-2974,outside
the UnitedStatesat (317)572·-3993or fax (317)572-4002.
Wiley also publishesitsbooksina varietyofelectronicformats. Some contentthatappears
inprintmay not be availableinelectronicformats. For more informationaboutWiley
products,visitourwebsite atwww.wiley.com.
LibraryofCongress Cataloging-in-Publication Data:
Gabarr6,StevenA., 1979-
Web applicationdesign and implementation: Apache2,PHP5,MySQL,
JavaScript,and Linux/Unix/bySteven A. Gabarro,
p. em.
Includesindex.
ISBN-13: 978-0-471-77391-7 (cloth)
ISBN-I0:0-471-77391-3(cloth)
1. Website development. 2. Web sites-Design. 3. Application
software-Development. I. Title.
TK5105.8883.G332007
006.7-dc22
2006014999
Contents
Preface xiii
About the Author / xiii
Before We Get Started I xiii
Who Should Read This Book? I xiv
About the Examples I xiv
How to Read This Book I xiv
Acknowledgments xv
Introduction: Web Application Recipe 1
Overview I I
Procedure I I
Step I-Understanding the Problem and Finding the
Solution / 1
Step 2-Designing the Database / 2
Step 3-MajorFunctionalities / 2
Step 4-Backside / 2
Step 5-Irnprovements on Functionality I 2
Step 6-Irnprovements on Looks / 3
Step 7-ThoroughTesting, Hacking Attempts / 3
Step 8-Presentation / 3
Step 9-Publication / 3
Step IO-Celebration (and Maintenance) I 4
v
vi CONTENTS
1. Fundamentals 5
The Origins of the Internet I 5
The World Wide Web I 6
The Web Browsers I 7
The Web Servers I 7
TCP/IP BASICS I 8
The Internet Layer I 9
The Transport Layer I 11
The Application Layer I 11
The Toolbox I 12
Browsers I 12
FTP I 13
Email Clients I 14
ProgrammingTools I 14
Other Useful Tools I 15
2. The Different Approaches of Web Programming 17
Before We Get Started I 17
The Basics-HTMloJ I 17
The Creator-SGML I 18
OtherSGML-Based Languages-XML and XSL I 19
The Good Old Java / 20
SomethingDifferent-JavaScript / 21
The Savior-PHP I 22
The Rival-ASP.N]~T I 22
The Myth-CGI I 23
Another BigOption-Perl / 23
The Future?-C# / 24
Client-Side versus Server-Side-Which Side to Pick? I 24
MyChoices-PHP, MySQL, JavaScript I 25
3. Introductionto HTML 27
What Do YouNeed to Get Started? I 27
How Does HTMLWork? I 28
Syntax Basics / 28
File Structure I 28
TagParameters I 30
Basic Text Formatting I 30
External References I 32
Links I 32
Images I 33
CONTENTS vii
Organizing Data / 34
Lists / 35
Tables / 36
Frames / 39
Special Characters I 43
4. Work Environment 45
Introduction I 45
Downloading the Software I 45
Installing the Apache Server I 46
Installation Steps I 46
Checking the Installation I 47
Possible Errors I 47
Configuring Apache I 48
Installing PHP5 I 48
Testing PHP I 50
Installing MySQL I 50
Adding a MySQL User I 51
How Do I Know ifMySQLisRunning? I 51
Installing PhpMyAdmin I 51
Installing a Bulletin Board: phpBB I 52
Installation Steps I 52
BasicSecurity Considerations I 54
Conclusion I 55
5. PHP-A Server-Side ScriptingLanguage 57
How Does It Work? I 57
Some "New" Wordson PHP / 57
Syntax Generalities I 58
Instructions I 58
Operators I 61
Mathematical Functions I 61
DataTypes I 63
Constants I 64
Variables I 65
6. PUP Arrays and Flow of Control 69
Arrays I 69
BasicArrays / 69
Associative Arrays I 70
viii CONTENTS
Multidimensional Arrays / 71
Array Functions / 74
PHPProgram Structure and Flow of Control / 77
Conditions / 77
Loops / 80
Functions / 82
7. Using Files, Folders,and Stringsin PUP 85
Using Files / 85
Folder Manipulation / 89
BasicString Manipulation / 90
Changinga String / 90
Finding and Comparing / 93
FormattingStrings / 94
Manipulating HT~f1LFiles / 95
PHPInformation Functions / 96
Closing Remarks / 97
Writing a BasicFile Explorer / 97
Requirements / 97
Hints / 98
Case Study: An Indexer/Searcher-Step 1 / 98
Overview / 98
The Indexer-Step 1 / 99
8. PUPS and Object-Oriented Programming 101
Overview / 101
Classes and Objects / 101
Classes in PHP / 102
Constructors and Destructors / 103
Visibility / 104
The Scope Resolution Operator / 105
The Static Keyword / 105
Class Constants / 106
Class Abstraction / 106
Object Interfaces / 106
Copying and Cloning Objects / 107
Comparing Objects / 108
Type Hinting / 109
Exceptions / 109
FinalWords / 110
CONTENTS ix
9. CreatingSome Interactivity 111
Overview / 111
Forms / 111
Writing a Form in HTML / 111
GET versusPOST / 115
Retrieving the Form Infomation on a PHP Script / 115
Dynamically CreatingForms / 116
TransferringData Between PHP Scripts / 117
Cookies / 117
Sessions / 120
One Last Useful Function and Design Techniques I 122
Assignments I 123
File Explorer-Step 2 / 123
Case Study: Indexer/Searcher-Step 2 / 124
10. Making CleanerCode and Output 127
Cleaning Up Your Code / 127
What YouNeed / 127
Howto Use It?-HTMLSide I 128
Howto Use It?-PHP Side / 128
Cleaning Up Your Output / 131
The CSSFile I 132
Useful Tools / 134
Assignment / 135
11. Using Databases 137
Overview I 137
Database Basics I 137
The Entity Relationship Model / 137
More Practical Examples I 138
TypicalSources ofError / 139
Simplifying the Diagrams I 140
UsingMySQL I 140
MySQLSyntax / 141
DataTypes / 142
MySQLNumeric Data1)rpes / 142
Date and Time DataTypes / 143
String DataTypes / 144
MySQLOperators / 144
MySQLInstructions / 145
UsingFunctions in MySQL / 150
X CONTENTS
12. Using PhpMyAdmin 151
Overview / 151
Creating a Database / 151
CreatingTables / 152
Accessing an Existing Table / 154
Exporting/Importing a Database Structure and Content / 154
Assignment-Final Project / 157
13. Creating Database-Driven Websites with PHP/MySQL 159
Overview / 159
Connecting to Your MySQL Server with PHP / 159
Submitting SQL Queries / 160
Processing the Results of a Query / 161
Example of Login Procedure / 162
Other Useful Functions / 163
Grouping Our Methods in a Class / 164
Indexer/Searcher-Steps 3and 4 / 168
14. JavaScript-A Client-Side Scripting Language 171
Introduction / 171
JavaScript Syntax / 173
Types of Data and Variables I 173
Operations and Calculations / 173
Arrays / 175
Decisions / 176
Loops / 176
Using Functions / 177
Using Objects / 178
The String Objects / 178
The Math Class / 179
The Array Objects / 181
The Date Objects / 181
15. Programmingthe Browser 185
Overview / 185
The Window Object / 185
The Location Object / 186
The History Object / 186
The Navigator Object / 186
The Screen Object / 187
The Document Object / 187
Description:Product Description: Web Application Design and Implementation uses a hands-on approach of the major technologies and programming languages to teach readers web development. Providing an understanding of all major aspects of web programming in order to achieve the construction of a database-driven w