Maxxus PRO SPK-23 Especificaciones

Busca en linea o descarga Especificaciones para Bicicletas de spin Maxxus PRO SPK-23. Maxxus PRO SPK-23 Specifications Manual de usuario

  • Descarga
  • Añadir a mis manuales
  • Imprimir
  • Pagina
    / 893
  • Tabla de contenidos
  • MARCADORES
  • Valorado. / 5. Basado en revisión del cliente
Vista de pagina 0
Luke Welling and Laura Thomson
201 West 103rd St., Indianapolis, Indiana, 46290 USA
PHP and MySQL
Web Development
00 7842 FM 3/6/01 3:38 PM Page i
Vista de pagina 0
1 2 3 4 5 6 ... 892 893

Indice de contenidos

Pagina 1 - Web Development

Luke Welling and Laura Thomson201 West 103rd St., Indianapolis, Indiana, 46290 USAPHP and MySQLWeb Development00 7842 FM 3/6/01 3:38 PM Page i

Pagina 2 - Warning and Disclaimer

PHP AND MYSQL WEB DEVELOPMENTxReturning Values from Functions ...141Code Blocks ...

Pagina 3 - Overview

Using Loops with each() and list()Because the indices in this associative array are not numbers, we cannot use a simple counterin a for loop to work w

Pagina 4

This line uses each() to take the current element from $prices, return it as an array, and makethe next element current. It also uses list() to turn t

Pagina 5 - Contents

Using PHP, we would write the following code to set up the data in the array shown in Figure 3.3.$products = array( array( “TIR”, “Tires”, 100 ),array

Pagina 6

price => 10 ),array( Code => “SPK”, Description => “Spark Plugs”, price =>4 ) );This array is easier to work with if you want to retrieve

Pagina 7

FIGURE 3.4This three-dimensional array allows us to divide products into categories.From the code that defines this array, you can see that a three-di

Pagina 8

echo “|”.$categories[$layer][$row][$column];}echo “|<BR>”;}}Because of the way multidimensional arrays are created, we could create four-, five-

Pagina 9

The function asort() orders the array according to the value of each element. In the array, thevalues are the prices and the keys are the textual desc

Pagina 10 - PART II Using MySQL 169

