1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Beginning PHP4 P1 doc

40 465 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Beginning PHP4
Tác giả Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman
Người hướng dẫn David Mercer, Technical Editor, Christian Peak, Technical Editor, Dan Squier, Technical Editor, Julia Gilbert, Technical Editor, Mark Waterhouse, Technical Editor
Trường học Wrox Press Ltd
Chuyên ngành PHP Programming
Thể loại sách
Năm xuất bản 2000
Thành phố Birmingham
Định dạng
Số trang 40
Dung lượng 710,82 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

HTML Form Controls and PHP 76 Text Fields Text Boxes 76 Submit Buttons and Reset Buttons 99 Using Values Returned From Forms In Your PHP Scripts 100 An Example of Branching in Day to

Trang 3

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

Trang 4

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

Contributing Authors Mark Mamone

Additional Material Rick Stones

Dan Squier

Production Manager Technical Architect Laurent Lafon

James Hart

Production Project Co-Ordinator Technical Editors Pip Wonson

David Mercer

Trang 5

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 (http://www.neoqst.com) in the Republic of Korea He is independently working on an open source PHP project called NeoBoard (http://www.neoboard.net), 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

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

Trang 6

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

Trang 8

Downloading the Source Code 5

Wrox Developer's Membership 6 Finding Errata on the Web Site 8

What We Can't Answer 9

Installing PHP4 Alongside IIS 24

Choosing Your Installation Method 29

Which Packages Do We Need? 30 Configuring and Starting Apache with PHP4 31

Trang 9

ii

Trang 10

HTML Form Controls and PHP 76

Text Fields (Text Boxes) 76

Submit Buttons and Reset Buttons 99

Using Values Returned From Forms In Your PHP Scripts 100

An Example of Branching in Day to Day Life 110

Trang 11

iv

Combining Operators 123

Trang 12

Iterating Through Non-Sequential Arrays 172 Iterating Through String-Indexed Arrays 176

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

Array Multisorting 188

Global and Local Variables 207

Using Global Variables Inside Functions 208 Getting Local Variables to Retain their Value 209

Trang 13

vi

Trang 14

Indent Your Code 237 Comment Your Code 237

Use Include Files 239 Use Sensible Variable Names 239

More Form Validation 241

Thinking Like Your Users 241 Protection From Mischievous or Malicious Users 242

Talking to the User – HTTP, HTML, PHP and Interactivity 262 Native Sessions in PHP4 264

Hidden Form Fields Revisited 265

Trang 15

viii

Trang 16

ix

Giving the Calculator a Memory 304 Exploring Our Simple Class Further 307

Passing By Value And By Reference 307

Creating a Class From Scratch 311 Extending an Existing Class 314

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

Other Directory Functions 359 Traversing a Directory Hierarchy 361 Creating a Directory Navigator 363

Trang 17

x

Setting Up The Root Account 387 Testing Our MySQL Server 388

Trang 18

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

Basic Connection Functions 400

Handling Server Errors 403

Creating the Sample Database and Tables with PHP 411 Altering Tables 415 Inserting Data Into a Table 417

Populating our Database Tables 419

Server Functions 426 Retrieving Fields 427

Limiting the Number of Results Returned 428 Ordering the Results 430

Getting Summaries 432 More Complex Retrievals 433

Retrieving Fields from More Than One Table 435

Using the User Viewer 446

Special Characters 450

Trang 19

xii

ENUM Options and Field Defaults 464

Trang 20

xiii

Multiple Mail Components 528

Attaching Files to e-Mail 529

The Image Coordinate System 557

Drawing on our Image 558

Trang 21

xiv

Putting it all Together 563

Trang 22

xv

Interactive Maps 566 Getting Started 567

Creating the database 568

A Stylized Map 585 Palette Limitations 589

User Requirements 594

Users vs Administrators 595 User Authentication 596

Trang 23

xvi

Trang 25

xviii

Trang 26

xix

Trang 27

xx

Trang 28

Introduction

PHP4 is the latest incarnation of PHP – the "PHP Hypertext Preprocessor" It's a programming language for building dynamic, interactive web sites, originally devised by Rasmus Lerdorf way back in 1994 Since then it's been through

a great many changes, and has been adopted by web programmers all around the world So what exactly is it?

In technical terms, PHP4 is a cross-platform, HTML-embedded, server-side web scripting language Let's take a moment to define these terms:

The PHP4 programs we write are run on a server – specifically, a web server

A web scripting language

We run PHP4 programs via a web browser We access the web server on which they reside, and this

runs the program, sending any resulting output back to the browser

This means that we're going to be writing programs that mix PHP4 code and HTML together, using the former to control and format the latter We'll then need to put those programs onto a web server to run them Finally, we're going to access them from a web browser, which displays the resulting HTML This means our programs can be made available for other people to access across the Web, simply by placing them on a public web server

You're probably already familiar with HTML – "HyperText Markup Language" This is the language used to write web pages, combining plain text and special tags that tell a browser how to treat that text We use HTML to describe how different elements in a web page should be displayed, how pages should be linked, where to put images, and so

on

Trang 29

2

Pure HTML documents, for all their versatility, are little more than static arrangements of text and pictures, albeit

nicely presented ones; but the majority of sites you can find on the Web aren't static, but dynamic, even

interactive They show you a list of sites containing words that you've specified, present you with the latest news,

even greet you by name when you log on They allow you to interact, and present different web pages according to

your choices

You can't build a web site like that using raw HTML, and that's where PHP4 comes in So what sort of things can we

do with it? Well, we can program sites that will:

❑ Present data from a wide variety of different sources, such as databases, or files

❑ Incorporate interactive elements, such as search facilities, message boards, and straw-polls

❑ Allow the user to perform actions, such as sending e-mail, or buying something

In other words, PHP4 can be used to write the kind of web sites which anyone familiar with the Web uses every day From e-commerce sites, to search engines, to information portals, most major web sites on the Internet incorporate some or all of these sorts of programming In the course of this book, we'll be using it to build, among other things:

❑ A browser-based text editor, which lets us create and edit files on the web server from our web browser

❑ A web site for a shopping mall, which allows us to search for stores which sell a certain item, and displays the stores on a map

❑ An online word game

❑ A directory-based web search engine, which allows people to search all the web sites in the

directory, or find web sites by navigating through a hierarchical topic structure

❑ A mailing-list service, which allows users to subscribe to different mailing lists, and allows the list administrators to then send out e-mails to the subscribers

So, PHP4 can be used for a diverse range of applications, from utilities like the text editor, to powerful web sites such as the shopping mall and directory examples In this book, we're going to try and equip you with the necessary knowledge to build any kind of web site you want using PHP4 You'll learn some useful coding techniques along the way, and we'll hopefully give you some ideas that you can incorporate into your own applications

Why PHP4?

One of the best things about PHP4 is that it is supported by a large number of Internet Service Providers (ISPs), which means that once you've written an application in PHP4, you can easily put it on the Web for anyone to use You can find a list of ISPs who can help you with hosting PHP-based websites at http://hosts.php.net/

The Prompt

When we start looking at databases in Chapter 11, we'll be introducing the MySQL database manager, and making extensive use of its command line interface If you're primarily using your computer in a

Trang 30

3

graphical environment like Windows or X, you may not be familiar with using the command line interface, or

"shell" Before these graphical environments came into common use, users had to start a new program not by finding its icon and clicking on it, but by typing its name The "shell" is the program that takes the name from you – the

"shell prompt" (or just "prompt") refers specifically to the text that prompts you to enter a new program name, and more generally to working with the shell instead of using a graphical interface Some people still find working with the shell much easier, and many sophisticated shells have been developed to simplify common tasks

To get to a prompt in Windows, look for Command Prompt or DOS Prompt in the Start Menu UNIX users should look for a program called something like console, terminal, konsole, xterm, eterm, or kterm You'll then usually be faced with a black screen with a small amount of text on it that says various things like:

What Do I Need To Use This Book?

We will be focusing on using PHP4 on Windows and UNIX platforms As we shall see in the first chapter, we can install it on Windows 95 and 98, and NT and 2000 It will also run on virtually any flavor of UNIX, although our instructions only detail installation from RPM files

As well as the actual PHP4 libraries, you'll need a text editor to create and edit your scripts We'll look at a number

of options in Chapter 1

You'll need a web server Apache is a good bet on UNIX machines – it's included in most Linux distributions, and you can obtain it from http://www.apache.org Windows users can also use Apache, or alternatively Microsoft's Personal Web Server (for 95 and 98) or Internet Information Server (for NT and 2000) Chapter 1 explains how to get up and running

To get the most out of certain chapters, you'll need to have an Internet connection However, don't panic if you haven't got one – as long as you have the necessary software installed, you can run most of the examples in the book

on a single machine, acting as both client and server

Trang 31

4

PHP4 Resources

Your first stop for information should be the official PHP site, which you can find at www.php.net This not only features news, downloads, and complete documentation (including user feedback), but it also features a complete searchable index of all the above

PHP4 is based on the Zend scripting engine, owned by Zend Technologies, whose site can be found at

www.zend.com Here you'll find information specific to PHP4, as well as articles, case studies, and news about the different uses PHP4 is currently being put to in business environments

Another very useful resource is the www.phpbuilder.com site, a community-driven forum for PHP programmers It's a good place for useful tips and tutorials, and generally finding out what's happening in the PHP programming community

Conventions

We have used various styles of text and layout in the book to help differentiate between different kinds of

information Here are examples of the styles we use and an explanation of what they mean:

Try It Out – A 'Try It Out' Example

'Try It Out' is our way of presenting a practical example

How It Works

Then the 'How It Works' section explains what's going on

Advice, hints and background information come in an indented, italicized font like this

Important bits of information that you shouldn't ignore come in boxes like this!

Important Words are in a bold typeface

❑ Words that appear on the screen in menus like the File or Window menu are in a similar font to that which you see on screen

Keys that you press on the keyboard, like Ctrl and Enter, are in italics

Code is presented in two formats If it's a word that we're talking about in the text, for example, when discussing the

it's shown in a gray box like this:

$fp = fopen("./data.txt", "r");

Ngày đăng: 23/12/2013, 01:16

w