Beginning PHP4 P1

Chia sẻ: Thach Sau | Ngày: | Loại File: PDF | Số trang:40

lượt xem

Beginning PHP4 P1

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Beginning PHP4 © 2000 Wrox Press All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. The author and publisher have made every effort in the preparation of this book to ensure the accuracy of the information.

Chủ đề:

Nội dung Text: Beginning PHP4 P1

  1. Beginning PHP4 Wankyu Choi Allan Kent Chris Lea Ganesh Prasad Chris Ullman with Jon Blank Sean Cazzell Wrox Press Ltd. 
  2. Beginning PHP4 © 2000 Wrox Press All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. The author and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, Wrox Press nor its dealers or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Published by Wrox Press Ltd, Arden House, 1102 Warwick Road, Acocks Green, Birmingham, B27 6BH, UK Printed in the United States ISBN 1861003730
  3. Trademark Acknowledgements Wrox has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Wrox cannot guarantee the accuracy of this information. Credits Authors Technical Reviewers Wankyu Choi Luis Argerich Allan Kent Darren Beale Chris Lea Mark Fehrenbacher Ganesh Prasad Dario Ferreira Gomes Chris Ullman Chris Harshman Andrew Hill Contributing Authors Mark Mamone Jon Blank Jurgen Prins Sean Cazzell Mark Roedel Bart Ssyszka Additional Material Rick Stones Simon Cozens Travis Swicegood John Kauffman Kevin Yank Dan Squier Production Manager Technical Architect Laurent Lafon James Hart Production Project Co-Ordinator Technical Editors Pip Wonson David Mercer Christian Peak Figures/Illustrations Dan Squier Shabnam Hussain Julia Gilbert Mark Waterhouse Cover Shelley Frazier Category Manager Viv Emery Chapter Divider Dan Squier Author Agent Lynne Bassett Proofreaders Fiona Berryman Index Odette Randall Alessandro Ansa Project Administrator Nicola Phillips
  4. About the Authors Wankyu Choi Wankyu holds a Master's degree in English/Korean interpretation and translation from the Graduate School of Translation & Interpretation. He is the president/CEO of NeoQuest Communications, Inc. running a PHP- powered English language education portal ( in the Republic of Korea. He is independently working on an open source PHP project called NeoBoard (, a feature-rich web discussion board. I'd like to thank my parents for their encouragement and guidance, the dedicated staff at Wrox and technical reviewers for all their hard work, the staff at NeoQuest for their support while working on the book, and last but not the least, my wife, Yonsuk Song for her patience and love for this particular computer nerd. Allan Kent Allan has been programming seriously for the last 7 years and other than the single blemish when he achieved a Diploma in Cobol programming, is entirely self-taught. He started his career working at the local University and now runs his own company. Allan lives in Cape Town, South Africa with his girlfriend and 5 cats. Chris Lea Chris is one of the co-founders of Lucid Designs (, a Venice, CA based web design and development firm. He received a BS in Physics with Highest Honors and Distinction from UNC- CH in 1997, and has been hacking away with lots of (mostly open source) software since then. During his tenure with Lucid, he has worked on a wide variety of web site development projects ranging from the entertainment industry to the financial sector. When he's not staring at his monitor, you can usually find him swing dancing somewhere in the Los Angeles area. For more information, you can always check out his personal site at Ganesh Prasad Ganesh has worked in IT for 13 years, specializing in applications software design and development, in a number of countries including India, the United Arab Emirates, and Australia. He currently works for Reply2 Ltd., Australia, an e-CRM service provider. His experience covers IBM mainframes, VAX and UNIX minis, as well as Windows client/server and Web-based programming. Interests include Java and Open Source software, and his hope is that Open Source software will bring cheap computing to the masses and kick-start Third World economies. He is an Australian citizen, and lives in Sydney with his wife Sashi and son Lalit. I'd like to thank my wife Sashi for her patience, support and encouragement. She took all my other responsibilities off my shoulders, allowing me to concentrate on the task of writing.
  5. Chris Ullman Chris Ullman is a Computer Science graduate who came to Wrox five years ago, when 14.4 modems were the hottest Internet technology and Netscape Navigator 2.0 was a groundbreaking innovation. Since then he's applied his knowledge of HTML, server-side web technologies, Java, and Visual Basic to developing, editing, and authoring books. When not trying to reconstruct the guts of his own PC or trying to write extra chapters in a hurry, he can be found either playing keyboards in psychedelic band, The Bee men, tutoring his cats in the way of eating peacefully from their own food bowl and not the one next to theirs, or hoping against hope that this is the year his favorite soccer team, Birmingham City, can manage to end their exile from the Premier League. Thanks to James and Sarah B for getting me involved on this project and Christian, David, and Dan for deciphering, organizing, and improving my work, and most importantly thanks to my wife Kate, for being there. Jon Blank Jon Blank has been a part of the Linux world since before "Linux" was a buzzword, and has been programming (much to the detriment of his social life) in various languages for the better part of his life. He came to the PHP world by way of Perl, and came to Perl by way of The Web Union, an experimental Web hosting provider for students and non-profit organizations that he runs in his spare time. He currently lives in the New York City area, where he is trying to craft a stable career in a field where tempting offers are always around the corner. He doesn't think he'll succeed. Jon's interests include online role-playing, vintage computers and video games, and graphics arts, as well as many sorts of programming. His primary focus as a programmer is on automation – convincing the computer to do the sort of boring and/or repetitive work that only a computer can stand.
  6. Table of Contents Introduction 1 Why PHP4? 2 The Prompt 2 What Do I Need To Use This Book? 3 PHP4 Resources 4 Conventions 4 Downloading the Source Code 5 Support & Errata 5 Online Forums at 6 Checking the Errata Online at 6 Wrox Developer's Membership 6 Finding Errata on the Web Site 8 Adding Errata and Obtaining e-Mail Support 9 Customer Support 9 Editorial 9 The Authors 9 What We Can't Answer 9 How to Tell Us Exactly What You Think 9 Chapter 1: Installation 11 Installing PHP4 on Windows 95 and 98 13 Obtaining PWS 13 Setting Up PWS 15 Installing PHP4 Alongside PWS 17 Installing PHP4 on Windows NT and 2000 19 Installing PHP4 Alongside IIS 24 Installing PHP4 on Linux and Other UNIXes 28 Choosing Your Installation Method 29 Obtaining RPMs 29 Which Packages Do We Need? 30 Configuring and Starting Apache with PHP4 31
  7. Testing Your Installation 34 ii
  8. Chapter 2: Writing PHP Programs 39 An Example PHP Program 40 Viewing a Web Page 44 Client-Server 44 Internet Protocols 44 The HTTP Protocol 45 The HTTP Request 46 The HTTP Response 48 Where PHP Comes Into This 49 Server-Side Scripting 50 Identifying a PHP Script 51 Caching 51 Variables 52 Data Types 55 String Data Type 55 Numeric Data Types 58 Constants 63 The Define Keyword 64 Initialization 64 Conversions 65 Type Casting 67 gettype and settype 67 isset, unset, and empty 68 Environment Variables 68 Summary 69 Chapter 3: Getting Data from the Client 71 Web Forms 72 The FORM Tag 72 Attributes of FORM 72 ACTION 72 METHOD 73 HTML Form Controls and PHP 76 Text Fields (Text Boxes) 76 Text Areas 80 Check Boxes 82 Radio Buttons 88 List Boxes 91 Hidden Controls 95 Passwords 99 Submit Buttons and Reset Buttons 99 Using Values Returned From Forms In Your PHP Scripts 100 Summary 106 Chapter 4: Decision Making 109 Conditional or Branching Statements 110 An Example of Branching in Day to Day Life 110 If Statements 111 Boolean Values 112 iii
  9. Boolean operators 112 Combining Operators 123 iv
  10. Multiple Conditions – else and elseif 127 Nesting If Statements 129 Switch Statements 135 Form Validation 140 Summary 144 Chapter 5: Loops and Arrays 147 Loops 148 while Loops 148 do while Loops 155 for Loops 159 Arrays 165 Initialization of Arrays 165 Iterating Through an Array 167 Iterating Through Non-Sequential Arrays 172 Iterating Through String-Indexed Arrays 176 Sorting Arrays 177 sort() 177 asort() 178 rsort() and arsort() 179 ksort() 179 Miscellaneous Array Functions 180 array_push() and array_pop() 180 Implode and Explode 180 HTTP_GET_VARS and HTTP_POST_VARS 182 Multi-dimensional Arrays 182 Practical Demonstration of Arrays 183 New Loop and Array Features in PHP4 188 Array Multisorting 188 foreach Loops 189 Summary 191 Chapter 6: Organizing Your Code 193 What's So Great About Code Reuse? 194 Modularization 194 Functions 194 Defining and Calling Functions 195 Switching Functions 201 Assigning the Value Returned by Functions to Variables 202 Passing Values 203 Passing By Value 203 Passing By Reference 203 Setting Default Parameter Values 204 Scope of Variables 206 Global and Local Variables 207 Using Global Variables Inside Functions 208 Getting Local Variables to Retain their Value 209 Nesting 213 v
  11. Recursion 215 vi
  12. Include Files 218 Common Uses of Include Files 221 Summary 224 Chapter 7: Handling and Avoiding Errors 227 Error Handling in PHP 228 Insecure Information 228 Unsightly Web Pages 228 Invisible Error Messages 228 Error Types 229 Syntax Errors 229 Logical Errors 232 Runtime Errors 232 Unexpected Output Errors 235 Errors In the Program Assumptions 235 Good Coding Practice 236 Indent Your Code 237 Comment Your Code 237 Use Functions 238 Use Include Files 239 Use Sensible Variable Names 239 Trying to Break your Code 240 More Form Validation 241 Thinking Like Your Users 241 Protection From Mischievous or Malicious Users 242 Receiving Input from Users 243 Regular Expressions 243 Patterns 243 Special Characters 244 Debugging PHP Script 255 Use echo() 256 Check the HTML Source 257 Suppressing Error Messages 257 Checking The Error Log 257 Doing the Hard Work Manually 258 Summary 258 Chapter 8: Working With the Client 261 Making the Most of a Stateless Protocol 262 Talking to the User – HTTP, HTML, PHP and Interactivity 262 Native Sessions in PHP4 264 'Do It Yourself' Persistence 265 Hidden Form Fields Revisited 265 Query Strings 272 Cookies 280 Sessions 287 vii
  13. PHP4 Sessions 288 Summary 292 viii
  14. Chapter 9: Objects 295 Object-Oriented Terminology 296 Using Pre-Defined Classes 297 Why Use Objects? 300 Giving the Calculator a Memory 304 Exploring Our Simple Class Further 307 Passing By Value And By Reference 307 Creating Classes Of Our Own 311 Creating a Class From Scratch 311 Extending an Existing Class 314 A Useful Object 317 Summary 321 Chapter 10: File and Directory Handling 323 Working with Files 324 Opening and Closing Files 325 Reading and Writing to Files 327 Reading and Writing Characters in Files 331 Reading Entire Files 333 Random Access to File Data 335 Getting Information on Files 339 Time-related Properties 340 Ownership and Permissions 342 Splitting the Name and Path from a File 347 Copying, Renaming and Deleting Files 347 Building a Text Editor 350 Working with Directories 358 Other Directory Functions 359 Traversing a Directory Hierarchy 361 Creating a Directory Navigator 363 Uploading Files 367 Putting it All Together – A Web Text Editor 371 Resources 377 Summary 377 Chapter 11: PHP Database Connectivity 379 Databases 379 Data Models 380 Normalization and Relational Databases 380 Database Architecture 382 Standalone Model 383 Client/Server Model 383 Why MySQL? 384 Installing MySQL 385 Installing On Windows 385 Installing On Linux 386 ix
  15. Setting Up The Root Account 387 Testing Our MySQL Server 388 x
  16. Introduction to SQL 388 Data Types 388 Indexes and Keys 390 Queries 391 A Quick Play with MySQL 393 Running the MySQL Client 393 Selecting a Database to Use 394 Taking a Peek at Data in a Database 394 Manipulating Data in a Database 396 Using GRANT and REVOKE commands 398 GRANT 398 REVOKE 399 Summing Up 399 PHP MySQL Connectivity 400 Basic Connection Functions 400 Handling Server Errors 403 Creating Databases and Tables from MySQL 407 Creating the Sample Database and Tables with PHP 411 Altering Tables 415 Inserting Data Into a Table 417 Escaping Quotes 418 Populating our Database Tables 419 Resources 421 Summary 421 Chapter 12: Retrieving Data from MySQL Using PHP 423 Retrieving Data Using PHP 423 SQL Statements for Retrieving Data 426 Server Functions 426 Retrieving Fields 427 Limiting the Number of Results Returned 428 Ordering the Results 430 Pattern Matching 431 Getting Summaries 432 More Complex Retrievals 433 Retrieving Fields from More Than One Table 435 Putting It All Together 437 Using the User Viewer 446 Resources 447 Summary 447 Chapter 13: Manipulating Data in MySQL Using PHP 449 Inserting Records Using PHP 449 Special Characters 450 Updating and Deleting Records in Tables 452 Working with Date and Time Type Fields 454 xi
  17. Getting Information on Database Tables 458 ENUM Options and Field Defaults 464 xii
  18. Creating a User Registration Script 468 Creating an Access Logger Script 474 Creating a User Manager 482 Resources 491 Summary 491 Chapter 14: XML 493 What is XML? 493 XML Document Structure 496 Well-Formed XML 496 DTDs 499 Event Driven Parsing 501 Parsing the Example File 502 Parsing an External File 508 Summary 512 Chapter 15: e-Mail Handling 515 Sending e-mails in PHP 515 Anatomy of an e-Mail 518 Handling Attachments 525 Anatomy of e-Mail Revisited 525 Content-type 526 Content-transfer-encoding 527 MIME-Version 527 Multiple Mail Components 528 Attaching Files to e-Mail 529 A Newsletter Mailing List Manager 536 Resources 552 Summary 553 Chapter 16: Generating Graphics 555 Laying a Foundation 555 Creating an Image 556 Setting up Colors 556 The Image Coordinate System 557 Drawing on our Image 558 Lines 558 Circles 560 Rectangles 562 xiii
Đồng bộ tài khoản