The following code sorts this array into alphabetical order using the second column in thearray—the description.function compare($x, $y){if ( $x[1] ==

Pagina 11 - CONTENTS

if ( $x[2] == $y[2] )return 0;else if ( $x[2] < $y[2] )return -1;elsereturn 1;}When usort($products, compare) is called, the array will be placed i

Pagina 12 - PHP AND MYSQL WEB DEVELOPMENT

Reordering ArraysFor some applications, you might want to manipulate the order of the array in other ways. Thefunction shuffle() randomly reorders the

Pagina 13

CONTENTSxiWeb Database Architecture ...180Architecture ...

Pagina 14

Because the code selects random pictures, it produces a different page nearly every time youload it, as shown in Figure 3.5.Using PHPPART I84FIGURE 3.

Pagina 15

Alternatively, we can use the array_reverse() function to reverse the array created byrange().$numbers = range(1,10);$numbers = array_reverse($numbers

Pagina 16

LISTING 3.3 vieworders2.php—Using PHP to Separate, Format, and Display Orders for Bob<html><head><title>Bob’s Auto Parts – Customer

Pagina 17

The code in Listing 3.3 loads the entire file into an array but unlike the example in Listing 3.2,here we are using the function explode() to split up

Pagina 18

There are a number of ways that we could have extracted numbers from these strings. Here weused the function, intval(). As mentioned in Chapter 1, int

Pagina 19

the output would appear in a browser as321Using each(), current(), reset(), end(), next(), pos(), and prev(), you can write yourown code to navigate t

Pagina 20

Occasionally, you might be interested in the key of each element as well as the value. Yourfunction can, as with MyPrint(), choose to ignore the key a

Pagina 21

For example, the following code$array = array(4, 5, 1, 2, 3, 1, 2, 1);$ac = array_count_values($array);creates an array called $ac that containsKey Va

Pagina 22 - PART VI Appendixes 779

TABLE 3.1 Allowed extract_types for extract()Type MeaningEXTR_OVERWRITE Overwrites the existing variable when a collision occurs.EXTR_SKIP Skips an el

Pagina 23 - About the Authors

CHAPTER4String Manipulation andRegular Expressions06 7842 CH04 3/6/01 3:41 PM Page 93

Pagina 24 - Acknowledgments

PHP AND MYSQL WEB DEVELOPMENTDropping a Whole Database ...226Further Reading ...

Pagina 25 - Tell Us What You Think!

Using PHPPART I94In this chapter, we’ll discuss how you can use PHP’s string functions to format and manipulatetext. We’ll also discuss using string f

Pagina 26

mail($toaddress, $subject, $mailcontent, $fromaddress);?><html><head><title>Bob’s Auto Parts - Feedback Submitted</title>&l

Pagina 27 - Introduction

Unsurprisingly, this function sends email. The prototype for mail() looks like this:bool mail(string to, string subject, string message, string [addit

Pagina 28 - What Is PHP?

Depending on your particular purpose, you might like to use the ltrim() or chop() functionsinstead. They are both similar to trim(), taking the string

Pagina 29 - Why Use PHP and MySQL?

Both of these techniques print a string “as is.” You can apply some more sophisticated format-ting using the functions printf() and sprintf(). These w

Pagina 30 - Some of PHP’s Strengths

All conversion specifications start with a % symbol. If you actually want to print a % symbol,you will need to use %%.The padding_character is optiona

Pagina 31 - Some of MySQL’s Strengths

The first column shows the function name, the second describes its effect, the third shows howit would be applied to the string $subject, and the last

Pagina 32 - How Is This Book Organized?

(This rule applies universally to special characters, so if you have \\ in your string, you needto replace it with \\\\.)PHP provides two functions sp

Pagina 33

component parts. PHP provides several string functions (and one regular expression function)that allow us to do this.In our example, Bob wants any cus

Pagina 34

The prototype for strtok() isstring strtok(string input, string separator);The separator can be either a character or a string of characters, but note

Pagina 35 - PHP Crash Course

CONTENTSTable Optimization ...262Using Indexes ...

Pagina 36

We will look at examples using this test string:$test = “Your customer service is excellent”;If you call it with a positive number for start (only), y

Pagina 37

return a number greater than zero. If str1 is less than str2, strcmp() will return a numberless than zero. This function is case sensitive.The functio

Pagina 38

Given the functions we have already looked at, we could use explode() or strtok() toretrieve the individual words in the message, and then compare the

Pagina 39 - Embedding PHP in HTML

There are two variants on strstr(). The first variant is stristr(), which is nearly identicalbut is not case sensitive. This will be useful for this a

Pagina 40 - Using PHP Tags

You can avoid this problem by using the === operator to test return values:$result = strpos($test, “H”);if ($result === false)echo “Not found”elseecho

Pagina 41 - PHP Statements

The length value is optional and represents the point at which PHP will stop replacing. If youdon’t supply this value, the string will be replaced fro

Pagina 42 - Comments

Character Sets and ClassesUsing character sets immediately gives regular expressions more power than exact matchingexpressions. Character sets can be

Pagina 43 - Adding Dynamic Content

TABLE 4.3 Character Classes for Use in POSIX-Style Regular ExpressionsClass Matches[[:alnum:]] Alphanumeric characters[[:alpha:]] Alphabetic character

Pagina 44 - The date() Function

Counted SubexpressionsWe can specify how many times something can be repeated by using a numerical expression incurly braces ( {} ).You can show an ex

Pagina 45 - Accessing Form Variables

Summary of Special CharactersA summary of all the special characters is shown in Tables 4.4 and 4.5. Table 4.4 shows themeaning of special characters

Pagina 46 - String Concatenation

PHP AND MYSQL WEB DEVELOPMENTAuthentication Principles ...291Using Authentication ..

Pagina 47 - Identifiers

The second use is to validate customer email addresses in our application by encoding the stan-dardized format of an email address in a regular expres

Pagina 48 - Variable Types

We can adapt the Smart Form example to use regular expressions as follows:if (!eregi(“^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$”, $email)){echo

Pagina 49 - Variable Variables

while (list($key, $value) = each ($arr))echo “<br>”.$value;This splits the host name into its five components and prints each on a separate line

Pagina 50 - Constants

CHAPTER5Reusing Code and WritingFunctions07 7842 CH05 3/6/01 3:35 PM Page 117

Pagina 51 - Operators

Using PHPPART I118This chapter explains how reusing code leads to more consistent, reliable, maintainable code,with less effort. We will demonstrate t

Pagina 52 - Arithmetic Operators

ReliabilityIf a module of code is in use somewhere in your organization, it has presumably already beenthoroughly tested. Even if it is only a few lin

Pagina 53 - Assignment Operators

If you load reusable.php, it probably won’t surprise you when “Here is a very simplePHP statement.” appears in your browser. If you load main.php, som

Pagina 54

Normally, PHP statements would not be processed if they were in a file called for example,page.html. PHP is usually only called upon to parse files wi

Pagina 55 - Comparison Operators

FIGURE 5.2TLA Consulting has a standard look and feel for all its Web pages.Directly reusing the sections of HTML that are common to all pages is a mu

Pagina 56 - Logical Operators

<!-- page header --><table width=”100%” cellpadding = 12 cellspacing =0 border = 0><tr bgcolor = black><td align = left><im

Pagina 57 - Bitwise Operators

CONTENTSUsing Encryption in PHP ...338Further Reading ...

Pagina 58 - Other Operators

You can see in Listing 5.1 that a number of distinct sections of code exist in this file. TheHTML head contains Cascading Style Sheet (CSS) definition

Pagina 59

LISTING 5.3 header.inc—The Reusable Header for All TLA Web Pages<html><head><title>TLA Consulting Pty Ltd</title><style>

Pagina 60 - Expressions

LISTING 5.4 footer.inc—The Reusable Footer for All TLA Web Pages<!-- page footer --><table width = “100%” bgcolor = black cellpadding = 12 bo

Pagina 61

If we use these directives, we do not need to type require() statements, but the headers andfooters will no longer be optional on pages.If you are usi

Pagina 62 - Variable Functions

This code will needlessly load both files every time the script is run, but only use one depend-ing on the value of $variable. However, if the code ha

Pagina 63 - Reinterpreting Variables

Note that we can create variables in the main file or in the included or required file, and thevariable will exist in both. This behavior is the same

Pagina 64 - Control Structures

Most functions do require one or more parameters—information given to a function when it iscalled that influences the outcome of executing the functio

Pagina 65

representing the file we want to open, and a variable called $openmode containing a string rep-resenting the mode in which we want to open the file. W

Pagina 66

Case and Function NamesNote that calls to functions are not case sensitive, so calling function_name(),Function_Name(), or FUNCTION_NAME() are all val

Pagina 67

This function declaration begins with function, so that human readers and the PHP parserknow that what follows will be a user-defined function. The fu

Pagina 68

PHP AND MYSQL WEB DEVELOPMENTConverting Between PHP and MySQL Date Formats ...396Date Calculations ...

Pagina 69 - Iteration: Repeating Actions

Many languages do allow you to reuse function names. This feature is called function over-loading. However, PHP does not support function overloading,

Pagina 70

FIGURE 5.4This HTML table is the result of calling create_table().Passing a parameter allowed us to get data that was created outside the function—in

Pagina 71

Optional values do not all need to be provided—we can provide some and ignore some.Parameters will be assigned from left to right.Keep in mind that yo

Pagina 72

The following code produces no output. Here we are declaring a variable called $var insideour function fn(). Because this variable is declared inside

Pagina 73

If we want a variable created within a function to be global, we can use the keyword global asfollows:function fn(){global $var;$var = “contents”;echo

Pagina 74

The contents of $value have not changed.This is because of the scope rules. This code creates a variable called $value which contains10. It then calls

Pagina 75 - Storing and Retrieving Data

Returning from FunctionsThe keyword return stops the execution of a function. When a function ends because either allstatements have been executed or

Pagina 76 - Saving Data for Later

$c = 1.9;larger($a, $b);larger($c, $a);larger($d, $a);will be as follows:2.51.9this function requires two numbersReturning Values from FunctionsExitin

Pagina 77

The following code:$a = 1; $b = 2.5; $c = 1.9;echo larger($a, $b).”<br>”;echo larger($c, $a).”<br>”;echo larger($d, $a).”<br>”;will

Pagina 78 - Opening a File

Line 1Line 2Line 1Line 2Line 1Line 2Because the code in these examples is properly indented, you can probably see the differencebetween them at a glan

Pagina 79 - Using fopen() to Open a File

CONTENTSSimple Session Example ...435Configuring Session Control ...

Pagina 80 - Opening Files for FTP or HTTP

In this listing, we have implemented two functions. Both of these will print a string in reverse.The function reverse_r() is recursive, and the functi

Pagina 81 - Problems Opening Files

Further ReadingThe use of include(), require(), function, and return are also explained in the online man-ual. To find out more about concepts such as

Pagina 82

07 7842 CH05 3/6/01 3:35 PM Page 146

Pagina 83 - Writing to a File

CHAPTER6Object-Oriented PHP08 7842 CH06 3/6/01 3:34 PM Page 147

Pagina 84 - Closing a File

Using PHPPART I148This chapter explains concepts of object-oriented development and shows how they can beimplemented in PHP.Key topics in this chapter

Pagina 85 - Reading from a File

In other areas of software development, OO is the norm and function-oriented software is con-sidered old fashioned. For a number of reasons, most Web

Pagina 86 - Knowing When to Stop: feof()

this would rarely be a problem. Bicycles are not likely to get confused and start using a car’smove operation instead. However, a programming language

Pagina 87

Structure of a ClassA minimal class definition looks as follows:class classname{}In order to be useful, our classes need attributes and operations. We

Pagina 88 - Reading a Character: fgetc()

One thing to remember is that PHP does not support function overloading, which means thatyou can only provide one function with any particular name, i

Pagina 89 - Other Useful File Functions

$this->attribute = $paramecho $this->attribute;}}Some programming languages allow you to limit access to attributes by declaring such dataprivat

Pagina 90

PHP AND MYSQL WEB DEVELOPMENT23 Debugging 477Programming Errors ...478Syntax E

Pagina 91 - File Locking

With only a single access point, we can implement checks to make sure that only sensible datais being stored. If it occurs to us later that the value

Pagina 92

We then call operations the same way that we call other functions: by using their name andplacing any parameters that they need in brackets. Because t

Pagina 93

It is important to note that inheritance only works in one direction. The subclass or child inher-its features from its parent or superclass, but the

Pagina 94

Declaring B does not affect the original definition of A. Consider the following two lines ofcode:$a = new A();$a -> operation();We have created an

Pagina 95

FIGURE 6.1PHP does not support multiple inheritance.The left combination shows class C inheriting from class B, which in turn inherits from classA. Ea

Pagina 96 - What Is an Array?

We are going to create a Page class. The main goal of this class is to limit the amount ofHTML needed to create a new page. It should allow us to alte

Pagina 97 - Numerically Indexed Arrays

We can also set attributes to store the page’s title. We will probably want to change this toclearly show what particular page our visitor is looking

Pagina 98 - Accessing Array Contents

$this -> DisplayHeader();$this -> DisplayMenu($this->buttons);echo $this->content;$this -> DisplayFooter();echo “</body>\n</ht

Pagina 99 - Associative Arrays

var $keywords = “TLA Consulting, Three Letter Abbreviation,some of my best friends are search engines”;var $buttons = array( “Home” => “home.ph

Pagina 100 - Using PHP

function DisplayTitle(){echo “<title> $this->title </title>”;}function DisplayKeywords(){echo “<META name=\”keywords\” content=\”$th

Pagina 101 - Multidimensional Arrays

CONTENTSSolution Overview ...542Implementing the Database ...

Pagina 102

{echo “<table width = \”100%\” bgcolor = white”.” cellpadding = 4 cellspacing = 4>\n”;echo “ <tr>\n”;//calculate button size$width = 100/

Pagina 103 - Using Arrays

}function DisplayFooter(){?><table width = “100%” bgcolor = black cellpadding = 12 border = 0><tr><td><p class=foot>&co

Pagina 104 - FIGURE 3.4

2. Creates an instance of the class Page. The instance is called $homepage.3. Calls the operation SetContent() within the object $homepage and pass so

Pagina 105 - Sorting Arrays

LISTING 6.3 services.php—The Services Page Inherits from the Page Class but OverridesDisplay() to Alter the Output<?require (“page.inc”);class Serv

Pagina 106 - Sorting in Reverse

Outside the class definition, we create an instance of our ServicesPage class, set the values forwhich we want non-default values and call Display().A

Pagina 107

IN THIS PART7 Designing Your Web Database 1718 Creating Your Web Database 1839 Working with Your MySQL Database 20710 Accessing Your MySQL Database fr

Pagina 108 - Reverse User Sorts

09 7842 part 2 3/6/01 3:39 PM Page 170

Pagina 109 - Reordering Arrays

CHAPTER7Designing Your Web Database10 7842 CH07 3/6/01 3:34 PM Page 171

Pagina 110 - Using array_reverse()

Using MySQLPART II172Now that you are familiar with the basics of PHP, we’ll begin looking at integrating a databaseinto your scripts. As you might re

Pagina 111 - Loading Arrays from Files

TablesRelational databases are made up of relations, more commonly called tables. A table is exactlywhat it sounds like—a table of data. If you’ve use

Pagina 112

PHP and MySQL Web DevelopmentCopyright © 2001 by Sams PublishingAll rights reserved. No part of this book shall be reproduced, stored in aretrieval sy

Pagina 113

PHP AND MYSQL WEB DEVELOPMENTScript Architecture ...623Logging In and Out

Pagina 114 - Other Array Manipulations

Julie Smith from the Customers table for example. If I open my telephone directory, there aretoo many listings of that name to count.We could distingu

Pagina 115

The relational database term for this relationship is foreign key. CustomerID is the primary key in Customers, but when it appears in another table, s

Pagina 116

written by two coauthors, each of whom had written other books, on their own or possibly withother authors. This type of relationship usually gets a t

Pagina 117

Designing Your Web DatabaseCHAPTER 77DESIGNING YOURWEB DATABASE177CustomerIDCUSTOMERSName Address City1 Julie Smith 25 Oak Street Airport West2 Alan W

Pagina 118 - Further Reading

With this design, we need to insert Julie’s details every time we take an order, so each time wemust check and make sure that her details are consiste

Pagina 119 - Regular Expressions

Choose Sensible KeysMake sure that the keys you choose are unique. In this case, we’ve created a special key forcustomers (CustomerID) and for orders

Pagina 120

The first way means adding a Review column to the Books table. This way, there is a field forthe Review to be added for each book. If many books are i

Pagina 121 - Continued

FIGURE 7.8The client/server relationship between a Web browser and Web server requires communication.The Web database applications we will build in th

Pagina 122 - Formatting Strings

Further ReadingIn this chapter, we covered some guidelines for relational database design. If you want to delveinto the theory behind relational datab

Pagina 123 - IGURE 4.2

CHAPTER8Creating Your Web Database11 7842 CH08 3/6/01 3:38 PM Page 183

Pagina 124

CONTENTSPreviewing the Newsletter ...703Sending the Message ...

Pagina 125 - Changing the Case of a String

Using MySQLPART II184In this chapter we’ll talk about how to set up a MySQL database for use on a Web site.We’ll cover• Creating a database• Users and

Pagina 126 - StripSlashes()

2. Have access to MySQL on a machine that you do not administer such as a Web hostingservice, a machine at your workplace, and so on.If this is the ca

Pagina 127 - IGURE 4.3

The mysql command invokes the MySQL monitor. This is a command line client that connectsyou to the MySQL server.The -h switch is used to specify the h

Pagina 128 - Using strtok()

If it isn’t your machine, make sure that you typed in the password correctly.You should now be at a MySQL command prompt, ready to create the database

Pagina 129 - Using substr()

For the purposes of setting up a Web database, it’s a good idea to set up at least one user perWeb application.You might ask, “Why would I want to do

Pagina 130 - Comparing Strings

The clauses in square brackets are optional. There are a number of placeholders in this syntax.The first, privileges, should be a comma-separated list

Pagina 131 - Functions

Types and Levels of PrivilegeThree basic types of privileges exist in MySQL: privileges suitable for granting to regularusers, privileges suitable for

Pagina 132

widely needed by users. Security is always a trade off between usability and safety. You shouldmake your own decision when it comes to ALTER, but it i

Pagina 133

The REVOKE CommandThe opposite of GRANT is REVOKE. It is used to take privileges away from a user. It is very simi-lar to GRANT in syntax:REVOKE privi

Pagina 134

mysql> revoke alter, create, drop-> on books.*-> from sally;And later, when she doesn’t need to use the database any more, we can revoke her

Pagina 135 - The Basics

PHP AND MYSQL WEB DEVELOPMENTProblems with Headers ...777Extending the Project ...

Pagina 136 - Character Sets and Classes

The first thing you’ll need to do when you log in is to specify which database you want to use.You can do this by typingmysql> use dbname;where dbn

Pagina 137 - Subexpressions

You can run an existing SQL file, such as one loaded from the CD-ROM, through MySQL bytyping> mysql -h host -u bookorama books -p < bookorama.s

Pagina 138 - Branching

Each of the tables is created by a separate CREATE TABLE statement. You see that we’ve createdeach of the tables in the schema with the columns that w

Pagina 139 - Summary of Special Characters

the auto_increment facility so that MySQL can manage these for us—it’s one less thing toworry about.The other columns are all going to hold string typ

Pagina 140

In this case, we don’t need to generate the primary key because ISBNs are generated elsewhere.We’ve left the other fields NULL because a bookstore mig

Pagina 141

| customers || order_items || orders |+-----------------+5 rows in set (0.06 sec)You can also use show to see a list of databases b

Pagina 142 - Expression Functions

A summary of possible identifiers is shown in Table 8.4. The only additional exception is thatyou cannot use ASCII(0) or ASCII(255) in identifiers (an

Pagina 143 - Reusing Code and Writing

Numeric TypesThe numeric types are either integers or floating point numbers. For the floating point num-bers, you can specify the number of digits af

Pagina 144 - Why Reuse Code?

TABLE 8.6 Floating Point Data TypesStorageType Range (Bytes) DescriptionFLOAT(precision) depends on varies Can be used to specifyprecision single or d

Pagina 145 - Using require() and include()

Date and Time TypesMySQL supports a number of date and time types. These are shown in Table 8.7. With allthese types, you can input data in either a s

Pagina 146

About the AuthorsLaura Thomson is a lecturer in Web programming in the Department of Computer Science atRMIT University in Melbourne, Australia. She i

Pagina 147 - PHP Tags and require()

String TypesString types fall into three groups. First, there are plain old strings, that is, short pieces of text.These are the CHAR (fixed length ch

Pagina 148 - FIGURE 5.2

Table 8.10 shows the TEXT and BLOB types. The maximum length of a TEXT field in charactersis the maximum size in bytes of files that could be stored i

Pagina 149 - LISTING 5.1 Continued

Further ReadingFor more information, you can read about setting up a database at the MySQL online manualat http://www.mysql.com/.NextNow that you know

Pagina 150

CHAPTER9Working with Your MySQLDatabase12 7842 CH09 3/6/01 3:36 PM Page 207

Pagina 151

Using MySQLPART II208In this chapter, we’ll discuss Structured Query Language (SQL) and its use in querying data-bases. We’ll continue developing the

Pagina 152

Inserting Data into the DatabaseBefore you can do a lot with a database, you need to store some data in it. The way you willmost commonly do this is w

Pagina 153 - Using include()

we insert a row with a NULL value or no value in this field, MySQL will generate the next num-ber in the autoincrement sequence and insert it for us a

Pagina 154

You can run this script by piping it through MySQL as follows:>mysql -h host -u bookorama -p < book_insert.sqlRetrieving Data from the DatabaseT

Pagina 155 - Using Functions in PHP

which matches all the columns in the specified table or tables. For example, to retrieve allcolumns and all rows from the order_items table, we would

Pagina 156

TABLE 9.1 Useful Comparison Operators for WHERE ClausesOperator Name Example Description(If Applicable)= equality customerid = 3 Tests whether two val

Pagina 157 - Call to Undefined Function

DedicationTo our Mums and Dads.AcknowledgmentsWe would like to thank the team at Sams for all their hard work. In particular, we would like tothank Sh

Pagina 158 - Basic Function Structure

LIKE uses simple SQL pattern matching. Patterns can consist of regular text plus the % (per-cent) character to indicate a wildcard match to any number

Pagina 159 - Naming Your Function

The output of this query is+---------+--------+------------+| orderid | amount | date |+---------+--------+------------+| 2 | 49.99 | 000

Pagina 160 - Parameters

As an extension, it can also be used to disambiguate column names from different databases.In this example, we have used a table.column notation. You

Pagina 161 - UNCTIONS

This query will return the following output:+-----------------+| name |+-----------------+| Michelle Arthur |+-----------------+Notice that

Pagina 162

This output shows us that there are no matching orderids for customers Melissa Jones andMichael Archer because the orderids for those customers are NU

Pagina 163

have values in common. If we want to find customers who live in the same city—perhaps toset up a reading group—we can give the same table (Customers)

Pagina 164

The ORDER BY clause is used to sort the rows on one or more of the columns listed in theSELECT clause. For example,select name, addressfrom customerso

Pagina 165

The most commonly used ones are listed in Table 9.3.TABLE 9.3 Aggregate Functions in MySQLName DescriptionAVG(column) Average of values in the specif

Pagina 166 - Returning from Functions

+------------+-------------+| customerid | avg(amount) |+------------+-------------+| 1 | 49.990002 || 2 | 74.980003 ||

Pagina 167

This query can be read as, “Select name from customers, and then return 3 rows, starting fromrow 2 in the output.” Note that row numbers are zero inde

Pagina 168 - Code Blocks

Tell Us What You Think!As the reader of this book, you are our most important critic and commentator. We value youropinion and want to know what we’re

Pagina 169 - Recursion

Note that in ANSI SQL you can make only one alteration per ALTER TABLE statement, butMySQL allows you to make as many as you like. Each of the alterat

Pagina 170

Let’s look at a few of the more common uses of ALTER TABLE.One thing that comes up frequently is the realization that you haven’t made a particular co

Pagina 171

Dropping TablesAt times you may want to get rid of an entire table. You can do this with the DROP TABLE state-ment. This is very simple, and it looks

Pagina 172

CHAPTER10Accessing Your MySQLDatabase from the Web with PHP13 7842 CH10 3/6/01 3:36 PM Page 227

Pagina 173 - Object-Oriented PHP

Using MySQLPART II228Previously, in our work with PHP, we used a flat file to store and retrieve data. When welooked at this in Chapter 2, “Storing an

Pagina 174 - Object-Oriented Concepts

4. The MySQL server receives the database query, processes it, and sends the results—a listof books—back to the PHP engine.5. The PHP engine finishes

Pagina 175 - Polymorphism

FIGURE 10.1The search form is quite general, so you can search for a book on its title, author, or ISBN.The script that will be called when the Search

Pagina 176 - Inheritance

@ $db = mysql_pconnect(“localhost”, “bookorama”, “bookorama”);if (!$db){echo “Error: Could not connect to database. Please try again later.”;exit;}my

Pagina 177 - Constructors

FIGURE 10.2The results of searching the database for books about Java are presented in a Web page using theresults.php script.The Basic Steps in Query

Pagina 178 - Using Class Attributes

Our next step is to verify that the user has entered a search term and search type. Note that wecheck he entered a search term after trimming whitespa

Pagina 179

00 7842 FM 3/6/01 3:38 PM Page xxvi

Pagina 180 - Calling Class Operations

Setting Up a ConnectionWe use this line in our script to connect to the MySQL server:@ $db = mysql_pconnect(“localhost”, “bookorama”, “bookorama”);We

Pagina 181

Bear in mind that there is a limit to the number of MySQL connections that can exist at thesame time. The MySQL parameter max_connections determines w

Pagina 182 - Overriding

In this case, we are searching for the user-input value ($searchterm) in the field the user speci-fied ($searchtype). You will notice that we have use

Pagina 183 - Multiple Inheritance

It’s useful to know this—if we plan to process or display the results, we know how many thereare and can now loop through them:for ($i=0; $i <$num_

Pagina 184 - Designing Classes

Disconnecting from the DatabaseYou can usemysql_close(database_connection);to close a nonpersistent database connection. This isn’t strictly necessary

Pagina 185

<body><h1>Book-O-Rama - New Book Entry</h1><form action=”insert_book.php” method=”post”><table border=0><tr><td

Pagina 186

{echo “You have not entered all the required details.<br>”.”Please go back and try again.”;exit;}$isbn = addslashes($isbn);$author = addslashe

Pagina 187

FIGURE 10.4The script completes successfully and reports that the book has been added to the database.Again, we have connected to the database using m

Pagina 188 - LISTING 6.1 Continued

Freeing Up ResourcesIf you are having memory problems while a script is running, you might want to usemysql_free_result(). This has the following prot

Pagina 189 - OBJECT-ORIENTED

Further ReadingFor more information on connecting MySQL and PHP together, you can read the appropriatesections of the PHP and MySQL manuals.For more i

Pagina 190

IntroductionWelcome to PHP and MySQL Web Development. Within its pages, you will find distilledknowledge from our experiences using PHP and MySQL, two

Pagina 191

13 7842 CH10 3/6/01 3:36 PM Page 244

Pagina 192

CHAPTER11Advanced MySQL14 7842 CH11 3/6/01 3:35 PM Page 245

Pagina 193

Using MySQLPART II246In this chapter, we’ll cover some more advanced MySQL topics including advanced privileges,security, and optimization.The topics

Pagina 194

Each of these tables stores information about privileges. They are sometimes called granttables. These tables vary in their specific function but all

Pagina 195 - Using MySQL

Each row in this table corresponds to a set of privileges for a user coming from a host and log-ging in with the password Password. These are the scop

Pagina 196

Grant_priv enum(‘N’,’Y’)References_priv enum(‘N’,’Y’)Index_priv enum(‘N’,’Y’)Alter_priv enum(’N’,’Y’)TABLE 11.3 Schema of the host Table in the mysql

Pagina 197 - Designing Your Web Database

TABLE 11.4 Schema of the tables_priv Table in the mysql DatabaseField TypeHost char(60)Db char(64)User char(16)Table_name char(64)Grantor char(77)Time

Pagina 198 - Relational Database Concepts

field is blank, then no password is required. It’s more secure to avoid having blank users,wildcards in hosts, and users without passwords.2. Request

Pagina 199

MySQL from the Operating System’s Point of ViewIt’s a bad idea to run the MySQL server (mysqld) as root if you are running a UNIX-like oper-ating syst

Pagina 200

We will use this functionality when we come to implement the projects in Part 5, “BuildingPractical PHP and MySQL Projects.”User PrivilegesKnowledge i

Pagina 201 - Relationships

PHP AND MYSQL WEB DEVELOPMENTUsing a language such as PHP and a database such as MySQL allows you to make your sitesdynamic: to have them be customiza

Pagina 202 - Avoid Storing Redundant Data

You should always check all data coming in from a user. Even if your HTML form consistedof select boxes and radio buttons, someone might alter the URL

Pagina 203

11ADVANCEDMYSQLAdvanced MySQLCHAPTER 11255+------------------------------------------------------------------------------------------------+| Grants f

Pagina 204 - Use Atomic Column Values

SHOW COLUMNS FROM table Lists all the columns in a particular table[FROM database] [LIKE column] from the database currently in use, or fromthe databa

Pagina 205 - Database

SHOW GRANTS FOR user Shows the GRANT statements required to givethe user specified in user his current levelof privilege.Getting Information About Col

Pagina 206 - Web Database Architecture

Using MySQLPART II258+-------------+--------+---------------+---------+---------+------------------+------+-------------+| table | type | poss

Pagina 207

In the previous example, you can see that one of the tables is joined using eq_ref (books), andone is joined using ref (order_items), but the other tw

Pagina 208

There are several ways you can fix problems you spot in the output from EXPLAIN.First, check column types and make sure they are the same. This applie

Pagina 209 - Creating Your Web Database

Speeding Up Queries with IndexesIf you are in the situation mentioned previously, in which the possible_keys column from anEXPLAIN contains some NULL

Pagina 210

You can also use the myisamchk utility to sort a table index and the data according to thatindex, like this:>myisamchk --sort-index --sort-records=

Pagina 211 - How to Log In to MySQL

• BDB. These tables are transaction safe; that is, they provide COMMIT and ROLLBACK capabil-ities. They are slower to use than the MyISAM tables, and

Pagina 212

INTRODUCTIONThe home page for PHP is available at http://www.php.netThe home page for Zend is at http://www.zend.comWhat’s New In PHP Version 4?If you

Pagina 213 - Users and Privileges

14 7842 CH11 3/6/01 3:35 PM Page 264

Pagina 214 - Principle of Least Privilege

IN THIS PART12 Running an E-commerce Site 26713 E-commerce Security Issues 28114 Implementing Authentication with PHP andMySQL 30315 Implementing Secu

Pagina 215

15 7842 part 3 3/6/01 3:42 PM Page 266

Pagina 216 - Types and Levels of Privilege

CHAPTER12Running an E-commerce Site16 7842 CH12 3/6/01 3:43 PM Page 267

Pagina 217

E-commerce and SecurityPART III268This chapter introduces some of the issues involved in specifying, designing, building, andmaintaining an e-commerce

Pagina 218 - The REVOKE Command

Online BrochuresNearly all the commercial Web sites in the early 1990s were simply an online brochure or salestool. This type of site is still the mos

Pagina 219 - Using the Right Database

Regardless of the size of your company, make sure that your Web site is of a high standard.Text should be written and proofread by somebody with a ver

Pagina 220 - Creating Database Tables

Simpler or cheaper options includeExamining Server Logs: Web servers store a lot of data about every request from your server. Much of this data is us

Pagina 221

make a decision now. The more time you give people to reconsider a purchasing decision, themore likely they are to shop around or change their mind. I

Pagina 222 - What the Other Keywords Mean

Some products are unlikely to succeed as e-commerce categories. Cheap, perishable items,such as groceries, seem a poor choice, although this has not d

Pagina 223

OverviewIntroduction 1PART I Using PHP1 PHP Crash Course 92 Storing and Retrieving Data 493 Using Arrays 694 String Manipulation and Regular Expressio

Pagina 224

PHP AND MYSQL WEB DEVELOPMENTTo demonstrate this, the examples in this book have been written and tested on two popular setups:• Linux using the Apach

Pagina 225 - MySQL Identifiers

Are you a reputable business? If your business is registered with the relevant authorityin a particular place, has a physical address and a phone numb

Pagina 226 - Column Data Types

CompatibilityBe sure to test your site in a number of browsers and operating systems. If the site does notwork for a popular browser or operating syst

Pagina 227 - Numeric Types

digitally, you need to do it immediately. You cannot manually oversee the process, or spreadpeaks of activity through the day. Immediate delivery syst

Pagina 228

Centralization can cut costs. If you have numerous physical sites, you need to pay numerousrents and overheads, staff at all of them, and the costs of

Pagina 229 - Date and Time Types

Most successful attacks on computer systems take advantage of well-known weaknesses suchas easily guessed passwords, common misconfigurations, and old

Pagina 230 - String Types

that sell similar things in surrounding areas. New competitors will open occasionally. With e-commerce, the terrain is less certain.Depending on shipp

Pagina 231

Deciding on a StrategySome people believe that the Internet changes too fast to allow effective planning. We wouldargue that it is this very changeabi

Pagina 232

CHAPTER13E-commerce Security Issues17 7842 CH13 3/6/01 3:36 PM Page 281

Pagina 233 - Working with Your MySQL

E-commerce and SecurityPART III282This chapter discusses the role of security in e-commerce. We will discuss who might be inter-ested in your informat

Pagina 234 - What Is SQL?

E-commerce Security IssuesCHAPTER 1313E-COMMERCESECURITY ISSUES283Hobby users will probably have limited time to learn about or work towards securing

Pagina 235 - OUR MYSQL

INTRODUCTIONCostPHP is free. You can download the latest version at any time from http://www.php.net forno charge.Learning PHPThe syntax of PHP is bas

Pagina 236

and should only contain information that either needs to be provided to the public or hasrecently been collected from the public.To reduce the risk of

Pagina 237

FIGURE 13.1Transmitting information via the Internet sends your information via a number of potentially untrustworthy hosts.To see the path that data

Pagina 238

You can take various measures to reduce the chance of data loss. Secure your servers againstcrackers. Keep the number of staff with access to your mac

Pagina 239

File integrity assessment software, such as Tripwire, records information about important files in aknown safe state, probably immediately after insta

Pagina 240 - Simple Two-Table Joins

Errors in SoftwareIt is possible that the software you have bought, obtained, or written has serious errors in it.Given the short development times no

Pagina 241

What is needed is a well-designed test plan that tests all the functions of your software on arepresentative sample of common machine types. A well-pl

Pagina 242 - Joining More Than Two Tables

An alliance between VISA, a number of financial organizations, and software companies, hasbeen promoting a standard called Secure Electronic Transacti

Pagina 243 - Finding Rows That Don’t Match

Creating a Security PolicyA security policy is a document that describes• The general philosophy towards security in your organization• What is to be

Pagina 244

nobody else knows or can guess the password, this is secure. Passwords on their own have anumber of potential weaknesses and do not provide strong aut

Pagina 245 - Summary of Joins

FIGURE 13.2Web browsers prompt users for authentication when they attempt to visit a restricted directory on a Web server.Both the Apache Web server a

Pagina 246 - Grouping and Aggregating Data

PHP AND MYSQL WEB DEVELOPMENTEase of UseMost modern databases use SQL. If you have used another RDBMS, you should have no troubleadapting to this one.

Pagina 247 - Aggregate Functions in MySQL

To create the protected directory whose authentication prompt is shown in Figure 13.2, weused Apache’s most basic type of authentication. (You’ll see

Pagina 248 - Choosing Which Rows to Return

your encrypted messages. As shown in Figure 13.4, both the sender (who encrypts the mes-sage) and the recipient (who decrypts the message) have the sa

Pagina 249

The most common public key algorithm is RSA, developed by Rivest, Shamir, and Adelman atMIT and published in 1978. RSA was a proprietary system, but t

Pagina 250

When a signed message is received, it can be checked. The signature is decrypted using thesender’s public key. A hash value is generated for the messa

Pagina 251

FIGURE 13.6The certificate path for www.equifaxsecure.com shows the network of trust that enables us to trust this site.The most common use for digita

Pagina 252 - Dropping a Whole Database

Installation instructions for the two most popular Web servers, Apache and IIS, are inAppendix A, “Installing PHP 4 and MySQL.” You can begin using SS

Pagina 253 - Database from the Web

Log files can help you detect erroneous or malicious behavior as it occurs. They can also tellyou how a problem or break-in occurred if you check them

Pagina 254

Backing Up DataYou cannot underestimate the importance of backups in any disaster recovery plan. Hardwareand buildings can be insured and replaced, or

Pagina 255 - CCESSING

Physical SecurityThe security threats we have considered so far relate to intangibles such as software, but youshould not neglect the physical securit

Pagina 256 - FIGURE 10.1

CHAPTER14Implementing Authenticationwith PHP and MySQL18 7842 CH14 3/6/01 3:35 PM Page 303

Pagina 257 - LISTING 10.2 Continued

IN THIS PART1 PHP Crash Course 92 Storing and Retrieving Data 493 Using Arrays 694 String Manipulation and Regular Expressions 935 Reusing Code and Wr

Pagina 258

E-commerce and SecurityPART III304This chapter will discuss how to implement various PHP and MySQL techniques for authenti-cating a user.Topics includ

Pagina 259

their customers’ details when they make their first order. This means that a customer is notrequired to type her details every time.Having asked for a

Pagina 260 - Setting Up a Connection

FIGURE 14.2When users enter incorrect details, we need to give them an error message. On a real site, you might want to give asomewhat friendlier mess

Pagina 261 - Querying the Database

<form method = post action = “secret.php”><table border = 1><tr><th> Username </th><td> <input type = text name

Pagina 262 - Retrieving the Query Results

Storing PasswordsThere are many better places to store usernames and passwords than inside the script. Insidethe script, it is difficult to modify the

Pagina 263

</tr></table></form><?}else{// connect to mysql$mysql = mysql_connect( ‘localhost’, ‘webauth’, ‘webauth’ );if(!$mysql){echo ‘Cann

Pagina 264

// visitor’s name and password combination are not correctecho “<h1>Go Away!</h1>”;echo “You are not authorized to view this resource.”;}}

Pagina 265

The PHP function crypt() provides a one-way cryptographic hash function. The prototype forthis function isstring crypt (string str [, string salt])Giv

Pagina 266

Protecting Multiple PagesMaking a script like this protect more than one page is a little harder. Because HTTP is state-less, there is no automatic li

Pagina 267 - IGURE 10.4

browser is then responsible for displaying a dialog box or similar device to get required infor-mation from the user.Although the Web server requests

Pagina 268 - Other PHP-Database Interfaces

02 7842 part 1 3/6/01 3:42 PM Page 8

Pagina 269

Using Basic Authentication in PHPPHP scripts are generally cross-platform, but using basic authentication relies on environmentvariables set by the se

Pagina 270

The code in Listing 14.4 acts in a very similar way to the previous listings in this chapter. Ifthe user has not yet provided authentication informati

Pagina 271 - Advanced MySQL

Using Basic Authentication with Apache’s .htaccessFilesWe can achieve very similar results to the previous script without writing a PHP script.The Apa

Pagina 272

LISTING 14.7 .htaccess—An .htaccess File Can Set Many Apache Configuration Settings,Including Activating AuthenticationErrorDocument 401 /chapter14/re

Pagina 273 - The user Table

Like the PHP example, to use HTTP authentication, we need to name our realm as follows:AuthName “Realm-Name”You can choose any realm name you prefer,

Pagina 274 - The db and host Tables

The optional m, d, p, or s switches can be used if you want to specify which encryption algo-rithm (including no encryption) you would like to use.The

Pagina 275

FIGURE 14.5The Microsoft Management Console allows us to configure Internet Information Server 5.To add basic authentication to the protected director

Pagina 276

In order to duplicate the behavior of the previous examples, we will also provide a page to tellusers that their authentication details were not corre

Pagina 277

Using mod_auth_mysql AuthenticationAs already mentioned, using mod_auth with Apache is easy to set up and is effective. Becauseit stores users in a te

Pagina 278 - Passwords

5. After following the other steps in Appendix A, you will need to create a database andtable in MySQL to contain authentication information. This doe

Pagina 279 - Web Issues

CHAPTER1PHP Crash Course03 7842 CH01 3/6/01 3:39 PM Page 9

Pagina 280 - Getting Information with SHOW

specify basic authentication and give a realm name. As in Listing 14.7, we will allow anyvalid, authenticated user access.Because we are using mod_aut

Pagina 281

The documentation for mod_auth_mysql can be found athttp://www.zend.comorhttp://www.express.ru/docs/mod_auth_mysql_base.htmlNextThe next chapter expla

Pagina 282

18 7842 CH14 3/6/01 3:35 PM Page 326

Pagina 283

CHAPTER15Implementing SecureTransactions with PHP andMySQL19 7842 CH15 3/6/01 3:40 PM Page 327

Pagina 284

E-commerce and SecurityPART III328In this chapter, we will explain how to deal with user data securely from input, through trans-mission, and in stora

Pagina 285

the user’s browser sending a request through the Internet to the Web server. If the page is aPHP script, the Web server will delegate processing the p

Pagina 286

We will look at the cURL library, which can be used to simulate connections from a browser,in Chapter 17, “Using Network and Protocol Functions.” This

Pagina 287 - General Optimization Tips

• Decide that your information is too sensitive to risk any chance of interception and findanother way to distribute your information.The Internet is

Pagina 288 - Different Table Types

One specific thing to consider when installing PHP is that it is generally more secure, as wellas much more efficient, to install PHP as a SAPI module

Pagina 289 - Loading Data from a File

Networking protocols and the software that implements them are usually arranged as a stack oflayers. Each layer can pass data to the layer above or be

Pagina 290

Using PHPPART I10This chapter gives you a quick overview of PHP syntax and language constructs. If you arealready a PHP programmer, it might fill some

Pagina 291 - E-commerce and Security

SSL is theoretically capable of providing a secure transmission environment for protocols otherthan HTTP, but is normally only used for HTTP. Other pr

Pagina 292

FIGURE 15.4SSL breaks up, compresses, hashes, and encrypts data before sending it.One thing you might notice from the diagram is that the TCP header i

Pagina 293 - Running an E-commerce Site

Screening User InputOne of the principles of building a safe Web application is that you should never trust userinput. Always screen user data before

Pagina 294 - Types of Commercial Web Sites

not have permission to write or create new files in directories that can be loaded from the Webserver. If you allow others to write files here, they c

Pagina 295 - Online Brochures

the Web server. If you are not the administrator for your Web server (as is likely the case if youare sharing a server), it might be worth discussing

Pagina 296 - Allowing a Site to Age

To obtain PGP for use outside the USA and Canada, see the list of international download sitesat the international PGP page:http://www.pgpi.orgAn Open

Pagina 297

For a Windows server, the process is just as easy. Download the zip file, unzip it and placegpg.exe somewhere in your PATH. (C:\Windows\ or similar wi

Pagina 298

Log in to your account on the Web server and change the permissions on the file so that otherusers will be able to read it. Typechmod 644 filenameYou

Pagina 299 - Unanswered Questions

Options within this program include help, which will describe the available commands—trust, sign, and save.Type trust and tell GPG that you trust your

Pagina 300 - Ease of Use

If you have GPG set up so that the user your PHP scripts run as can use it from the commandline, you are most of the way there. If this is not working

Pagina 301 - Compatibility

Using PHPIn order to work through the examples in this chapter and the rest of the book, you will needaccess to a Web server with PHP installed. To ge

Pagina 302 - Cutting Costs

//create a unique file name$infile = tempnam(“”, “pgp”);$outfile = $infile.”.asc”;//write the user’s text to the file$fp = fopen($infile, “w”);fwrite(

Pagina 303 - Risks and Threats

<p>Your message could not be encrypted, so has not been sent.<p>Sorry.”;}?>In order to make this code work for you, you will need to ch

Pagina 304 - Extensive Competition

While we are thinking about the security of our script, it is important to consider all flows ofinformation within our system. GPG will encrypt our em

Pagina 305 - System Capacity Limits

Currently, our open form tag looks like this:<form method = post action = send_private_mail.php>We could alter it to send data via SSL even if t

Pagina 306 - Deciding on a Strategy

19 7842 CH15 3/6/01 3:40 PM Page 348

Pagina 307 - E-commerce Security Issues

IN THIS PART16 Interacting with the File System and the Server 35117 Using Network and Protocol Functions 36918 Managing the Date and Time 39119 Gener

Pagina 308

20 7842 part 4 3/6/01 3:35 PM Page 350

Pagina 309 - Security Threats

CHAPTER16Interacting with the FileSystem and the Server21 7842 CH16 3/6/01 3:40 PM Page 351

Pagina 310

Advanced PHP TechniquesPART IV352In Chapter 2, “Storing and Retrieving Data,” we saw how to read data from and write data tofiles on the Web server. I

Pagina 311 - Loss or Destruction of Data

As you can see, the form has a box where the user can enter a filename, or click the Browsebutton to browse files available to him locally. You might

Pagina 312 - Modification of Data

Part of the HTML for this is shown in Listing 1.1. There are two important things to notice inthis code.LISTING 1.1 orderform.html—HTML for Bob’s Basi

Pagina 313 - Denial of Service

You can choose whatever name you like for the file, but keep it in mind as you will usethis name to access your file from the receiving PHP script.Wri

Pagina 314 - Errors in Software

<?if ($userfile==”none”){echo “Problem: no file uploaded”;exit;}if ($userfile_size==0){echo “Problem: uploaded file is zero length”;exit;}if ($user

Pagina 315 - Repudiation

echo “Preview of uploaded file contents:<br><hr>”;echo $contents;echo “<br><hr>”;?></body></html><?// This f

Pagina 316 - Security

Finally we display the contents of the file so the user can see that their file uploaded successfully.The results of one (successful) run of this scri

Pagina 317 - Authentication Principles

Common ProblemsThere are a few things to keep in mind when performing file uploads.• The previous example assumes that users have been authenticated e

Pagina 318 - Using Authentication

LISTING 16.3 browsedir.php—A Directory Listing of the Uploaded Files<html><head><title>Browse Directories</title></head>

Pagina 319 - Encryption Basics

FIGURE 16.3The directory listing shows all the files in the chosen directory, including the . (the current directory) and .. (one levelup) directories

Pagina 320 - Private Key Encryption

Creating and Deleting DirectoriesIn addition to passively reading information about directories, you can use the PHP functionsmkdir() and rmdir() to c

Pagina 321 - Public Key Encryption

echo “<a href=\”filedetails.php?file=”.$file.”\”>”.$file.”</a><br>”;}We can then create the script filedetails.php to provide furthe

Pagina 322 - Digital Signatures

?></body></html>The results of one sample run of Listing 16.4 are shown in Figure 16.4.Interacting with the File System and the ServerC

Pagina 323 - Digital Certificates

You might want to consider adopting a coding standard for field names so that all field namesthroughout your site use the same format. This makes it e

Pagina 324 - Secure Web Servers

The fileperms() function returns the permissions on the file. We have reformatted them as anoctal number using the decoct() function to put them into

Pagina 325 - Auditing and Logging

Creating, Deleting, and Moving FilesYou can use the file system functions to create, move, and delete files.First, and most simply, you can create a f

Pagina 326 - Firewalls

There are four techniques you can use to execute a command on the Web server. They are allpretty similar, but there are some minor differences.1. exec

Pagina 327 - Backing Up Data

LISTING 16.5 progex.php—File Status Functions and Their Results<?echo “<pre>”;// exec versionexec(“ls -la”, $result);foreach ($result as $lin

Pagina 328 - Physical Security

Note that the environment we are talking about here is the environment in which PHP runs onthe server.You can get a list of all PHP’s environment vari

Pagina 329 - Implementing Authentication

CHAPTER17Using Network and ProtocolFunctions22 7842 CH17 3/6/01 3:39 PM Page 369

Pagina 330 - Identifying Visitors

Advanced PHP TechniquesPART IV370In this chapter, we’ll look at the network-oriented functions in PHP that enable your scripts tointeract with the res

Pagina 331 - Implementing Access Control

Sending and Reading EmailThe main way to send mail in PHP is to use the simple mail() function. We discussed the useof this function in Chapter 4, “St

Pagina 332

LISTING 17.1 lookup.php—Script Retrieves a Stock Quote from the NASDAQ for theStock with the Ticker Symbol Listed in $symbol<html><head>&l

Pagina 333

FIGURE 17.1The script uses a regular expression to pull out the stock quote from information retrieved from NASDAQ.The script itself is pretty straigh

Pagina 334 - Storing Passwords

PART V Building Practical PHP and MySQL Projects22 Using PHP and MySQL for Large Projects 45923 Debugging 47724 Building User Authentication and Perso

Pagina 335 - LISTING 14.2 Continued

FIGURE 1.2Text passed to PHP’s echo construct is echoed to the browser.None of the raw PHP is visible. This is because the PHP interpreter has run thr

Pagina 336 - Encrypting Passwords

You can use this approach for a variety of purposes. Another good example is retrieving localweather information and embedding it in your page.The bes

Pagina 337 - UTHENTICATION

<form method=post action=”directory_submit.php”>URL: <input type=text name=”url” size=30 value=”http://”><br>Email contact: <inpu

Pagina 338 - Basic Authentication

The script that performs these checks uses two functions from the PHP network functionssuite—gethostbyname() and getmxrr(). The full script is shown i

Pagina 339

?></body></html>Lets’ go through the interesting parts of this script.First, we take the URL and apply the parse_url() function to it.

Pagina 340

If the URL is valid, we then go on to check the email address. First, we split it into usernameand hostname with a call to explode():$email = explode(

Pagina 341 - IGURE 14.4

Using FTP to Back Up or Mirror a FileThe FTP functions are useful for moving and copying files from and to other hosts. One com-mon use you might make

Pagina 342

echo “Checking file time...<br>”;if (file_exists($localfile)){$localtime = filemtime($localfile);echo “Local file last updated “;echo date(“G:i

Pagina 343

?></body></html>The output from running this script on one occasion is shown in Figure 17.4.Using Network and Protocol FunctionsCHAPTER

Pagina 344

The basic steps we follow in this script are the same as if you wanted to manually FTP the filefrom a command line interface:1. Connect to the remote

Pagina 345

The function takes three parameters: an FTP connection (obtained from ftp_connect()), ausername, and a password. It will return true if the user can b

Pagina 346

Different tag styles are available. This is the short style. If you have some problems runningthis script, it might be because short tags are not enab

Pagina 347 - IGURE 14.7

useful result from the function. In this case, we choose to artificially set the $remotetime vari-able to be “newer” than the $localtime variable by a

Pagina 348 - Installing mod_auth_mysql

There are two modes for an FTP transfer, ASCII and binary. The ASCII mode is used for trans-ferring text files (that is, files that consist solely of

Pagina 349 - Using mod_auth_mysql

Avoiding TimeoutsOne problem you might face when FTPing files is exceeding the maximum execution time.You will know whether this happens because PHP w

Pagina 350

In terms of other FTP functions, almost anything that you can do from an FTP command line,you can do with the FTP functions. You can find the specific

Pagina 351

The only things that change with the application are the URL that you connect to and the para-meters you set with curl_opt(). There are a large number

Pagina 352

The curl_setopt() function takes three parameters. The first is the session handle, the secondis the name of the parameter to set, and the third is th

Pagina 353 - Transactions with PHP and

The cURL Web site has some tips on how to use the command line versions of the cURL func-tions, and these are fairly easily translated into the PHP ve

Pagina 354 - Providing Secure Transactions

CHAPTER18Managing the Date and Time23 7842 CH18 3/6/01 3:43 PM Page 391

Pagina 355 - The User’s Machine

Advanced PHP TechniquesPART IV392In this chapter, we’ll discuss checking and formatting the date and time and convertingbetween date formats. This is

Pagina 356 - The Internet

D Day of the week in 3-character abbreviated text format. Range is from“Mon” to “Sun”.F Month of the year in full text format. Range is from “January”

Pagina 357 - Your System

As you have probably guessed, using the echo construct has a very simple result; it prints (orechoes) the string passed to it to the browser. In Figur

Pagina 358

w Day of the week as a single digit. Range is from “0” (Sunday) to “6”(Saturday).y Year in 2-digit format, for example, “00”.Y Year in 4-digit format,

Pagina 359 - IGURE 15.3

you can pass in 0s to the hour, minute, and second parameters. You can, however, leave outvalues from the right side of the parameter list. If you lea

Pagina 360

Validating DatesYou can use the checkdate() function to check whether a date is valid. This is especially use-ful for checking user input dates. The c

Pagina 361 - IGURE 15.4

The format code %m represents the month as a 2-digit number; %d, the day as a 2-digit number;and %Y, the year as a 4-digit number. A summary of the mo

Pagina 362 - Providing Secure Storage

will return the date formatted as a UNIX time stamp. You can then do as you will with it in PHP.As a rule of thumb, use a UNIX timestamp for date calc

Pagina 363

Now, the slightly tricky part—to convert this time period back to a more human-friendly unitof measure. This is not a time stamp but instead the age o

Pagina 364 - Using Encryption in PHP

Further ReadingIf you’d like to read more about date and time functions in PHP and MySQL, you can consultthe relevant sections of the manuals athttp:/

Pagina 365 - Installing GPG

CHAPTER19Generating Images24 7842 CH19 3/6/01 3:42 PM Page 401

Pagina 366

Advanced PHP TechniquesPART IV402One of the useful things you can do with PHP is create images on-the-fly. PHP has some built-in image information fun

Pagina 367

Image FormatsThe GD library supports JPEG, PNG, and WBMP formats. It no longer supports the GIF for-mat. Let’s briefly look at each of these formats.J

Pagina 368 - Testing GPG

way they did, when it was last modified, and so on. You will generally find comments in allbut the simplest PHP scripts.The PHP interpreter will ignor

Pagina 369

GIFGIF stands for Graphics Interchange Format. It is a compressed lossless format widely used onthe Web for storing images containing text, straight l

Pagina 370 - LISTING 15.2 Continued

3. Outputting the final graphic4. Cleaning up resourcesWe’ll begin by looking at a very simple image creation script. This script is shown in Listing

Pagina 371

FIGURE 19.1The script draws a black background and then adds a line and a text label for the image.An alternative way is to read in an existing image

Pagina 372

The function returns a color identifier that we can use to access the color later on.Second, to actually draw into the image, a number of different fu

Pagina 373

It takes as parameters the image identifier, the font, the x and y coordinates to start writing thetext, the text to write, and the color.The font is

Pagina 374

This tells the browser how to interpret the data that follows.In this case, we want to tell the browser that we are sending an image instead of the us

Pagina 375 - Advanced PHP Techniques

Cleaning UpWhen you’re done with an image, you should return the resources you have been using to theserver by destroying the image identifier. You ca

Pagina 376

FIGURE 19.3The dynamically produced inline image appears the same as a regular image to the end user.We will also use TrueType fonts so that we can us

Pagina 377 - System and the Server

FIGURE 19.4The front end lets a user choose the button color and type in the required text.Advanced PHP TechniquesPART IV412FIGURE 19.5A button genera

Pagina 378 - Introduction to File Upload

if (empty($button_text) || empty($color)){echo “Could not create image - form not filled out correctly”;exit;}// create an image of the right backgrou

Pagina 379 - HTML for File Upload

FIGURE 1.3PHP’s date() function returns a formatted date string.Calling FunctionsLook at the call to date(). This is the general form that function ca

Pagina 380

{// We have found a font size that will fit// Now work out where to put it$text_x = $width_image/2.0 - $width_text/2.0;$text_y = $height_image/2.0 - $

Pagina 381 - LISTING 16.2 Continued

The function ImageCreateFromPNG() takes the filename of a PNG as a parameter, and returnsa new image identifier for an image containing a copy of that

Pagina 382

// find out the size of the text at that font size$bbox=imagettfbbox ($font_size, 0, “arial.ttf”, $button_text);$right_text = $bbox[2]; // right co-

Pagina 383 - HAPTER 16

TABLE 19.1 Contents of the Bounding Box ArrayArray Index Contents0 X coordinate, lower-left corner1 Y coordinate, lower-left corner2 X coordinate, low

Pagina 384 - Using Directory Functions

After we have this, we test the loop condition:} while ( $font_size>8 &&( $height_text>$height_image_wo_margins ||$width_text>$width_

Pagina 385

Writing the Text onto the ButtonAfter that, it’s all smooth sailing. We set up the text color, which will be white:$white = ImageColorAllocate ($im, 2

Pagina 386

Graphing is the other thing these functions are primarily used for. You can chart any data youwant—sales, Web hits, or whatever takes your fancy.For t

Pagina 387 - Get File Info

<input type=radio name=vote value=”John Smith”>John Smith<br><input type=radio name=vote value=”Mary Jones”>Mary Jones<br><

Pagina 388

FIGURE 19.8Vote results are created by drawing a series of lines, rectangles, and text items onto a canvas.The new parts of this script relate to draw

Pagina 389

// get current results of poll, regardless of whether they voted$query = “select * from poll_results”;if(!($result = @mysql_query($query, $db_conn))){

Pagina 390 - Changing File Properties

Accessing Form VariablesThe whole point of using the order form is to collect the customer order. Getting the details ofwhat the customer typed in is

Pagina 391 - ILE SYSTEM

Part 2 sets up some variables that we will use to actually draw the graph.Working out the values for these sorts of variables can be tedious, but a bi

Pagina 392

$text_color = $black;$percent_color = $black;$bg_color = $white;$line_color = $black;$bar_color = $blue;$number_color = $pink;// Create “canvas” to dr

Pagina 393

to draw a black outline around the edge of the canvas. This function draws an outlined rectan-gle instead of a filled one. The parameters are the same

Pagina 394

ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);// draw title for this valueImageTTFText($im, $main_size, 0, $text_indent

Pagina 395 - Using Network and Protocol

This is a long-ish script, but can be easily adapted to suit your needs, or to auto-generate pollsvia an interface. One important feature that this sc

Pagina 396 - Overview of Protocols

CHAPTER20Using Session Control in PHP25 7842 CH20 3/6/01 3:42 PM Page 429

Pagina 397 - Using Other Web Services

Advanced PHP TechniquesPART IV430This chapter will discuss the session control functionality in PHP 4.We will cover• What session control is• Cookies•

Pagina 398

the server. (You can change this to use a database if you are willing to write your own function—more on this in the section “Configuring Session Cont

Pagina 399 - SING NETWORK

You can delete a cookie by calling setcookie() again with the same cookie name but novalue. If you set the cookie with other parameters (such as speci

Pagina 400

It is generally easier to compile with --enable-trans-sid, where possible. Note also that theSID constant will only work like this if you have configu

Pagina 401

There are two ways of accessing the form data via variables.In this example, and throughout this book, we have used the short style for referencing fo

Pagina 402

Note that you need to pass a string containing the name of the variable tosession_register(). This string should not include the $ symbol.This will re

Pagina 403

When you are finished with a session, you should first deregister all the variables and then callsession_destroy();to clean up the session ID.Simple S

Pagina 404 - Using FTP

Notice that we change the value after the variable has been registered. We could also do theopposite—set the value and then register the variable. The

Pagina 405

LISTING 20.3 page3.php—Ending the Session<?session_start();echo “The content of \$sess_var is $sess_var<br>”;session_destroy();?>As you ca

Pagina 406 - LISTING 17.4 Continued

session.cookie_path / Path to set in session cookie.session.name PHPSESSID The name of the session that is used asthe cookie name on a user’s system.s

Pagina 407

FIGURE 20.4Because the user has not yet logged in, show her a login page.This page gives the user a place to log in. If she attempts to access the Mem

Pagina 408 - Logging In to the FTP Server

FIGURE 20.6After the user has logged in, she can access the members’ areas.Let’s look at the code for this application. Most of the code is in authmai

Pagina 409 - Checking File Update Times

<body><h1>Home page</h1><?if (session_is_registered(“valid_user”)){echo “You are logged in as: $valid_user <br>”;echo “<

Pagina 410 - Downloading the File

The script’s activities revolve around the $valid_user session variable. The basic idea is that ifsomeone logs in successfully, we will register a ses

Pagina 411 - Uploading Files

Because we now know who she is, we don’t need to show her the login form again. Instead,we’ll tell her we know who she is, and give her the option to

Pagina 412 - Using Other FTP Functions

This is the string concatenation operator and is used to add strings (pieces of text) together.You will often use it when sending output to the browse

Pagina 413

else{echo “<p>You are not logged in.</p>”;echo “<p>Only logged in members may see this page.</p>”;}echo “<a href=\”authmain

Pagina 414

else{// if they weren’t logged in but came to this page somehowecho “You were not logged in, and so have not been logged out.<br>”;}?>The cod

Pagina 415

25 7842 CH20 3/6/01 3:42 PM Page 446

Pagina 416

CHAPTER21Other Useful Features26 7842 CH21 3/6/01 3:41 PM Page 447

Pagina 417 - Managing the Date and Time

Advanced PHP TechniquesPART IV448Some useful PHP functions and features do not fit into any particular category. This chapterwill explain these featur

Pagina 418 - Using the date() Function

PHP has a useful capability to automatically or magically add and strip slashes for you. Withtwo settings in your php.ini file, you can turn on or off

Pagina 419

Terminating Execution: die and exitSo far in this book we have used the language construct exit to stop execution of a script. Asyou probably recall,

Pagina 420 - Dealing with UNIX Time Stamps

However, you might still want to store a PHP array or object in a file or database. If you do,there are two functions you need to know how to use: ser

Pagina 421 - Using the getdate() Function

The get_loaded_extensions() function returns an array of all the function sets currentlyavailable to PHP. Given the name of a particular function set

Pagina 422 - Validating Dates

You can check the last modification date of a script with the getlastmod() (note the lack ofunderscores in the function name) function, as follows:ech

Pagina 423

User-Declared VariablesYou can declare and use your own variables in addition to the variables you are passed fromthe HTML form.One of the features of

Pagina 424 - Date Calculations

$max_execution_time = ini_get(“max_execution_time”);echo “new timeout is $max_execution_time <br>”;?>The ini_set() function takes two paramet

Pagina 425 - Using the Calendar Functions

highlight.default = #0000BBhighlight.html = #000000The colors are in standard HTML RGB format.NextPart V, “Building Practical PHP and

Pagina 426

26 7842 CH21 3/6/01 3:41 PM Page 456

Pagina 427 - Generating Images

IN THIS PART22 Using PHP and MySQL for Large Projects 45923 Debugging 47724 Building User Authentication and Personalization 49725 Building a Shopping

Pagina 428

27 7842 part 5 3/6/01 3:42 PM Page 458

Pagina 429 - Image Formats

CHAPTER22Using PHP and MySQL forLarge Projects28 7842 CH22 3/6/01 3:37 PM Page 459

Pagina 430 - Creating Images

In the earlier parts of this book, we’ve discussed various components of and uses for PHP andMySQL. Although we’ve tried to make all our examples inte

Pagina 431 - Creating a Canvas Image

increase the amount of dynamic content in Web sites to the level in which Web sites offer ser-vices rather than documents, this paradigm no longer fit

Pagina 432

• Build a prototype, based on all the previous information. Show it to users. Iterate.• Remember that, in all of this, it is important and useful to s

Pagina 433

If you end up developing your own functions or components, you should seriously considermaking them available to the PHP community when you have finis

Pagina 434 - Outputting the Final Graphic

Type StrengthPHP is a very weakly typed language. In most programming languages, variables can onlyhold one type of data, and that type must be declar

Pagina 435

It’s also a good idea to distinguish between variables and constants with case—a commonscheme is to use all lowercase for variables (for example, $res

Pagina 436 - Cleaning Up

Commenting Your CodeAll programs should be commented to a sensible level. You might ask what level of comment-ing is sensible. Generally you should co

Pagina 437

type, they consume a lot of screen space on many people’s monitors. We use an indent level oftwo to three spaces for all projects.The way you lay out

Pagina 438 - Listing 19.2

Even if all team members will be working on all pieces of the code, it’s generally a good ideato assign primary responsibility for each component to a

Pagina 439 - LISTING 19.2 Continued

You can solve all these problems with a version control system.These systems can track changes to each file in the repository so that you can see not

Pagina 440 - Setting Up the Base Canvas

Choosing a Development EnvironmentTalking about version control brings up the more general topic of development environments.All you really need are a

Pagina 441

Documenting Your ProjectsYou can produce many different kinds of documentation for your programming projects,including, but not limited to the followi

Pagina 442

PrototypingPrototyping is a development lifecycle commonly used for developing Web applications. Aprototype is a useful tool for working out customer

Pagina 443

presentation from content can be extended to scripting. In general, sites will be easier to useand maintain in the long run if you can separate logic

Pagina 444 - Positioning the Text

• Speed up database queries. Reduce the number of queries that you make, and make surethat they are optimized. With a complex (and therefore slow) que

Pagina 445 - Finishing Up

ContentsIntroduction 1Who Should Read This Book? ...1What Is PHP? ...

Pagina 446

(As you can see, PHP allows a lot of freedom in this area—all languages will let you changethe value of a variable, but not many will allow you to cha

Pagina 447

TestingReviewing and testing code is another basic point of software engineering that is often over-looked in Web development. It’s easy enough to try

Pagina 448 - FIGURE 19.8

site for a client company, they can often supply a good set of naive users by getting staff attheir company to work through the site. (This has the in

Pagina 449

28 7842 CH22 3/6/01 3:37 PM Page 476

Pagina 450

CHAPTER23Debugging29 7842 CH23 3/6/01 3:41 PM Page 477

Pagina 451

This chapter will deal with debugging PHP scripts. If you have been through some of theexamples in the book or used PHP before, you will probably have

Pagina 452

If a script does not follow the rules of PHP’s syntax—if it contains syntax errors—the PHPparser will not be able to process some or all of it. Peopl

Pagina 453

These errors can be hard to find if they result from a combination of multiple files. They canalso be difficult to find if they occur in a large file.

Pagina 454 - Other Image Functions

Common causes of runtime errors include the following:• calls to functions that do not exist• reading or writing files• interaction with MySQL or othe

Pagina 455 - Using Session Control in PHP

but except in the, possibly rare, case in which the variable $var has the value 4, the call tostrstr() will not occur, and no warning will be issued.C

Pagina 456 - Basic Session Functionality

If an error occurs, you can access the text of the error message using the functionmysql_error(),or an error code using the function mysql_errno(). If

Pagina 457 - Setting Cookies from PHP

One important difference between constants and variables is that when you refer to a constant,it does not have a dollar sign in front of it. If you wa

Pagina 458 - Storing the Session ID

empty table or searching for data that does not exist. Assuming that you have connected to adatabase successfully, and have a table called exists and

Pagina 459 - Implementing Simple Sessions

This does not mean that you need to attempt to simulate every different error that might occur.MySQL for example can provide one of around 200 differe

Pagina 460 - Using Session Variables

Logic errors are not caused by any sort of failure of the code, but merely a failure of the pro-grammer to write code that instructs the computer to d

Pagina 461 - Simple Session Example

LISTING 23.1 dump_variables.php—This Code Can Be Included in Pages to Dump theContents of Variables for Debugging<?// these lines format the output

Pagina 462

$string .= “ }”;}return $string;}else{// if it is not an array, just return itreturn $array;}}?>This code will iterate through four arrays of varia

Pagina 463 - Configuring Session Control

the user’s name in—valid_user. As discussed in Chapter 20, PHP uses a cookie to link ses-sion variables to particular users. Our script is echoing the

Pagina 464

E_ALL itself is effectively a combination of all the other error types. It could be replaced by theother levels ORed together using the bitwise or ope

Pagina 465

Turning track_errors on might help you to deal with errors in your own code, rather than let-ting PHP provide its default functionality. Although PHP

Pagina 466 - FIGURE 20.6

Triggering Your Own ErrorsThe function trigger_error()can be used to trigger your own errors. Errors created in thisway will be handled in the same wa

Pagina 467

Logical actions might include• Displaying the error message provided• Storing information in a log file• Emailing the error to an address• Terminating

Pagina 468

Arithmetic OperatorsArithmetic operators are very straightforward—they are just the normal mathematical opera-tors. The arithmetic operators are shown

Pagina 469

FIGURE 23.1You can give friendlier error messages than PHP if you use your own error handler.This custom error handler does not do any more than the d

Pagina 470

If you are still using PHP 3 and have a very troublesome bug that you cannot track down anyother way, you might want to look up the PHP configuration

Pagina 471

29 7842 CH23 3/6/01 3:41 PM Page 496

Pagina 472

CHAPTER24Building User Authenticationand Personalization30 7842 ch24 3/6/01 3:34 PM Page 497

Pagina 473 - Other Useful Features

In this project, we’ll get users to register at our Web site. When they’ve done that, we’ll be ableto keep track of what they’re interested in and sho

Pagina 474 - Using Magic Quotes

Third, we need to be able to recommend to a user sites that might appeal to her, based on whatwe know about her already.Solution ComponentsNow that we

Pagina 475 - Evaluating Strings: eval()

Storing BookmarksTo store a user’s bookmarks, we will need to set up some space in our MySQL database. Wewill need the following functionality:• User

Pagina 476 - Serialization

We’ll build a module for each box on this diagram—some will need one script and others,two. We’ll also set up function libraries for• User authenticat

Pagina 477

db_fns.php Functions to connect to the databaseuser_auth_fns.php Functions for user authenticationurl_fns.php Functions for adding and deleting bookma

Pagina 478 - Identifying the Script Owner

The SQL to create this database, and to create a user for connecting to the database from theWeb, is shown in Listing 24.1. You should edit it if you

Pagina 479

String OperatorsWe’ve already seen and used the only string operator. You can use the string concatenationoperator to add two strings and to generate

Pagina 480 - Source Highlighting

You will then be prompted to type in your password.With the database set up, let’s go on and implement the basic site.Implementing the Basic SiteThe f

Pagina 481

FIGURE 24.3The front page of the PHPBookmark system is produced by the HTML rendering functions in login.php.As you can see, this file is just a conta

Pagina 482

LISTING 24.4 do_html_header() Function from output_fns.php—This Function Outputsthe Standard Header That Will Appear on Each Page in the Applicationfu

Pagina 483 - MySQL Projects

RegisteringTo register a user, we need to get his details via a form and enter him in the database.When a user clicks on the “Not a member?” link on t

Pagina 484

The gray form on this page is output by the function display_registration_form(),contained in output_fns.php. When the user clicks on the Register but

Pagina 485 - Large Projects

}// check password length is ok// ok if username truncates, but passwords will get// munged if they are too long.if (strlen($passwd)<6 || strlen($p

Pagina 486 - Development

This is the first script with any complexity to it that we have looked at in this application.The script begins by including the application’s functio

Pagina 487 - YSQL FOR

LISTING 24.8 valid_email() Function from data_valid_fns.php—This Function ChecksWhether an Email Address Is Validfunction valid_email($address){// che

Pagina 488 - Reusing Code

FIGURE 24.5Registration was successful—the user can now go to the members page.The register() function is in the included library called user_auth_fns

Pagina 489 - Writing Maintainable Code

$result = mysql_query(“insert into user values(‘$username’, password(‘$password’), ‘$email’)”);if (!$result)return “Could not register you in databas

Pagina 490

Combination Assignment OperatorsIn addition to the simple assignment, there is a set of combined assignment operators. Each ofthese is a shorthand way

Pagina 491 - Indenting

LISTING 24.11 member.php—This Script is the Main Hub of the Application<?// include function files for this applicationrequire_once(“bookmark_fns.p

Pagina 492 - Breaking Up Code

First, we check whether the user has come from the front page—that is, whether he has justfilled in the login form—and try to log them in as follows:i

Pagina 493 - Implementing Version Control

FIGURE 24.6The member.php script checks that a user is logged in; retrieves and displays his bookmarks; and gives hima menu of options.We will now loo

Pagina 494

return 0;if (mysql_num_rows($result)>0)return 1;elsereturn 0;}As you can see, this function connects to the database and checks that there is a use

Pagina 495

Logging OutYou might have noticed that there is a link marked “Logout” on the menu in Figure 24.6. Thisis a link to the logout.php script. The code fo

Pagina 496 - Documenting Your Projects

Changing PasswordsIf a user follows the ”Change Password” menu option, he will be presented with the formshown in Figure 24.7.Building User Authentica

Pagina 497 - Separating Logic and Content

display_user_menu();do_html_footer();exit;}else{if ($new_passwd!=$new_passwd2)echo “Passwords entered were not the same. Not changed.”;else if (strle

Pagina 498 - Optimizing Code

// return true or false{// if the old password is right// change their password to new_password and return true// else return falseif (login($username

Pagina 499 - Using Zend Products

FIGURE 24.8The forgot_form.php script supplies a form in which users can ask to have their passwords reset and sent tothem.LISTING 24.17 forgot_passwd

Pagina 500

As you can see, this script uses two main functions to do its job: reset_password() andnotify_password(). Let’s look at each of these in turn.The rese

Pagina 501

However, if the ++ is after the $a, we are using the post-increment operator. This has a differ-ent effect. Consider the following:$a=4;echo $a++;In t

Pagina 502

LISTING 24.19 The get_random_word() Function from user_auth_fns.php—This FunctionGets a Random Word from the Dictionary for Use in Generating Password

Pagina 503 - Debugging

The function has two clever bits. The first is that, if we reach the end of the file while lookingfor a word, we go back to the beginning:if (feof($fp

Pagina 504 - Programming Errors

It would be more secure to give users a truly random password—made from any combinationof upper and lowercase letters, numbers, and punctuation—rather

Pagina 505

LISTING 24.21 add_bms.php—This Script Adds New Bookmarks to a User’s Personal Page<?require_once(“bookmark_fns.php”);session_start();do_html_header

Pagina 506 - Runtime Errors

Again this script follows the pattern of validation, database entry, and output.To validate, we first check whether the user has filled out the form u

Pagina 507

This function is fairly simple. It checks that a user does not already have this bookmark listedin the database. (Although it is unlikely that they wo

Pagina 508 - Reading or Writing Files

The array from get_user_urls() can be passed to display_user_urls(). This is again a sim-ple HTML output function to print the user’s URLs in a nice t

Pagina 509

elseecho “Could not delete “.htmlspecialchars($url).”.<br>”;}}elseecho “No bookmarks selected for deletion”;}// get the bookmarks this user has

Pagina 510

As you can see, this is again a pretty simple function. It attempts to delete the bookmark for aparticular user from the database. One thing to note i

Pagina 511 - Logic Errors

However, as you might recall, MySQL does not support subqueries. We will have to performtwo different queries and feed the output of the first into th

Pagina 512 - Variable Debugging Aid

It is easy to confuse this with =, the assignment operator. This will work without giving anerror, but generally will not give you the result you want

Pagina 513 - DEBUGGING

The full script for making recommendations is shown in Listing 24.26 and 24.27. The mainscript for making recommendations is called recommend.php (see

Pagina 514

// create set of users with urls in common// for use in IN clause$row = mysql_fetch_object($result);$sim_users = “(‘“.($row->username).”’”;while ($

Pagina 515 - Error Reporting Levels

if (!($num_urls=mysql_num_rows($result)))return false;$urls = array();// build an array of the relevant urlsfor ($count=0; $row = mysql_fetch_object($

Pagina 516

Wrapping Up and Possible ExtensionsThat’s the basic functionality of the PHPBookmark application. There are many possibleextensions. You might conside

Pagina 517

30 7842 ch24 3/6/01 3:34 PM Page 538

Pagina 518 - Handling Errors Gracefully

CHAPTER25Building a Shopping Cart31 7842 CH25 3/6/01 3:38 PM Page 539

Pagina 519

Building Practical PHP and MySQL ProjectsPART V540In this chapter, you will learn how to build a basic shopping cart. We will add this on top of theBo

Pagina 520 - Remote Debugging

We’ll also need to add some information to our existing database about shipping addresses,payment details, and so on.We already know how to build an i

Pagina 521

We can work out the total of an order from a user’s shopping cart session variable. We willrecord the final order details in the database, and get rid

Pagina 522

FIGURE 25.2The administrator view of the Book-O-Rama system allows insertion, editing, and deletion of books and categories.In Figure 25.1, we show th

Pagina 523 - Building User Authentication

PHP supports logical AND, OR, XOR (exclusive or), and NOT.The set of logical operators and their use is summarized in Table 1.4.TABLE 1.4 PHP’s Logica

Pagina 524 - The Problem

As in the last project, we will also build and use a set of function libraries. For this project, wewill use a function API similar to the one in the

Pagina 525 - Solution Components

insert_category.php Administration Inserts new category into database.insert_book_form.php Administration Form to let administrator add a newbook to s

Pagina 526 - Solution Overview

Implementing the DatabaseAs we mentioned earlier, we have made some minor modifications to the Book-O-Rama data-base presented in Part II.The SQL to c

Pagina 527 - ERSONALIZATION

orderid int unsigned not null auto_increment primary key,customerid int unsigned not null,amount float(6,2),date date not null,order_status char(10),s

Pagina 528 - Implementing the Database

grant select, insert, update, deleteon book_sc.*to book_sc@localhost identified by ‘password’;Although nothing was wrong with the original Book-O-Rama

Pagina 529 - IGURE 24.2

The front page of the site is produced by the script called index.php. The output of this scriptis shown in Figure 25.3.Building a Shopping CartCHAPTE

Pagina 530 - Implementing the Basic Site

FIGURE 25.4Each book in the category is listed with a photo.Building Practical PHP and MySQL ProjectsPART V550FIGURE 25.5Each book has a details page

Pagina 531 - IGURE 24.3

Listing CategoriesThe first script, index.php, lists all the categories in the database. It is shown in Listing 25.2.LISTING 25.2 index.php—Script to

Pagina 532

The functions get_categories() and display_categories() are in the function librariesbook_fns.php and output_fns.php, respectively. The function get_c

Pagina 533 - Registering

In our case, we will return this array back all the way to index.php, where we pass it to thedisplay_categories() function from output_fns.php. This f

Pagina 534

Other OperatorsIn addition to the operators we have covered so far, there are a number of others.The comma operator, , ,is used to separate function a

Pagina 535 - LISTING 24.6 Continued

$name = get_category_name($catid);do_html_header($name);// get the book info out from db$book_array = get_books($catid);display_books($book_array);//

Pagina 536

$result = @mysql_query($query);if (!$result)return false;$num_cats = @mysql_num_rows($result);if ($num_cats ==0)return false;$result = mysql_result($r

Pagina 537

// set url for “continue button”$target = “index.php”;if($book[“catid”]){$target = “show_cat.php?catid=”.$book[“catid”];}// if logged in as admin, sho

Pagina 538 - FIGURE 24.5

Implementing the Shopping CartThe shopping cart functionality all revolves around a session variable called $cart. This is anassociative array that ha

Pagina 539 - Logging In

In this case, we have clicked the View Cart link when our cart is empty; that is, we have notyet selected any items to purchase.Figure 25.7 shows our

Pagina 540 - Chapter 20

LISTING 25.9 show_cart.php—This Script Controls the Shopping Cart<?include (‘book_sc_fns.php’);// The shopping cart needs sessions, so start oneses

Pagina 541

echo “<p>There are no items in your cart”;echo “<hr>”;}$target = “index.php”;// if we have just added an item to the cart,// continue shop

Pagina 542 - FIGURE 24.6

output_fns.php, which is included here as Listing 25.10. Although it is a display function, itis reasonably complex, so we include it here.LISTING 25.

Pagina 543

echo “</td><td align = center>$”.number_format($book[“price”], 2);echo “</td><td align = center>”;// if we allow changes, quan

Pagina 544 - Logging Out

2. Provide an image for each book, if one exists. We use the HTML image height and widthtags to resize the image a little smaller here. This means tha

Pagina 545 - Changing Passwords

For example, under UNIX-like operating systems, you can use$out = `ls -la`;echo “<pre>”.$out.”</pre>”;or, equivalently on a Windows server

Pagina 546

Third, we need to work out the total price and number of items in the cart. For this, we use thecalculate_price() and calculate_items() functions, as

Pagina 547 - Resetting Forgotten Passwords

if(is_array($cart)){foreach($cart as $isbn => $qty){$items += $qty;}}return $items;}The calculate_items() function is simpler—it just goes through

Pagina 548 - FIGURE 24.8

(We covered variable variables in Chapter 1, “PHP Crash Course.”) As a reminder, when werefer to $$isbn, we are actually referring to the variable who

Pagina 549

FIGURE 25.8The checkout.php script gets the customer’s details.LISTING 25.13 checkout.php—This Script Gets the Customer Details<?//include our func

Pagina 550

display_button(“show_cart.php”, “continue-shopping”, “Continue Shopping”);do_html_footer();?>There are no great surprises in this script. If the ca

Pagina 551

session_start();do_html_header(“Checkout”);// if filled outif($from==’process’||$cart&&$name&&$address&&$city&&$zip&am

Pagina 552 - Adding Bookmarks

The logic here is straightforward: We check that the user filled out the form and inserteddetails into the database using a function called insert_ord

Pagina 553

if (!$result)return false;}$query = “select customerid from customers wherename = ‘$name’ and address = ‘$address’and city = ‘$city’ and state = ‘$sta

Pagina 554 - Bookmarks to the Database

$query = “delete from order_items whereorderid = $orderid and isbn = ‘$isbn’”;$result = mysql_query($query);$query = “insert into order_items values(

Pagina 555 - Displaying Bookmarks

FIGURE 25.10This transaction was successful, and the items will now be shipped.The code for process.php can be found in Listing 25.16.LISTING 25.16 pr

Pagina 556 - Deleting Bookmarks

PHP AND MYSQL WEB DEVELOPMENTviAccessing Form Variables ...19Form Variables ...

Pagina 557 - Bookmark from a User’s List

FIGURE 1.5The totals of the customer’s order have been calculated, formatted, and displayed.The total amount seems to be correct, but why were the mul

Pagina 558 - Implementing Recommendations

//empty shopping cartsession_destroy();echo “Thankyou for shopping with us. Your order has been placed.”;display_button(“index.php”, “continue-shoppi

Pagina 559

As with other places where we directly refer to $HTTP_POST_VARS, you need to havetrack_vars enabled for this to work. We process the user’s card, and,

Pagina 560 - Actual Recommendations

FIGURE 25.11Users must pass through the login page to access the admin functions.Building Practical PHP and MySQL ProjectsPART V576FIGURE 25.12The adm

Pagina 561 - LISTING 24.27 Continued

The code for the admin menu is shown in Listing 25.17.LISTING 25.17 admin.php—This Script Authenticates the Administrator and Lets HerAccess the admin

Pagina 562 - FIGURE 24.11

We identify the administration user after login by means of the $admin_user session variableand the check_admin_user() function. This function and the

Pagina 563

LISTING 25.18 insert_book.php—This Script Validates the New Book Data and Puts Itinto the Database<?// include function files for this applicationr

Pagina 564

FIGURE 25.14The show_book.php script produces different output for an administrative user.The administrator has access to two new options on this page

Pagina 565 - Building a Shopping Cart

FIGURE 25.15The edit_book_form.php script gives the administrator access to edit book details or delete a book.This is, in fact, the same form we used

Pagina 566

// most of the form is in plain HTML with some// optional PHP bits throughout?><form method=postaction=”<?=$edit?”edit_book.php”:”insert_book

Pagina 567

<td>Price:</td><td><input type=text name=pricevalue=”<?=$edit?$book[“price”]:””; ?>”></td></tr><tr><

Pagina 568

left xorleft andright printleft = += -= *= /= .= %= &= |= ^= ~= <<= >>=left ? :left ||left &&left |left ^left &n/a == != =

Pagina 569

If we pass in an array containing the book data, the form will be rendered in edit mode andwill fill in the fields with the existing data:<input ty

Pagina 570

This has a lot of advanced features such as customer tracking, timed sales, multiple languages,credit card processing, and support for multiple online

Pagina 571

31 7842 CH25 3/6/01 3:39 PM Page 586

Pagina 572

CHAPTER26Building a ContentManagement System32 7842 ch26 3/6/01 3:36 PM Page 587

Pagina 573 - LISTING 25.1 Continued

In this chapter, we’ll look at a content management system for storing, indexing, and searchingtext and multimedia content.Content management systems

Pagina 574

Editing ContentFirst, we need to think about how we will get content into the system, and how we will storeand edit that content.Getting Content into

Pagina 575

Databases Versus File StorageAn important decision to make at an early stage is how the content will be stored after it hasbeen uploaded into the syst

Pagina 576 - FIGURE 25.5

Document StructureThe example stories we will be using are short one- or two-paragraph news stories with a sin-gle image, designed for people in a hur

Pagina 577 - Listing Categories

We can then develop a search engine algorithm that ranks matches according to this human-specified relevance for stories, rather than a complex algori

Pagina 578

Implementing a templated structure such as this from a page design is very simple. In theHTML source, you will find the <TD> where the main cont

Pagina 579 - Listing Books in a Category

You can use as many sets of parentheses as you like in an expression. The innermost set ofparentheses will be evaluated first.Variable FunctionsBefore

Pagina 580

$height = $size[1];$x_ratio = $max_width / $width;$y_ratio = $max_height / $height;if ( ($width <= $max_width) && ($height <= $max_heigh

Pagina 581 - Showing Book Details

The key to the resize operation is the calculation of the new width and height parameters. Wefind the ratio between the actual and maximum dimensions.

Pagina 582

logo.gif Image The logo file displayed in header.phpheadlines.php Application Shows the most recent headline from eachpage of the sitepage.php Applica

Pagina 583

Designing the DatabaseListing 26.2 shows the SQL queries used to create the database for the content system. Thislisting is part of the file create_da

Pagina 584 - IGURE 25.7

create table writer_permissions (writer varchar(16) not null, # foreign key writers.usernamepage varchar(16) not null

Pagina 585

LISTING 26.3 headlines.php Shows the Most Recent Headline from Each Page<?include (“include_fns.php”);include (“header.php”);$conn = db_connect();$

Pagina 586 - Viewing the Cart

The hard work is done by two database queries. First,select * from pages order by codewill retrieve the list of pages that are in the database. Next t

Pagina 587

LISTING 26.4 page.php Displays All the Published Stories on a Page<?include (“include_fns.php”);include (“header.php”);$conn = db_connect();if (!$p

Pagina 588 - LISTING 25.10 Continued

will send the visitor back to the headline page so that the omission of $page will not cause anerror.The first queryselect * from storieswhere page =

Pagina 589 - Adding Items to the Cart

Building a Content Management SystemCHAPTER 2626CONTENTMANAGEMENTSYSTEMS603FIGURE 26.5The story management page for writers.These screens are not form

Pagina 590

Testing Variable StatusPHP has several functions for testing the status of a variable.The first of these is isset(), which has the following prototype

Pagina 591 - Saving the Updated Cart

</TR></TABLE><INPUT TYPE=SUBMIT VALUE=”Log in”></FORM><?}else {$conn = db_connect();$w = get_writer_record($auth_user);echo

Pagina 592 - Checking Out

echo “[<A HREF=\”story.php?story=”.$qry[id].”\”>edit</A>] “;echo “[<A HREF=\”delete_story.php?story=”.$qry[id].”\”>delete</A>]

Pagina 593 - FIGURE 25.8

All this information is shown on the stories screen, first withecho date(“M d, H:i”, $qry[created]);and thenecho date(“M d, H:i”, $qry[modified]);and

Pagina 594 - Gets the Payment Details

The complete listing of story.php can be seen in listing 26.6.LISTING 26.6 story.php Is Used to Create or Edit a Story<?include (“include_fns.php”)

Pagina 595 - LISTING 25.14 Continued

</TR><TR><TD ALIGN=CENTER><INPUT TYPE=FILE NAME=”html” SIZE=40></TD></TR><TR><TD ALIGN=CENTER>Picture&

Pagina 596

If $story is not set, the preceding code will produce no value from the PHP statement, so theheadline input box will be blank. If $story is set, it wi

Pagina 597 - LISTING 25.15 Continued

set headline = ‘$headline’,story_text = ‘$story_text’, page = ‘$page’,modified = $timewhere id = $story”;}else { // It’s a new story$sql = “in

Pagina 598 - Implementing Payment

The delete story link calls delete_story.php, which actions a simple DELETE statement andreturns the writer to the calling page. The code for delete_s

Pagina 599 - FIGURE 25.10

FIGURE 26.7Setting keywords for a story.The search form in search_form.php contains a single field for keywords, and submits tosearch.php, which queri

Pagina 600 - LISTING 25.16 Continued

}$and .= “and ($k_string) “;}$sql = “select s.id,s.headline,10 * sum(k.weight) / $num_keywords as scorefrom stories s, keywords kwhere s.id = k.story$

Pagina 601

Each of these accepts a variable as input and returns the variable’s value converted to theappropriate type.Control StructuresControl structures are t

Pagina 602 - FIGURE 25.12

else$k_string .= “k.keyword = ‘“.$k[$i].”’ “;}$and .= “and ($k_string) “;}This code uses the PHP function split() to create an array containing each w

Pagina 603

$conn = db_connect();$sql = “select * from stories order by modified desc”;$result = mysql_query($sql, $conn);echo “<H2>Editor admin</H2>”

Pagina 604 - IGURE 25.13

This will mark a story as published and authorize it for public viewing.Similarly, unpublish_story.php uses the following query to mark a story as unp

Pagina 605

CHAPTER27Building a Web-Based EmailService33 7842 CH27 3/6/01 3:41 PM Page 617

Pagina 606 - IGURE 25.14

Building Practical PHP and MySQL ProjectsPART V618More and more often these days, sites want to offer Web-based email to their users. This chap-ter ex

Pagina 607

Solution ComponentsPHP has excellent IMAP and POP3 support, but it is provided via the IMAP function library. Inorder to use the code presented in thi

Pagina 608 - LISTING 25.19 Continued

For a user to read his mail, we will need to get his server and account details. Rather than get-ting these details from the user every time, we’ll se

Pagina 609

We will also give him the option of viewing detailed headers for a particular message. Viewingthe complete headers can tell you a lot about a message.

Pagina 610 - Using an Existing System

Setting Up the DatabaseThe database for Warm Mail is fairly simple because we aren’t actually going to store any ofthe emails in it.We will need to st

Pagina 611

create table accounts(username char(16) not null,server char(100) not null,port int not null,type char(4) not null,remoteuser char(50) not null,remote

Pagina 612

echo “<font color=red>”;echo “You did not order anything on the previous page!<br>”;echo “</font>”;}The three lines of code enclosed

Pagina 613 - Management System

//*****************************************************************************// Stage 1: pre-processing// Do any required processing before page hea

Pagina 614 - Solution Requirements

store_account_settings($auth_user, $HTTP_POST_VARS);break;}case ‘select-account’ :{// if have chosen a valid account, store it as a session variableif

Pagina 615 - Editing Content

//display any text generated by functions called before headerecho $status;if(!check_auth_user()){echo “<P>You need to log in”;if($action&&a

Pagina 616 - Databases Versus File Storage

case ‘view-message’ :{// if we have just picked a message from the list, or were looking at// a message and chose to hide or view headers, load a mess

Pagina 617 - Using Metadata

display_new_message_form($auth_user, $to, $cc, $subject, $body);}break;}case ‘forward’ :{//set message as quoted body of current messageif(!$imap)$ima

Pagina 618 - Formatting the Output

The four main sections to the script are as follows:1. We do some processing that must take place before we send the page header to thebrowser, such a

Pagina 619 - Image Manipulation

This is the default behavior for the application. With no $action chosen yet, and no logindetails supplied, we will execute the following parts of the

Pagina 620

FIGURE 27.3After successful login, the user can begin using the application.On this execution of the script, we will activate different sections of co

Pagina 621 - Solution Design/Overview

In addition to setting up the buttons we saw while not logged in, we add another button toallow the user to log out again, as follows:if(check_auth_us

Pagina 622

Look back at the script in Listing 27.2. This time around because of the value of $action,weget different behavior.We get a slightly different header,

Pagina 623 - Designing the Database

if( $totalqty == 0){echo “You did not order anything on the previous page!<br>”;}else{if ( $tireqty>0 )echo $tireqty.” tires<br>”;if (

Pagina 624 - Implementation

function will display the form that we can see in Figure 27.4. You can see that we use it in twodifferent ways here: We use it with no parameters to d

Pagina 625 - ANAGEMENT

LISTING 27.5 store_account_settings() Function from mail_fns.php—Function to SaveNew Account Details for a Userfunction store_account_settings($auth_u

Pagina 626 - IGURE 26.3

As you can see, we then execute the display_account_setup() function as before to list theuser’s account details. The newly added account will now be

Pagina 627

After execution returns to index.php, the body stage will run the following code:case ‘store-settings’ :case ‘account-setup’ :case ‘delete-account’ :{

Pagina 628 - Back End

$query = “select count(*) from accounts where username = ‘$auth_user’”;if(db_connect()){$result = mysql_query($query);if($result)return mysql_result($

Pagina 629 - IGURE 26.5

This SELECT option is generated in the do_html_header() function from output_fns.php,asshown in the following code fragment:<?// include the accoun

Pagina 630 - LISTING 26.5 Continued

Choosing one of the options in the SELECT activates the select_account event. If you look atthe URL in Figure 27.5, you can see this appended to the e

Pagina 631

echo “No mailbox selected<br><br><br><br><br><br>.”;}else{$imap = open_mailbox($auth_user, $accountid);if($imap){e

Pagina 632 - IGURE 26.6

We open the mailbox for a user account with a call to the open_mailbox() function that wehave written in mail_fns.php. This function is shown in Listi

Pagina 633 - MANAGEMENT

If the protocol is not specified, it defaults to IMAP. In the code we have written, you cansee that we specify POP3 if the user has specified that pro

Pagina 634

switch StatementsThe switch statement works in a similar way to the if statement, but allows the condition totake more than two values. In an if state

Pagina 635

The messageid is the sequence number used in the headers we retrieved earlier. Note thatIMAP messages are numbered from 1, not 0.If the user clicks on

Pagina 636 - LISTING 26.7 Continued

The line$fullheaders = ($action==’show-headers’);could have been more verbosely—and perhaps more clearly—written asif($action==’show-headers’)$fullhea

Pagina 637 - Searching

$message[‘subject’] = $header->subject;$message[‘fromaddress’] = $header->fromaddress;$message[‘toaddress’] = $header->toaddress;$message

Pagina 638 - FIGURE 26.7

Finally we close the mailbox with imap_close() and return the array we have built. The display_message() function can then display the message’s field

Pagina 639

Deleting MailIf a user clicks the Delete button on a particular email, he will activate the “delete” action.This will execute the following code from

Pagina 640 - Editor Screen

Sending MailFinally we come to sending mail. There are a few ways to do this from this script: The usercan send a new message, reply to, or forward ma

Pagina 641

Clicking the Send Message button invokes the “send-message” action, which executes the fol-lowing code:case ‘send-message’ :{if(send_message($to, $cc,

Pagina 642

}}}As you can see, this function uses mail() to send the email. First, however, it loads the user’semail address out of the database to use in the Fro

Pagina 643 - Building a Web-Based Email

$header = imap_header($imap, $messageid);if($header->reply_toaddress)$to = $header->reply_toaddress;else$to = $header->fromaddress;$subject =

Pagina 644

you could allow them to use many addresses. You would need to change a limitedamount of other code too. The send mail form would need a drop-down box

Pagina 645

This HTML code has added a new form variable whose value will be “a”, “b”, “c”, or “d”. Wecould handle this new variable with a series of if and elsei

Pagina 646

33 7842 CH27 3/6/01 3:41 PM Page 654

Pagina 647 - EB-BASED

CHAPTER28Building a Mailing ListManager34 7842 CH28 3/6/01 3:46 PM Page 655

Pagina 648 - Setting Up the Database

After you’ve built up a base of subscribers to your Web site, it’s nice to be able to keep intouch with them by sending out a newsletter. In this chap

Pagina 649 - Script Architecture

Solution ComponentsThere are a number of components we will need to fulfil the requirements. The main ones aresetting up a database of lists, subscrib

Pagina 650 - LISTING 27.2 Continued

Sending Mail with AttachmentsFor this project, we would like to be able to send users either a plain text newsletter or a“fancy” HTML version, accordi

Pagina 651 - WEB-BASED

In Figure 28.1 you can see the actions that can be taken by a user who is not logged in. As youcan see, he can log in (if he already has an account),

Pagina 652

Because we have used an event-driven approach again, the backbone of the application is con-tained in one file, index.php, which calls on a set of fun

Pagina 653

• Sub_lists: A record of which users have subscribed to which lists (a many-to-many relationship)• Mail: A record of email messages that have been sen

Pagina 654

LISTING 28.1 Continuedsent datetime,modified timestamp);#stores the images that go with a particular mailcreate table images(mailid int not null,path

Pagina 655 - Logging In and Out

email (subject), and the listid of the list it has been sent to or will be sent to. The actual textor HTML of the message could be a large file, so we

Pagina 656

try to use whichever conditional will be most readable in your situation. You will acquire a feelfor this with experience.Iteration: Repeating Actions

Pagina 657 - IGURE 27.3

LISTING 28.2 Continued$status = ‘’;// need to process log in or out requests before anything elseif($email&&$password){$login = login($email,

Pagina 658 - Setting Up Accounts

LISTING 28.2 Continued$buttons[0] = ‘change-password’;$buttons[1] = ‘account-settings’;$buttons[2] = ‘show-my-lists’;$buttons[3] = ‘show-other-lists’;

Pagina 659

LISTING 28.2 Continued// only these actions can be done if not logged inswitch ( $action ){case ‘new-account’ :{unset($normal_user);unset($admin_user)

Pagina 660 - Creating a New Account

LISTING 28.2 Continued}//all other actions require user to be logged inif(check_logged_in()){switch ( $action ){case ‘account-settings’ :{display_acco

Pagina 661

LISTING 28.2 Continuedcase ‘store-change-password’ :{if(change_password(get_email(), $old_passwd,$new_passwd, $new_passwd2)){echo “<p>OK: Passwo

Pagina 662 - Deleting an Account

LISTING 28.2 Continuedcase ‘send’ :{send($id, $admin_user);break;}case ‘view-mail’ :{display_items(“Unsent Mail”, get_unsent_mail(get_email()),‘previe

Pagina 663 - Reading Mail

LISTING 28.3 Continuedif (session_is_registered(“normal_user”))return true;elsereturn false;}function check_admin_user()// see if somebody is logged i

Pagina 664

Action Usable By Descriptionshow-all-lists Anyone Shows a list of availablemailing listsshow-archive Anyone Displays archived news-letters for a parti

Pagina 665

One noticeable omission from this table is an option along the lines of store-mail, that is, anaction that actually uploads the newsletters entered vi

Pagina 666 - Viewing Mailbox Contents

Creating a New AccountIf a user selects the New Account menu option, this activates the new-account action. Thisactivates the following code in index.

Pagina 667

CONTENTSviiBreaking Out of a Control Structure or Script ...47Next: Saving the Customer’s Order ...

Pagina 668 - User Mailbox

</tr><tr><td align = right>100</td><td align = right>10</td></tr><tr><td align = right>150</t

Pagina 669 - Reading a Mail Message

The submit button on this form invokes the store-account action. The code for this action isas follows:case ‘store-account’ :{if (store_account($norma

Pagina 670 - IGURE 27.6

LISTING 28.3 Continuedelse{echo “<p>Sorry, that email address is already registered here.”;echo “<p>You will need to log in with that addr

Pagina 671

LISTING 28.3 Continued// need to process log in or out requests before anything elseif($email&&$password){$login = login($email, $password);if

Pagina 672

LISTING 28.3 Continued//echo $query;$result = mysql_query($query);if (!$result)return false;if (mysql_num_rows($result)<1)return false;if(mysql_res

Pagina 673 - Viewing Message Headers

Back in our main program, we report to the user whether she was logged in or not, and at what level.The output from one login attempt is shown in Figu

Pagina 674 - Deleting Mail

Viewing ListsWe will implement a number of options for viewing available lists and list details. In Figure28.6, you can see two of these options: Show

Pagina 675 - Sending Mail

FIGURE 28.7The display_items() function has been used to lay out a list of the lists that the user is not subscribed to.This is the Show Other Lists p

Pagina 676

LISTING 28.6 Continuedif($items == 0)echo “<tr><td colspan = “.(1+$actions).”align = center>No Items to Display</td></tr>”;els

Pagina 677

• $title is the title that appears at the top of the table—in the case shown in Figure 28.7,we are passing in the title Unsubscribed Lists, as shown i

Pagina 678

LISTING 28.7 get_unsubscribed_lists() Function from mlm_fns.php—This Function Is Usedto Build an Array of Mailing Lists That a User Is Not Subscribed

Pagina 679

echo $num.”<BR>”;$num++;}At the beginning of each iteration, the condition is tested. If the condition is false, the blockwill not be executed a

Pagina 680

case ‘information’ :{display_information($id);break;}To see what the display_information() function does, look at Figure 28.8.Building Practical PHP a

Pagina 681 - Building a Mailing List

LISTING 28.8 Continued$info = load_list_info($listid);if($info){echo “<h2>”.pretty($info[listname]).”</h2>”;echo ‘<p>’.pretty($info[

Pagina 682

LISTING 28.9 Continued$query = “select count(*) from mail where listid = $listidand status = ‘SENT’”;$result = mysql_query($query);if($result){$info[‘

Pagina 683 - File Upload

LISTING 28.10 Continued$query = “select mailid, subject, listid from mailwhere listid = $listid and status = ‘SENT’ order by sent”;if(db_connect()){$r

Pagina 684 - Sending Mail with Attachments

unsubscribe(get_email(), $id);display_items(“Subscribed Lists”, get_subscribed_lists(get_email()),‘information’, ‘show-archive’, ‘unsubscribe’);break;

Pagina 685 - IGURE 28.3

LISTING 28.11 Continued$result = mysql_query($query);return $result;}The subscribe() function adds a row to the sub_lists table corresponding to the s

Pagina 686

FIGURE 28.9The display_password_form() function enables users to change their passwords.When a user clicks on the Change Password button at the bottom

Pagina 687

LISTING 28.12 change_password() Function from user_auth_fns.php—This FunctionValidates and Updates a User’s Passwordfunction change_password($email, $

Pagina 688

unset($normal_user);unset($admin_user);}This snippet of code disposes of the session variables and destroys the session. Notice that italso unsets the

Pagina 689

The extra options they have are Create List (create a new mailing list), Create Mail (create anew newsletter), and View Mail (view and send created ne

Pagina 690 - LISTING 28.2 Continued

The basic structure of a for loop isfor( expression1; condition; expression2)expression3;• expression1 is executed once at the start. Here you will us

Pagina 691 - MAILING LIST

case ‘store-list’ :{if(store_list($admin_user, $HTTP_POST_VARS)){echo “<p>New list added<br>”;display_items(“All Lists”, get_all_lists(),

Pagina 692

LISTING 28.13 Continued$query = “insert into lists values (NULL,‘$details[name]’,‘$details[blurb]’)”;$result = mysql_query($query);return $result;}}Th

Pagina 693

FIGURE 28.12The Create Mail option gives the administrator an interface for uploading newsletter files.The form you see is similar to a regular file u

Pagina 694

LISTING 28.14 Continued<td bgcolor = “#cccccc”><select name = list><?for($i = 0; $i<$lists; $i++){echo “<option value = “.$list[$

Pagina 695

LISTING 28.14 Continued</td></form></tr></table><?}The thing to note here is that the files we want to upload will have the

Pagina 696

LISTING 28.15 Continued$buttons[0] = ‘change-password’;$buttons[1] = ‘create-list’;$buttons[2] = ‘create-mail’;$buttons[3] = ‘view-mail’;$buttons[4] =

Pagina 697

LISTING 28.15 Continueddo_html_footer();exit;}// creating directory will fail if this is not the first message archived// that’s ok@ mkdir(“archive/$l

Pagina 698 - Implementing Login

LISTING 28.15 Continuedif($i==0)$destination = “archive/$list/$mailid/text.txt”;else if($i == 1)$destination = “archive/$list/$mailid/index.html”;else

Pagina 699

Strictly speaking, we should probably also check the $list and $mailid variables forunwanted characters, but we have ignored this for the sake of brev

Pagina 700

She can also click on the View Mail button, which will show her all the unsent newsletters inthe system, if she wants to preview and send mail later.

Pagina 701

do..while LoopsThe final loop type we will mention behaves slightly differently. The general structure of ado..while statement isdoexpression;while( c

Pagina 702 - LISTING 28.3 Continued

Sending the MessageClicking on the Send button for a newsletter activates the send action, which triggers the following code:case ‘send’ :{send($id, $

Pagina 703 - Logged In User

LISTING 28.16 Continued}else if (mysql_num_rows($result)==0){echo “There is nobody subscribed to list number $listid”;return false;}else{include(‘clas

Pagina 704 - Implementing User Functions

LISTING 28.16 Continuedmysql_result($result, $i, 1));}}// add HTML and text to the mimemail object$mail->add_html($html, $text);// note that we bui

Pagina 705 - Viewing Lists

LISTING 28.15 Continued$result = mysql_query($query);if(!$result)echo “<p>Error getting subscriber list”;$count = 0;// for each subscriberwhile(

Pagina 706 - FIGURE 28.7

This means each piece of mail must essentially be sent twice: once in test mode and once in real mode.The function also sends two different kinds of e

Pagina 707

Extending the ProjectAs usual with these projects, there are many ways you could extend the functionality. Youmight like to• Confirm membership with s

Pagina 708

34 7842 CH28 3/6/01 3:46 PM Page 710

Pagina 709 - Viewing List Information

CHAPTER29Building Web Forums35 7842 CH29 3/6/01 3:34 PM Page 711

Pagina 710 - Displays List Information

Building Practical PHP and MySQL ProjectsPART V712One good way to get users to return to your site is to offer Web forums. These can be used forpurpos

Pagina 711

The most difficult part of this application is finding a database structure that will store theinformation we want, and a way of navigating that struc

Pagina 712 - Viewing List Archives

Next: Saving the Customer’s OrderNow you know how to receive and manipulate the customer’s order. In the next chapter, we’lllook at how to store the o

Pagina 713 - Subscribing and Unsubscribing

The replies will be stored in an array. Each reply will itself be a tree_node, that can contain anarray of replies to that article, which are themselv

Pagina 714

FIGURE 29.2There are three main parts of the blah-blah forum system.A summary of the files in this application is shown in Table 29.1.TABLE 29.1 Files

Pagina 715 - Changing Account Settings

Designing the DatabaseThere are a few attributes we’ll need to store about each article posted to the forum: the personwho wrote it, called the poster

Pagina 716 - IGURE 28.9

We will make one other optimization: We will separate the message bodies from the other dataand store them in a separate table. The reason for this is

Pagina 717

grant select, insert, update, deleteon discussion.*to discussion@localhost identified by ‘password’;You can create this database structure by running

Pagina 718 - IGURE 28.10

Building Web ForumsCHAPTER 2929BUILDING WEBFORUMS719FIGURE 29.4The initial view of the article list shows the articles in “collapsed” form.What we see

Pagina 719 - Creating a New List

FIGURE 29.5The thread of discussion about persistence has been expanded.Building Practical PHP and MySQL ProjectsPART V720FIGURE 29.6All the threads h

Pagina 720

If you look closely at Figures 29.5 and 29.6, you can see that we are passing some parametersback to index.php in the command line. In Figure 29.5, th

Pagina 721 - Uploading a New Newsletter

unset($expanded);elseunset($expanded[$collapse]);}do_html_header(“Discussion Posts”);display_index_toolbar();// display the tree view of conversations

Pagina 722 - Displays the File Upload Form

If we are trying to expand a particular thread, we will have been passed a postid via $expand.We therefore add a new entry to the $expanded array to r

Pagina 723 - LISTING 28.14 Continued

CHAPTER2Storing and Retrieving Data04 7842 CH02 3/6/01 3:37 PM Page 49

Pagina 724 - Handling Multiple File Upload

Displaying the ArticlesLet’s look at the display_tree() function, shown in Listing 29.4.LISTING 29.4 display_tree() Function from output_fns.php—Creat

Pagina 725 - LISTING 28.15 Continued

the first level articles, which have no parent. After the tree has been constructed, we simplycall its display function to actually display the list o

Pagina 726

$query = “select * from header where parent = $postid order by posted”;$result = mysql_query($query);for ($count=0; $row = @mysql_fetch_array($result)

Pagina 727

// display + or - or a spacerif ( !$sublist && $this->m_children && sizeof($this->m_childlist))// we’re on the main page, have s

Pagina 728 - Previewing the Newsletter

This class contains the functionality that drives the tree view in this application.One instance of the treenode class contains details about a single

Pagina 729 - IGURE 28.14

When we construct the root treenode from display_tree() from the main page, we are actu-ally creating a -dummy node with no article associated with it

Pagina 730 - Sending the Message

Building Practical PHP and MySQL ProjectsPART V730After all that’s done, we call the root treenode’s display function (this is back in display_tree())

Pagina 731 - LISTING 28.16 Continued

Building Web ForumsCHAPTER 2929BUILDING WEBFORUMS731// we are collapsed - offer button to expandecho “<a href = ‘index.php?expand=”.$this->m_pos

Pagina 732

FIGURE 29.7We can now see the message body for this posting.This script shows us the message body, as well as the replies to this message. You will se

Pagina 733

echo “<br><br>”;display_replies_line();display_tree($expanded, 0, $postid);}do_html_footer();?>This script uses three main function cal

Pagina 734

Using PHPPART I50Now that we know how to access and manipulate data entered in an HTML form, we can lookat ways of storing that information for later

Pagina 735

This function, given a postid, will perform the two queries required to retrieve the messageheader and body for that posting, and put them together in

Pagina 736

First, look at the URL:http://webserver/chapter29/new_post.php?parent=5The parameter passed in as parent will be the parent postid of the new posting.

Pagina 737 - Building Web Forums

do_html_header(“$title”);display_new_post_form($parent, $area, $title, $message, $name);if($error)echo “Your message was not stored. Make sure you ha

Pagina 738

//get all header information from ‘header’$query = “select title from header where postid = $postid”;$result = mysql_query($query);if(mysql_numrows($r

Pagina 739

After the user types in his reply and clicks the Post button, he will be taken to thestore_new_post.php script. Sample output from this script is show

Pagina 740

As you can see, this is a short script. Its main task is to call the store_new_post() function.This page has no visual content of its own. If storing

Pagina 741

$query = “insert into header values(‘“.$post[‘parent’].”’,‘“.$post[‘poster’].”’,‘“.$post[‘title’].”’,0,‘“.$post[‘area’].”’,now(),NULL)”;$result = mysq

Pagina 742

return false;}return $id;}}This is a long function, but it is not overly complex. It is only long because inserting a postingmeans inserting entries i

Pagina 743

NextIn Chapter 30, “Generating Personalized Documents in Portable Document Format (PDF),” wewill use the PDF format to deliver documents that are attr

Pagina 744 - Viewing the Tree of Articles

CHAPTER30Generating PersonalizedDocuments in PortableDocument Format (PDF)36 7842 CH30 3/6/01 3:40 PM Page 743

Pagina 745

We’ve modified the form to include a quick way to obtain the customer’s shipping address.You can see this form in Figure 2.1.Storing and Retrieving Da

Pagina 746 - FIGURE 29.6

On service driven sites, we sometimes need to deliver personalized documents, generated inresponse to input from our visitors. This can be used to pro

Pagina 747 - Expanding and Collapsing

Evaluating Document FormatsThe most important decision we need to make is what format to deliver the certificate in.Options include paper, ASCII text,

Pagina 748

programs; and variable quality printing. In addition, although HTML can include any type of external element, the capability of the browser to display

Pagina 749

Because the format is documented and freely available, RTF is readable by more software thanWord’s binary format. Be aware though that users opening a

Pagina 750 - Displaying the Articles

You can read more about Ghostscript athttp://www.ghostscript.com/and download it fromhttp://www.cs.wisc.edu/~ghost/For our current application, PostSc

Pagina 751 - Using the treenode Class

Solution ComponentsTo get the system working, we will need to be able to examine users’ knowledge and (assum-ing that they pass the test) generate a c

Pagina 752 - LISTING 29.5 Continued

To create the file, we used Microsoft Word to design a document. One of the tools in theAcrobat package is Adobe Distiller. Within Distiller, we neede

Pagina 753 - BUILDING WEB

There is a free trial option for this service if you want to test it out.There is also a free ftp-based interface to ps2pdf at the Net Distillery:http

Pagina 754

Solution OverviewWe will produce a system with three possible outcomes. As shown in Figure 30.1, we will askquiz questions, assess the answers, and th

Pagina 755

TABLE 30.1 ContinuedName Type Descriptionpdflib.php Application Script to generate PDFcertificate using PDFlibsignature.tif image Bitmap image of sign

Pagina 756

Overview of File ProcessingThere are three steps to writing data to a file:1. Open the file. If the file doesn’t already exist, it will need to be cre

Pagina 757 - Viewing Individual Articles

LISTING 30.1 Continued<li><input type = radio name = q1 value = 1>Outputs strings.<li><input type = radio name = q1 value = 2>

Pagina 758 - IGURE 29.7

FIGURE 30.2index.html asks the user to answer quiz questions.Grading the AnswersWhen the user submits his answers to the questions in index.html, we n

Pagina 759

LISTING 30.2 ContinuedSorry:<img src = ‘rosette.gif’ alt = ‘’></h1>”;echo “<p>You need to fill in your name and answer all questions

Pagina 760 - Adding New Articles

LISTING 30.2 Continued<input type = image src = ‘certificate.gif’ border = 0></center>”;echo “<input type = hidden name = score value =

Pagina 761

FIGURE 30.3score.php presents successful visitors with the option to generate a certificate in one of three ways.Generating an RTF CertificateThere is

Pagina 762 - Title from the Database

We can build a complex document, such as the one shown in Figure 30.4, easily using a wordprocessor.Generating Personalized Documents in Portable Docu

Pagina 763

LISTING 30.3 Continuedif( !$name || !$score ){echo “<h1>Error:</h1>This page was called incorrectly”;}else{//generate the headers to help

Pagina 764 - IGURE 29.9

header( “Content-type: application/msword” );header( “Content-Disposition: inline, filename=cert.rtf”);The first header tells the browser that we are

Pagina 765

This approach works very well. The calls to str_replace() run very quickly, even though ourtemplate and therefore the contents of $output are fairly

Pagina 766 - LISTING 29.13 Continued

This is a neat way of doing things, but it has two limitations. First, it assumes that you own acopy of Acrobat. Second, it is difficult to substitute

Pagina 767 - Extensions

Using fopen() to Open a FileLet’s assume that we want to write a customer order to Bob’s order file. You can open this filefor writing with the follow

Pagina 768

LISTING 30.4 Continuedif(!$name||!$score){echo “<h1>Error:</h1>This page was called incorrectly”;}else{//generate the headers to help a br

Pagina 769 - Document Format (PDF)

FIGURE 30.6pdf.php generates a certificate from an PDF template.One problem with this approach is that the code runs quite slowly because of the regul

Pagina 770

Although it is possible that the official PHP binding might be better then the current one when(or if) it arrives, the current one is very good. The o

Pagina 771 - Evaluating Document Formats

LISTING 30.5 Continued// display a link to downloadecho “download the pdf <a href = ‘hello.pdf’>here</a>”;?>The most likely error you w

Pagina 772 - Rich Text Format

PDFlib works in points, both for page size, and for locating coordinate locations on each page.For reference, A4 is approximately 595 by 842 points an

Pagina 773 - PostScript

pdf_set_font($pdf, “Helvetica-Bold”, 24, “host”);Font sizes are specified in points. We have chosen host character encoding. The allowable val-ues are

Pagina 774 - Portable Document Format

When we have finished the whole PDF document, we need to close it using pdf_close().When we are generating a file, we also need to close the file.The

Pagina 775

LISTING 30.6 Continued// create a pdf document in memory$pdf = pdf_open();// set up the page size in points// US letter is 11” x 8.5”// there are appr

Pagina 776

LISTING 30.6 Continued$startx = 70;pdf_show_xy($pdf, “This is to certify that:”, $startx, 430);pdf_show_xy($pdf, strtoupper($name), $startx+90, 391);p

Pagina 777 - OCUMENTS IN

LISTING 30.6 Continuedpdf_lineto($pdf, 666, 150);pdf_closepath($pdf);pdf_stroke($pdf);// draw ribbon 2pdf_moveto($pdf, 660, 150);pdf_lineto($pdf, 680,

Pagina 778

PHP AND MYSQL WEB DEVELOPMENTviiiAssociative Arrays ...73Initializing an

Pagina 779 - Asking the Questions

TABLE 2.1 Summary of File Modes for fopenMode Meaningr Read mode—Open the file for reading, beginning from the start of the file.r+ Read mode—Open the

Pagina 780 - LISTING 30.1 Continued

LISTING 30.6 Continued$y = $radius*sin($angle) + $centery;}else{$x = $inner_radius*cos($angle) + $centerx;$y = $inner_radius*sin($angle) + $centery;}i

Pagina 781 - Grading the Answers

We will look at some of the parts of this script that are different from the previous examples.Visitors need to get their own details on a certificate

Pagina 782 - LISTING 30.2 Continued

pdf_rect($pdf, $inset+$border/2,$inset+$border/2,$width-2*($inset+$border/2),$height-2*($inset+$border/2));pdf_stroke($pdf);After we have drawn our on

Pagina 783

pdf_moveto($pdf, 630, 150);pdf_lineto($pdf, 610, 55);pdf_lineto($pdf, 632, 69);pdf_lineto($pdf, 646, 49);pdf_lineto($pdf, 666, 150);pdf_closepath($pdf

Pagina 784 - Generating an RTF Certificate

Some of our headers seemed to cause problems with session control headers. There are a fewways around this. We have found using GET parameters rather

Pagina 785 - IGURE 30.4

IN THIS PARTA Installing PHP 4 and MySQL 781B Web Resources 803AppendixesPARTVI37 7842 part 6 3/6/01 3:35 PM Page 779

Pagina 786

37 7842 part 6 3/6/01 3:35 PM Page 780

Pagina 787 - IGURE 30.5

APPENDIXAInstalling PHP 4 and MySQL38 7842 app a 3/6/01 3:40 PM Page 781

Pagina 788

AppendixesPART VI782Apache, PHP, and MySQL are available for multiple operating systems and Web servers. Inthis appendix, we will explain how to set u

Pagina 789

PHP accomplishes this by doing the following:• PHP refuses to interpret the command-line arguments, when invoked as a CGI binary.• PHP also prevents a

Pagina 790 - LISTING 30.4 Continued

If the filename you use begins with ftp://, a passive mode FTP connection will be opened tothe server you specify and a pointer to the start of the fi

Pagina 791 - IGURE 30.6

Our installation will be done on a Red Hat 6.2 Linux server, but will be generic enough toapply to other UNIX servers.Let’s start by listing out the t

Pagina 792

Let’s begin! Become root by using su.$ suand enter the user root’s password. Change to the directory that you have stored the sourcefiles in, for exam

Pagina 793

Enter password:+--------------------+| Databases |+--------------------+| mysql |+--------------------+When you install MySQL, i

Pagina 794

# ./configure --with-mysql=/usr/local/mysql \--with-xml --with-apache=../apache_1.3.x \--with-curl=/usr/local/curl \--with-pspell=/usr/local/pspell \

Pagina 795

It’s time to set up OpenSSL. This is what you will use to create temporary certificates andCSR files. The --prefix specifies the main installation dir

Pagina 796

(You could alternatively set SSL_BASE and RSA_BASE as environment variables if you prefer.)Finally you can make Apache and the certificates, and then

Pagina 797 - DOCUMENTS IN

| Apache the first time by running: || || /usr/local/apache/

Pagina 798

FIGURE A.1The default test page provided by Apache.Is PHP Support Working?Now we will test for PHP support. Create a file with the name of test.php wi

Pagina 799

FIGURE A.2The function phpinfo() provides useful configuration information.Is SSL Working?Okay, now we are ready to test for SSL. First, stop the serv

Pagina 800

certificate from VeriSign or Thawte, the browser would not prompt you because their certifi-cates come from a trusted Certification Authority (CA). In

Pagina 801

If you get this error, you need to make sure that the user that the script runs as has permissionto access the file you are trying to use. Depending o

Pagina 802

Windows 95/98The Windows 95/98 version of MySQL comes with two different MySQL servers:• mysqld: Compiled with full debugging and automatic memory all

Pagina 803 - Problems with Headers

FIGURE A.4The Services Control Manager allows you to configure the services running on your machine.To test whether or not MySQL is working, you can e

Pagina 804

FIGURE A.5The Apache installer is easy to use.The install program will prompt you for the following:• The directory to install Apache. (The default is

Pagina 805 - Appendixes

Running Apache in a Console WindowTo run Apache from the console window, select the Start Apache as console App option fromthe Start menu. This will o

Pagina 806

Apache will be listening to port 80 (unless you changed the Port, Listen, or BindAddress direc-tives in the configuration files) after it starts. To c

Pagina 807 - Installing PHP 4 and MySQL

side-effect of the Windows port. Apache 2.0 is making progress to implement theexpected asynchronous behavior, and we hope to discover that the NT/200

Pagina 808

Note that with the AddType directive, you can specify how Apache should handle the variousfile extensions. In the case previously mentioned, we specif

Pagina 809

2. Start the Microsoft Management Console (it might appear as the Internet ServicesManager, either in your Windows NT 4.0 Option Pack branch or in the

Pagina 810

2. Edit the enclosed PWS-php4.reg file to reflect the location of your php4isapi.dll. Forwardslashes should be escaped, for example:[HKEY_LOCAL_MACHIN

Pagina 811

APPENDIXBWeb Resources39 7842 App B 3/6/01 3:38 PM Page 803

Pagina 812

FIGURE 2.3Using your own error messages instead of PHP’s can be more user friendly.Writing to a FileWriting to a file in PHP is relatively simple. You

Pagina 813 - Apache and mod_SSL

AppendixesPART VI804This appendix lists some of the many resources available on the Web, which can be used to findtutorials, articles, news, and sampl

Pagina 814

HotScripts.com—http://www.hotscripts.com—A great categorized selection of scripts. Thesite has scripts in various languages like PHP, ASP, and Perl. I

Pagina 815

e-gineer—http://www.e-gineer.com—Articles, scripts, and a knowledge base of commonquestions and answers.Source Forge—http://sourceforge.net—Extensive

Pagina 816

INDEXSYMBOLS+ (addition operator), 26= (assignment operator), 27@ (at symbol), 56\ (back slashes), 263, 448^ (bitwise operator), 31| (bitwise operator

Pagina 817 - Is PHP Support Working?

+ (plus symbol)808+ (plus symbol), Webforum articles, 719’ (quote symbol)” (quotes)& (reference operator), 29* (regular expressions),111+ (regular

Pagina 818 - Is SSL Working?

architecture809aliases for tables, 218-219ALL privilege, 191ALTER privilege, 190ALTER TABLE statement,223syntaxes, 224ALTER [COLUMN] column{SET DEFAUL

Pagina 819

architecture810performing actions, 663preprocessing, 663Web databases, 180-181arcs, ImageArc() function,428arithmetic operators, 26array push() functi

Pagina 820 - Windows NT/Win2000

Boutell Web site811mod_auth_mysql module,322-324documentation Websites, 324installing, 322-323passwords, 291-292session control, 438-445authmain.php s

Pagina 821

boxes812boxes, 416. See alsobounding boxesbranching (regularexpressions), 112break statement, 47breaking up code, 466-467brochureware sites, 269-271co

Pagina 822 - Running Apache for Windows

code813Certifying Authorities(CAs), 297-298CGI Interpreter, 782-783PHP, running, 782-783CGI specification Website, 368Change Password button,689change

Pagina 823 - Running Apache as a Service

File FormatsWhen you are creating a data file like the one in our example, the format in which you storethe data is completely up to you. (However, if

Pagina 824

code814HTMLfile upload, 353files, catching, 354-356indenting, 39, 465-466line graphs, script for outputting, 405logic, 471separating from content, 472

Pagina 825 - Installing PHP for Windows

column types (tables)815script to make HTTPS connections, 388script to verify URL andemail address, 376scriptslist of extensions andfunctions in PHP,

Pagina 826 - Let’s Test Our Work

columns816columnsDESCRIBE statement, 257values, EXPLAIN state-ment, 259columns (tables), 173keys, 173-175creating for Web databases, 179foreign keys,

Pagina 827

control characters817component structures,467componentsonline newsletters, 657user personalization,499-500compressionGIF, LZW (Lempel ZivWelch), 404GI

Pagina 828 - Other Configurations

control structures818control structures, 38-47breaking out of, 47conditionals, 38-42code blocks, 38-39comparing, 42else statements, 39-40elseif statem

Pagina 829 - Web Resources

databases819in a particular order,219-220with specific criteria,212, 214rows, returning, 222-223sensitive datacredit card numbers,storing, 338storing,

Pagina 830 - PHP Resources

databases820privilege system, 246-247columns_priv table,249-250db table, 248-249grant table, 250-251host table, 248-249privileges, updating,251tables_

Pagina 831

discussion board application821decrement operators, 28-29decryption, 294default values, databaseoptimization, 262delete bm() function, 531DELETE privi

Pagina 832 - Apache Resources

discussion board application822plus symbols, 719treenode class, 725-731database design, 716-718extensions, 741files, 715posters, 716solution component

Pagina 833

email client application (Warm Mail)823Ee-commerce Web sites,268-280adding value to goods orservices, 276authentication, 284cutting costs, 276-277onli

Pagina 834 - + (plus symbol)

This function will return true if the file was successfully closed or false if it wasn’t. This isgenerally much less likely to go wrong than opening a

Pagina 835

email client application (Warm Mail)824script architecture,623-629sending mail, 649-652forwarding messages,651-652new messages, 649-651replying to mes

Pagina 836

files825EXPLAIN statement, 257-260column values, 259join types, 258output, 257, 260explode() function, 86-87,102, 377exploits, BUGTRAQarchives Web sit

Pagina 837 - Boutell Web site

files826catching, code, 354-356checking existence of, 63checking size of, 63closing, 58-59content management systems, 595create database.sql,595-586db

Pagina 838

fopen() function827url_fns.php, 502user_auth_fns.php, 502pollsetup.sql, 420progex.php, 366properties, changing, 364reading, 52, 361-364feof() function

Pagina 839

for loops828for loops, 45-46foreign keys (databases),175forgot_form.php, 501forgot_passwd.php, 501format codes, date() function, 392-394formattingoutp

Pagina 840

functions829ftp_fput() function, 385ftp_get() function, 385ftp_login() function, 382ftp_mdtm() function, 383ftp_nlist() function, 386ftp_put() functio

Pagina 841

functions830display_account_setup(),633, 636display_book_form(),581-583display_cart(), 560-563display_categories(), 553display_list(), 640-641display_

Pagina 842

functions831htmlspecialchars() function, 233ImageArc(), 428ImageColorAllocate(), 406ImageCopyResized(), 594ImageCreate(), 405ImageCreateFromGIF(),406,

Pagina 843

functions832phpinfo(), 368, 751posix_getgrgid(), 363posix_getpwuid(), 363pretty(), 685prev() function, 88print(), 97printf(), 98-99prototypes, 130pute

Pagina 844

highlight_string() function833get user urls() function,515get writer record() function, 607getdate() function, 395getenv() function, 367-368gethostbya

Pagina 845

FIGURE 2.4The vieworders.php script displays all the orders currently in the orders.txt file in the browser window.Let’s look at the functions in this

Pagina 846

host table834host table, 247-249mysql database, 249HotScripts.com Web site,805htaccess files (ApacheWeb server), basicauthentication (HTTP),316-319HTM

Pagina 847

insert_book.php script835PNG (Portable NetworkGraphics), 403WBMP (WirelessBitmap), 403functions, 428generating automatically,410inline, dynamically pr

Pagina 848

insert_book.php script (Shopping Cart application)836insert_book.php script(Shopping Cart applica-tion), 578-579insert_book_form.phpscript (Shopping C

Pagina 849

listings837Gnu Privacy Guard(GPG), 340importing (Gnu PrivacyGuard), 341keys (arrays), 71keys (databases), 173-175creating for Web databases,179foreign

Pagina 850

listings838get_random_word()Function fromuser_auth_fns.php, 524get_unsubscribed_lists()Function frommlm_fns.php, 683get_user_urls() Functionfrom url_f

Pagina 851

members_only.php script (authentication)839logout.php files, 605logout.php script (authen-tication), 444-445lookup functions, 374-378checkdnsrr(), 378

Pagina 852

memory, freeing up (mysql_free_result() function)840memory, freeing up(mysql_free_result()function), 241-242message headers (WarmMail application), vi

Pagina 853

mysql_select_db() function841databasescreating, 187creating from PHPscripts, 242deleting, 242results.php script,230-231selecting, 193-194tables, creat

Pagina 854

naming functions842Nnaming functions, 133-134naming conventions,code, 463function names, 464modular names, 464variable names, 463-464Natural Order Str

Pagina 855

operators843Page class code listing,161-165ServicesPage class,166-167TLA Consulting homepage, generating,165-166ODBC (Open DatabaseConnectivity), func

Pagina 856

There are many different functions that can be used to read from files. The fgets() function isuseful when dealing with files that contain plain text

Pagina 857

operators844reference operator, 29returning values, 27associativity, 34-35bitwise operators, 31comma operator, 32comparison, WHEREclauses, 212-213comp

Pagina 858

PHP845payment module(Shopping Cart applica-tion), 572-575payment systems(Shopping Cart applica-tion), 541-542PDF (Portable DocumentFormat), 744, 748fu

Pagina 859

PHP846floor() function, 399getdate() function, 395mktime() function,394-398PHP Web site, 400date() function, 17-18development environments,IDE (integr

Pagina 860

php4win Web site847associativity, 34-35bitwise operators, 31comma operator, 32comparison operators,29-30error suppression operator, 32execution operat

Pagina 861

phpautodoc Web site848phpautodoc Web site, 470PHPBookmark applicationcreating, 498database schema, 502diagrams, 500front page, 504-506function librari

Pagina 862

prototypes (functions)849printing stringsformatting strings forprinting, 97-99print() function, 97printf() function, 98-99sprintf() function, 98text o

Pagina 863

public key encryption850public key encryption,295-296public keys, Gnu PrivacyGuard (GPG), 340exporting, 340importing, 341publish story.php, 596publish

Pagina 864

retrieving851character sets, 109-110curly braces ({}), 112slash (\), 112Smart Form Mail applica-tion, 113-114special characters, 113literal special ch

Pagina 865

retrieving852tables, joining, 216-217tables, rows unmatched,217-218two-table joins,214-216with specific criteria,212-214vote database results, code,42

Pagina 866

Secure Sockets Layer (SSL)853scope (variables), 25scope fields, 248score.php, 752-757screening user input, 336script architecture, 663-672footers, 663

Pagina 867

The optional second parameter specifies whether PHP should look for the file in theinclude_path and operates the same way as in fopen(). The function

Pagina 868

Secure Sockets Layer (SSL)854handshaking, 334protocol stacks, 333sending data, 334-335secure storage, 336-337credit card numbers, 338secure transactio

Pagina 869

setting up855Send button, 704send() function, 704sending messages, onlinenewsletters, 704-708sending mail, 371Warm Mail application,649-652forwarding

Pagina 870

settype() function856settype() function, 36set_magic_quotes_runtime() function, 449set_time_limit() function,386Shopping Cart applica-tion, 540adminis

Pagina 871

statements857software, errors (securitythreats), 288-289software engineering,460-462defined, 460-462software errors (commer-cial Web sites), 279soluti

Pagina 872

statements858echo statements, 20-21else statements, 39-40elseif statements, 40exit statement, 47EXPLAIN, 257-260column values, 259join types, 258outpu

Pagina 873

syntax859orderingstrcasecmp() function,105strcmp() function, 104strnatcmp() function,105printingformatting strings forprinting, 97-99print() function,

Pagina 874

syntax860MODIFY [COLUMN] column_description, 224RENAME [AS]new_table_name, 224SHOW statement, 255-257syntax errors, 478-480syntax highlighter, 454-455

Pagina 875

tracking user’s purchases (Shopping Cart application)861plain text (encryption), 293positioning onto buttons,418writing onto buttons, 419text files, 5

Pagina 876

transactions, secure862transactions, secure, 328-332Internet, 330-331screening user input, 336Secure Sockets Layer(SSL), 332-335secure storage, 336-33

Pagina 877

variable variables863user feedback (commer-cial Web sites), 271user input, screening, 336user interface design(commercial Web sites),274user personali

Pagina 878

It is not generally sensible to read a file character-by-character unless for some reason we wantto process it character-by-character.Reading an Arbit

Pagina 879 - Secure Sockets Layer (SSL)

variables864variablesarrays, 70-71applying functions toelements, 89-90associative arrays,73-75converting to scalarvariables, 91-92counting elements,90

Pagina 880

Web issues, database security865messages, 643-647selecting accounts,637-640script architecture, 623-629sending mail, 649-652forwarding messages,651-65

Pagina 881

Web pages, protecting multiple pages866Web pages, protectingmultiple pages, 312Web serversApache. See Apache Webserverauthentication, 292-293commands,

Pagina 882

ZEND.Com Web site867PHP Base Library, 805PHP Center, 805PHP Classes Repository,805Metabase, 243PHP Club, 805PHP Developer, 805PHP Homepage, 805PHP ima

Pagina 883

CONTENTSixIntroduction to Regular Expressions ...109The Basics ...

Pagina 884

Navigating Inside a File: rewind(), fseek(), and ftell()You can manipulate and discover the position of the file pointer inside a file using rewind(),

Pagina 885

File LockingImagine a situation where two customers are trying to order a product at the same time. (Notuncommon, especially when you start to get any

Pagina 886

You should also add locks to vieworders.php:$fp = fopen(“$DOCUMENT_ROOT /../orders/orders.txt”, “r”);flock($fp, 1); // lock file for reading// read f

Pagina 887

How RDBMSs Solve These ProblemsRelational database management systems address all of these issues:• RDBMSs can provide faster access to data than flat

Pagina 888

04 7842 CH02 3/6/01 3:37 PM Page 68

Pagina 889

CHAPTER3Using Arrays05 7842 CH03 3/6/01 3:41 PM Page 69

Pagina 890

Using PHPPART I70This chapter shows you how to use an important programming construct—arrays. The vari-ables that we looked at in the previous chapter

Pagina 891 - Web issues, database security

After we have the information as an array, we can do a number of useful things with it. Usingthe looping constructs from Chapter 1, we can save work b

Pagina 892

If you have the information stored in file on disk, you can load the array contents directly fromthe file. We’ll look at this later in this chapter un

Pagina 893 - ZEND.Com Web site

Using Loops to Access the ArrayBecause the array is indexed by a sequence of numbers, we can use a for loop to more easilydisplay the contents:for ( $

Comentarios a estos manuales

Sin comentarios