Upload
vannhi
View
224
Download
0
Embed Size (px)
Citation preview
Contents
ACKNOWLEDGMENTS xviiINTRODUCTION xix
PART 1
Writing Basic PHP Programs
1 CreatingPHPPrograms ........................................Creating a PHP Script ..........................................
Writing PHP Scripts .....................................WritingthePHPCodeSkeleton ............................CreatingOutputforaWebBrowser .........................DocumentingaPHPScript ................................
ExecutingaPHPScript """""""""""""""""""'"
UpIoadingaPHPScript """"""""""""""""'"
Executing a Script .......................................Project 1-1: A First PHP Script ...,...............................
TroubleshootingaScript ..................................MasteryCheck ...............................................
2 PHPBuildingBlocks ...........................................Numbers and Strings ...........................................
Numbers ,........Strings ................................................
v
344468
101013131417
19202021
.VI PHP 4: A Beginner's Guide
LiteralsandVariables ..........................................Project 2-1: Viewing the Values of PHP Variables. . . . . . . . . . . . . . . . . . . .Scalars and Arrays .............................................Operators and Functions .......................................
Operators ..............................................Functions ..............................................
Project 2-2: A Calculation in PHP .................................MasteryCheck ...............................................
3 CreatingHTMLForms .........................................FormDesignBasics ............................................
DesigningaForm .......................................CreatinganHTMLForm ..................................
Project 3-1: Viewing Form Fields .................................Creating Form Controls ........................................
Creating Customized Text Boxes ...........................Creating Text Areas ......................................Creating Password Boxes ..................................Creating Check Boxes ....................................Creating Radio Buttons ...................................Creating Selects .........................................Creating Hidden Fields ...................................
Project3-2:AnHTMLBusyBox ..................................MoreonSubmittingaForm .....................................
Using an Image to Submit Data .............................CreatingaResetButton ...................................CreatingaMulti-FormPage ...............................Using a Link to Provide Data to a Script. . . . . . . . . . . . . . . . . . . . . .
Project3-3:AMulti-FormHTMLPage .............................MasteryCheck ...............................................
4 AccessingData ...............................................GettingandUsingDatafromaForm ..............................
SendingOutputtotheBrowser .............................Constructing Strings that Embed Values of Variables. . . . . . . . . . . .
Project 4-1: The E-mail Address Books .............................ProjectGoals ...........................................
Getting and Using Data from an Environment Variable. . . . . . . . . . . . . . . .Project 4-2: Viewing Environment Variables. . . . . . . . . . . . . . . . . . . . . . . .
ProjectGoals ...........................................MasteryCheck ...............................................
2326272929323435
373838394445464647474848495256565757586164
65666870717173777778
Contents..
Vll
5 WorkingwithScalarValues .....................................Working with Constants and Dynamic Variables. . . . . . . . . . . . . . . . . . . . .
Working with Constants ..................................Working with Dynamic Variables ...........................
Working with Types """"""""""""""""""""'"AutomaticTypeConversion ...............................ManualTypeConversion .................................
Project 5-1: A Simple Calculator ..................................Project 5-2: A Date Calculator ...................................Project5-3:ANewsStoryGenerator ..............................MasteryCheck ...............................................
PART2
Writing More Advanced PUP Programs
6 Writing Conditional Statements ,
UsingTrue/FalseValues ........................................Writing Simple if Statements ...................................Project6-1:TestingNumericValues ..............................Writing More Sophisticated if Statements. . . . . . . . . . . . . . . . . . . . . . . . .Writing switch, break, and default Statements. . . . . . . . . . . . . . . . .Writing for Statements ,.....Writing while and do while Statements. . . . . . . . . . . . . . . . . . . . . . . . .Project 6-2: Validating User Input ,....MasteryCheck ...............................................
7 Using Functions 127UsingaFunction , 128Using Included Files 129DefiningaFunction 131
Calling a User-Defined Function 132Terminating Function Execution. . . . . . . . . . . . . . . . . . . . . . . . . .. 133RecursiveFunctions 133DefiningDefaultArguments 134
PHPVariablesandReferences 136.Using Global Variables 136Using Local and Static Variables 137Using References (PHP4) 139
Project7-1:The ContactForm Revisited. . . . . . . . . . . . . . . . . . . . . . . . . .. 141MasteryCheck 148
7980808183838487899296
99100104105107110113115118125
111 PHP 4: A Beginner's Guide
8 Using Arrays 149Creating Arrays 150
Creating an Array Using Assignment. . . . . . . . . . . . . . . . . . . . . . .. 150Usingthearray() Function 152
Iterating Through an Array 154Iterating Through a Sequential Array. . . . . . . . . . . . . . . . . . . . . . .. 155SearchingaSequentialArray 155The break Statement 156The continue Statement 157
Iterating Through a Non-Sequential Array. . . . . . . . . . . . . . . . . . .. 158Working with List Functions 160
Thekey() Function 161Theeach() Function 161Thelist() Function 162
SortingArrays 164Project 8-1: The Contact Form Revisited. . . . . . . . . . . . . . . . . . . . . . . . . .. 166MasteryCheck 170
9 Using Strings .................................................Creating and Displaying Strings ..................................
Additional Escape Sequences """""""""""""""
Working with ASCII Codes ................................Single-quoted Strings .....................................Creating Formatted Output ................................
ManipulatingStrings ...........................................GettingtheLengthofaString ..............................Trimming a String .......................................Converting Strings to All Upper- or Lowercase. . . . . . . . . . . . . . . .
Comparing and Searching Strings .................................Comparing Strings .......................................Finding and Extracting Substrings. . . . . . . . . . . . . . . . . . . . . . . . . .Replacing a Substring ....................................MatchingCharacters .....................................
Project 9-1: A Regular Expression Matcher. . . . . . . . . . . . . . . . . . . . . . . . .Project Goals ...........................................
MasteryCheck ...............................................
PART 3
Working with Stored Data
10 Using Cookies 199Accessing and Creating Cookies 200
AccessingaCookie 200
171172172174175176180180180182183183185187188192192196
Contents.IX
. ",'"""""'""""",,,,"'~,'.".«."""""»..'..~~=W$'
CreatingaCookie 201DeletingaCookie 203
Advanced Cookie Techniques 203Storing Multiple Values in a Cookie. . . . . . . . . . . . . . . . . . . . . . . .. 203Specifying Access to a Cookie and Other Options. . . . . . . . . . . . .. 205
ProjectlO-l:ALoginPage 207MasteryCheck 210
11 Working with Files and Directories 211The Unix File System 212
Working with Unix Files 212Working with Unix Directories 221
WorkingwithFiles 225Ownership and Privilege Issues 226ObtainingaFile'sAttributes 227Changing File Privileges 229Changing File Ownership 230OpeningaFile 230Verifying Completion of a File Operation. . . . . . . . . . . . . . . . . . . .. 232ClosingaFile 233ReadingfromaFile 234NavigatingaFile 237WritingtoaFile 239
Project11-1:APageHitCounter 240Getting Exclusive Access to a File 242
Project 11-2: An Improved Page Hit Counter. . . . . . . . . . . . . . . . . . . . . . .. 244CopyingaFile 246RenamingaFile 247DeletingaFile 247
WorkingwithDirectories 250Getting and Changing the Working Directory. . . . . . . . . . . . . . . .. 250ManipulatingPaths 250Viewing and Changing Directory Privileges. . . . . . . . . . . . . . . . . .. 251ReadingDirectoryContents 252CreatingaDirectory 253Deleting a Directory 253
Projectl1-3:AnAddressBookBrowser 254MasteryCheck 259
12 Sending and Receiving E-mail ,.. 261SendingE-mail 262Project 12-1: A Form Submission Script. . . . . . . . . . . . . . . . . . . . . . . . . . .. 263
ProjectGoals 264
x PHP 4: A Beginner's Guide
ReceivingE-mail 267OpeningaMailbox 267Switching Folders , 269ClosingaMailbox 271Getting Information about a Mailbox. . . . . . . . . . . . . . . . . . . . . . .. 271Getting a list of Messages 274WorkingwithMessagelDs 275Getting Information about a Message. . . . . . . . . . . . . . . . . . . . . . .. 275GettingaMessageBody 276GettingMessageHeaders 277MarkingaMessageforDeletion 279
Project 12-2: An E-mail Browser 281Project Goals 281
ManipulatingFolders 286listing Existing Folders 287CreatingaFolder 287Renaming a Folder 288DeletingaFolder 288Copying Messages to a Folder 289Moving Messages to a Folder 290
MasteryCheck 291
13 ReiationalDatabaseandSQLPrimer 293Relational Database Concepts 294
Structure of a Relational Database. . . . . . . . . . . . . . . . . . . . . . . . . .. 294Rationale for Relational Databases. . . . . . . . . . . . . . . . . . . . . . . . .. 296
Facilitating Sharing of Data 296Providing Data Independence 297Ad Hoc Querying 297Organizing Data 298Securing Data 298Minimizing Necessary Programmer Expertise. . . . . . . . . . . . . . . .. 298Achieving Computational Efficiency. . . . . . . . . . . . . . . . . . . . . . . .. 298Deciding When to Use a Relational DBMS. . . . . . . . . . . . . . . . . . .. 299
DatabaseImplementation 300DatabaseDesign 300E-RModeling 300DatabaseNormalization 304RefinementofE-RModels 307
MySQLDatabaseCreation 309AccessingDatabaseData:SQLQueries 314ChangingDatabaseData 318
Contents.
Xl
Sorting, Aggregating, and Grouping 322Expressions and Functions 325Joins 329Project13-1:WorkingwithSQL 331MasteryCheck 335
PART4
Using Advanced PHP Facilities14 Accessing Relational Databases 339
UsingMySQLDatabases 340ConnectingtotheMySQLServer 340Selecting the Database 342Checking for Errors 342Suppressing Unwanted Errors and Warnings. . . . . . . . . . . . . . . . .. 343Closing the MySQL Server Connection. . . . . . . . . . . . . . . . . . . . . .. 344
Performing UPDATE, INSERT, and DELETEQueries. . . . . . . . . . . . . . . .. 345Themysql_query() Function , 346
Checking Queries That Do Not Return Table Rows. . . . . . . . . . . . . . . . . .. 346Using Auto_Increment TableColumns. . . . . . . . . . . . . . . . . . . . . . . ., 347Processing the Results of SELECTQueries. . . . . . . . . . . . . . . . . . . . . . . . .. 349WorkingwithResultsets 354
Obtaining the Number of Resultset Columns. . . . . . . . . . . . . . . . .. 354Obtaining the Name of a Resultset Column. . . . . . . . . . . . . . . . . .. 354Obtaining the Length of a Resultset Column. . . . . . . . . . . . . . . . .. 355Obtaining the MySQLFlags Associated with a Resultset Column.. 355Obtaining the MySQLType of a Resultset Column. . . . . . . . . . . .. 356Determining the MySQLTable Associatedwith a Resultset Column.. 357Obtaining the Complete Resultset Structure. . . . . . . . . . . . . . . . . .. 357Accessing Resultset Columns Non-sequentially. . . . . . . . . . . . . . .. 359
ExploringtheDBMS 360Determining the Databases Hosted by a Server. . . . . . . . . . . . . . . .. 361Determining the Tables Contained Within a Database. . . . . . . . . .. 362Determining the Columns Contained Within a Table. . . . . . . . . .. 363
Quotes and Magic Quotes 365MagicQuotes 366EscapingandUnescaping , 367EscapingHTMLText 367EscapingURLs , 369
Other PHP Data Management Facilities. . . . . . . . . . . . . . . . . . . . . . . . . . .. 370Postgresql 370ODBC 371
..xu PHP 4: A Beginner's Guide
LDAP 372XML 372
Project 14-1: An Address Book Browser. . . . . . . . . . . . . . . . . . . . . . . . . . .. 374MasteryCheck 380
15 Using Classes and Objects 381Introduction to Object-oriented 382
Classes 383Inheritance 384
Defining and Instantiating a Class 386Instantiating an Object 387DefiningaConstructor 388
Using Inheritance 390Overriding Methods 391Invoking an Overridden Method 393
Object Arrays 395ProjectI5-1:WorkingwithObjects 397MasteryCheck 401
16 Using Application Templates 403Introduction to Templates 404Using a Template 405
Creating the Template File """""""""""""""" 406Instantiating a FastTemplate Object. . . . . . . . . . . . . . . . . . . . . . . .. 406Associating a Template Variable with the Template File. . . . . . . .. 407Assigning Values to the Template Variables. . . . . . . . . . . . . . . . . .. 407Parsing the Template Variable Associated with the Template File.. 408Printing the Template Variable Containing the Result. . . . . . . . . .. 408ConstructingaCompleteWebSite 408
Project 16-1: Working with Templates 410MasteryCheck 415
17 DebuggingPHPScripts 417Debugging and Common Programming Errors. . . . . . . . . . . . . . . . . . . . .. 418
Understanding Bugs 419Syntax Errors 420Run-time Error Messages 421Incorrect or Missing Output 423
ErrorMessageManagementinPHP 424AvoidingErrorMessages 424Suppressing Error Messages 425Logging Error Messages 426
Contents...
Xl11
The Art and Practice of Debugging 429ReproducingtheSymptom 430Pinpointing the Bug 430UnderstandingtheBug 432Fixing the Bug 432Testing the Program 433
Project 17-1: PHPErrorMessages 434MasteryCheck 437
PARTV
Appendixes
A AnswerstoMasteryChecks 441Modulel:CreatingPHPPrograms 442Module2:PHPBuildingBlocks 442Module 3: Creating HTML Forms 443Module4:AccessingData 444Module 5: Working with Scalar Values. . . . . . . . . . . . . . . . . . . . . .. 445Module 6: Writing Conditional Statements. . . . . . . . . . . . . . . . . . .. 446Module7:UsingFunctions 447Module8:UsingArrays 448Module9:UsingStrings 449Modulel0:UsingCookies 449Module 11: Working with Files and Directories. . . . . . . . . . . . . . .. 450Module 12: Sending and Receiving E-mail. . . . . . . . . . . . . . . . . . .. 451Module 13: Relational Database and SQL Primer. . . . . . . . . . . . . . .. 452Module 14: Accessing Relational Databases. . . . . . . . . . . . . . . . . .. 452Module 15: Using Classes and Objects. . . . . . . . . . . . . . . . . . . . . .. 453Module 16: Using Application Templates. . . . . . . . . . . . . . . . . . . .. 454Modulel7:DebuggingPHPScripts 455
B InstallingPHP 457RedHatLinux7.1 458
InstallingApache 458InstallingPHP 458InstallingMySQL 459InstallingIMAP 459ConfiguringMySQL 460Starting the Apache Service 460Testing the Installation 461
Other Linux and Unix Releases 462WindowsNT/2000and95/98 463
Apache 464lIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 464
.XIV PHP 4: A Beginner's Guide
PWS 464PHP 464
C PHPResources 465WebSites 466Mailing lists 467Newsgroups 467
D Unix Essentials 469
BasicUnixConcepts 470Files 470Directories 471Paths 471User Accounts 473User Groups 474Ownership and Permissions 474
BasicUnixTechniques 475LogginglnandOut 476IssuingaUnixCommand 477ChangingYourPassword 477SeeingWho'sLoggedin 478Checking Whether a Host Is Alive 478ReadingandSendingE-Mail 479Changing the Current Working Directory. . . . . . . . . . . . . . . . . . .. 479ViewingDirectoryContents 480
Viewing File Contents and Command Output. . . . . . . . . . . . . . . .. 481EditingaFile 482CreatingaDirectory 482DeletingaFile 482Deleting a Directory 483CopyingaFileorDirectory 483Renaming a File or Directory 484Shell Metacharacters and Name Globbing . . . . . . . . . . . . . . . . . . . .. 484Consulting Online Unix Documentation. . . . . . . . . . . . . . . . . . . .. 485Reporting Disk Space Usage 485SettingFileOwnership 486Setting File Permissions 486Finding a File 487Finding a File Containing Specified Text. . . . . . . . . . . . . . . . . . . .. 488DeterminingtheTypeofaFile 489ComparingTextFiles 489WorkingwithCompressedFiles 489WorkingwithTarballs 490Transferring Files to and from Remote Hosts. . . . . . . . . . . . . . . . .. 491
Contents xv
E Escapes 493
F ASCII 495
G PHP Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 501
H Security """""""""""""""""""""""""'" 505ThreatsandRisks 506Countermeasures 508
Authenticating and Authorizing Users. . . . . . . . . . . . . . . . . . . . . .. 508DistrustingUserlnput 508Employing Cryptographic Measures. . . . . . . . . . . . . . . . . . . . . . . .. 509Properly Configuring Servers 509Securing Databases 509Resources 510
I PHPFunctions 511
Array Functions 513AspellFunctions 515Calendar Functions 516
Class/ObjectFunctions 516Character type Functions 517DateandTimeFunctions 518
Directory Functions 518Error Handling and Logging Functions. . . . . . . . . . . . . . . . . . . . . . . . . . .. 519FilesystemFunctions 519FTPFunctions 522HTTPFunctions 523
IMAP,POP3andNNTPFunctions 524Mail Functions , 527MathematicalFunctions 527Miscellaneous Functions 529
MySQLFunctions 530Output Control Functions 533PHPoptions&information 533Program Execution Functions 535PO~XFunctlons , 535
POSIX Extended Regular Expression Functions. . . . . . . . . . . . . . . . . . . .. 537Session Handling Functions 537String Functions 538URLFunctions 542Variable Functions 542
Index , 545