Escolar Documentos
Profissional Documentos
Cultura Documentos
Part 1:
The Only Acronym You'll Ever Need
If you're new to Web development, you could be forgiven for thinking that it
consists of no more than a mass of acronyms, each one more indecipherable
than the last. ASP, CGI, SOAP, XML, HTTP - the list seems never-ending, and
the sheer volume of information on each of these can discourage the most
avid programmer. But before you put on your running shoes and flee, there's
a little secret you should know. To put together a cutting-edge Web site,
chock full of all the latest bells and whistles, there's only one acronym you
really need to know:
PHP
Now, while you have almost certainly heard of PHP, you may not be aware of
just how powerful the language is, and how much it can do for you. Today,
PHP has the enviable position of being the only open-source server-side
scripting language that's both fun and easy to learn. This is not just
advertising: recent surveys show that more than 16,000,000 Web sites use
PHP as a server side scripting language, and the language also tops the list
of most popular Apache modules.
Why, you ask? The short answer: it's powerful, it's easy to use, and it's free.
Extremely robust and scalable, PHP can be used for the most demanding of
applications, and delivers excellent performance even at high loads. Built-in
database support means that you can begin creating data-driven
applications immediately, XML support makes it suitable for the new
generation of XML-enabled applications, and the extensible architecture
makes it easy for developers to use it as a framework to build their own
custom modules. Toss in a great manual, a knowledgeable developer
community and a really low price (can you spell f-r-e-e?) and you've got the
makings of a winner!
My goal in this series of tutorials is very simple: I'll be teaching you the
basics of using PHP, and showing you why I think it's the best possible tool
for Web application development today. I'll be making no assumptions about
your level of knowledge, other than that you can understand basic HTML and
have a sense of humor. And before you ask... Yes, this series covers both
PHP 4 and PHP 5, with new PHP 5 features flagged for easy reference.
From the above, it is clear that in order to begin using PHP, you need to have
a proper development environment set up.
This series will focus on using PHP with the Apache Web server on Linux, but
you can just as easily use PHP with Apache on Windows, UNIX and Mac OS.
Detailed instructions on how to set up this development environment on
each platform are available in the online manual, at http://www.php.net/
manual/en/installation.php - or you can just download a copy of PHP 5 from
http://www.php.net and read the installation instructions.
Go do that now, and come back when you've successfully installed and
tested PHP.
Start Me Up
There's one essential concept that you need to get your mind around before
we proceed further. Unlike CGI scripts, which require you to write code to
output HTML, PHP lets you embed PHP code in regular HTML pages, and
execute the embedded PHP code when the page is requested.
These embedded PHP commands are enclosed within special start and end
tags, like this:
<?php
?>
Here's a simple example that demonstrates how PHP and HTML can be
combined:
<html>
<head></head>
<body>
<?php
// print output
echo 'Neo: I am Neo, but my people call me The One.';
?>
</body>
</html>
Not quite your traditional "Hello, World" program... but then again, I always
thought tradition was over-rated.
Save the above script to a location under your Web server document root,
with a .php extension, and browse to it. You'll see something like this:
<html>
<head></head>
<body>
Agent: So who do you think you are, anyhow?
<br />
Neo: I am Neo, but my people call me The One.
</body>
</html>
What just happened? When you requested the script above, Apache
intercepted your request and handed it off to PHP. PHP then parsed the
script, executing the code between the <?php...?> marks and replacing it
with the output of the code run. The result was then handed back to the
server and transmitted to the client. Since the output contained valid HTML,
the browser was able to render it for display to the user.
A close look at the script will reveal the basic syntactical rules of PHP. Every
PHP statement ends in a semi-colon. This convention is identical to that used
in Perl, and omitting the semi-colon is one of the most common mistakes
newbies make. That said, it is interesting to note that a semi-colon is not
needed to terminate the last line of a PHP block. The PHP closing tag
includes a semi-colon, therefore the following is perfectly valid PHP code:
<?php
// print output
echo 'Neo: I am Neo, but my people call me The One.'
?>
It's also possible to add comments to your PHP code, as I've done in the
example above. PHP supports both single-line and multi-line comment
blocks:
<?php
?>
Blank lines within the PHP tags are ignored by the parser. Everything outside
the tags is also ignored by the parser, and returned as-is. Only the code
between the tags is read and executed.
A Case of Identity
Variables are the bread and butter of every programming language... and
PHP has them too. A variable can be thought of as a programming construct
used to store both numeric and non-numeric data; the contents of a variable
can be altered during program execution. Finally, variables can be compared
with each other, and you - the programmer - can write code that performs
specific actions on the basis of this comparison.
Note that variable names in PHP are case sensitive, so $me is different from
$Me or $ME.
<?php
// define variables
$name = 'Neo';
$rank = 'Anomaly';
$serialNumber = 1;
// print output
echo "Neo: I am <b>$name</b>, the <b>$rank</b>. You can
call me by my serial number, <b>$serialNumber</b>.";
?>
</body>
</html>
Here, the variables $name, $rank and $serialNumber are first defined with
string and numeric values, and then substituted in the echo() function call.
The echo() function, along with the print() function, is commonly used to
print data to the standard output device (here, the browser). Notice that I've
included HTML tags within the call to echo(), and those have been rendered
by the browser in its output. You can do this too. Really.
An Equal Music
To assign a value to a variable, you use the assignment operator: the =
symbol. This is used to assign a value (the right side of the equation) to a
variable (the left side). The value being assigned need not always be fixed; it
could also be another variable, an expression, or even an expression
involving other variables, as below:
<?php
?>
Interestingly, you can also perform more than one assignment at a time.
Consider the following example, which assigns three variables the same
value simultaneously:
<?php
?>
Not My Type
Every language has different types of variable - and PHP is no exception. The
language supports a wide variety of data types, including simple numeric,
character, string and Boolean types, and more complex arrays and objects.
Here's a quick list of the basic ones, with examples:
$auth = true;
?>
Integer: An integer is a plain-vanilla whole number like 75, -95, 2000
or 1. <?php
$age = 99;
?>
Floating-point: A floating-point number is typically a fractional
number such as 12.5 or 3.141592653589. Floating point numbers may
be specified using either decimal or scientific notation. <?php
$temperature = 56.89;
?>
String: A string is a sequence of characters, like "hello" or
"abracadabra". String values may be enclosed in either double quotes
("") or single quotes(''). (Quotation marks within the string itself can
be "escaped" with a backslash (\) character.) String values enclosed in
double quotes are automatically parsed for special characters and
variable names; if these are found, they are replaced with the
appropriate value. Here's an example: <?php
?>
To learn more about PHP's data types, visit http://www.php.net/manual/en/
language.types.php.
Market Value
If variables are the building blocks of a programming language, operators
are the glue that let you build something useful with them. You've already
seen one example of an operator - the assignment operator -, which lets you
assign a value to a variable. Since PHP believes in spoiling you, it also comes
with operators for arithmetic, string, comparison and logical operations.
A good way to get familiar with operators is to use them to perform
arithmetic operations on variables, as in the following example:
<html>
<head>
</head>
<body>
<?php
// set quantity
$quantity = 1000;
?>
</body>
</html>
Looks complex? Don't be afraid - it's actually pretty simple. The meat of the
script is at the top, where I've set up variables for the unit cost and the
quantity. Next, I've performed a bunch of calculations using PHP's various
mathematical operators, and stored the results of those calculations in
different variables. The rest of the script is related to the display of the
resulting calculations in a neat table.
<?php
// this...
$a = 5;
$a = $a + 10;
?>
<?php
?>
<?php
// define string
$str = 'the';
?>
To learn more about PHP's arithmetic and string operators, visit http://
www.php.net/manual/en/language.operators.arithmetic.php and http://
www.php.net/manual/en/language.operators.string.php.
That's about it for this tutorial. You now know all about the basic building
blocks and glue of PHP - its variables and operators. In Part Two of this
series, I'll be using these fundamental concepts to demonstrate PHP's
powerful form processing capabilities.
Part 2:
Not What You Expected
In Part One of this series, I gave you a brief introduction to PHP, and how it
fits into your Web application development environment. I also taught you
the basics of PHP variables, and showed you how to add, multiply and
concatenate them together.
Now that you know the basics, it's time to focus in on one of PHP's nicer
features - its ability to automatically receive user input from a Web form and
convert it into PHP variables. If you're used to writing Perl code to retrieve
form values in your CGI scripts, PHP's simpler approach is going to make
you weep with joy. So get that handkerchief out, and scroll on down.
Form...
Forms have always been one of quickest and easiest ways to add
interactivity to your Web site. A form allows you to ask customers if they like
your products, casual visitors for comments on your site, and pretty girls for
their phone numbers. And PHP can simplify the task of processing the data
generated from a Web-based form substantially, as this first example
demonstrates. This example contains two scripts, one containing an HTML
form (named form.htm) and the other containing the form processing logic
(message.php). Here's form.htm:
<html>
<head></head>
<body>
<form action="message.php" method="post">
Enter your message: <input type="text" name="msg"
size="30">
<input type="submit" value="Send">
</form>
</body>
</html>
As you probably already know, the "action" attribute of the <form> tag
specifies the name of the server-side script (message.php in this case) that
will process the information entered into the form. The "method" attribute
specifies how the information will be passed.
...And Function
Now for the other half of the puzzle: the message.php script. This script
reads the data submitted by the user and "does something with it". Here is
message.php:
<html>
<head></head>
<body>
<?php
// retrieve form data
$input = $_POST['msg'];
// use it
echo "You said: <i>$input</i>";
?>
</body>
</html>
When you enter some data into form.htm (let's say "Boo"), and submit it,
the form processor message.php will read it and display it to you ("You
said: Boo"). Thus, whenever a form is submitted to a PHP script, all variable-
value pairs within that form automatically become available for use within
the script, through a special PHP container variable: $_POST. You can then
access the value of the form variable by using its "name" inside the $_POST
container, as I did in the script above.
Obviously, PHP also supports the GET method of form submission. All you
need to do is change the "method" attribute to "get", and retrieve values
from $_GET instead of $_POST. The $_GET and $_POST variables are
actually a special type of PHP animal called an array, which I'll be teaching
you about shortly. Don't worry too much about it at the moment, just make
sure you're comfortable with retrieving simple values from a form with PHP,
and then scroll on down to learn about some more operators that are useful
in this context.
You've already seen some of PHP's arithmetic and string operators. However,
the language also comes with operators designed specifically to compare two
values: the so-called "comparison operators". Here's an example that
demonstrates them in action:
<?php
// less-than operator
// returns true if left side is less than right
// returns true here
$result = ($mean < $median);
print "result is $result<br />";
// greater-than operator
// returns true if left side is greater than right
// returns false here
$result = ($mean > $median);
print "result is $result<br />";
// less-than-or-equal-to operator
// returns true if left side is less than or equal to right
// returns false here
$result = ($median <= $mode);
print "result is $result<br />";
// greater-than-or-equal-to operator
// returns true if left side is greater than or equal to
right
// returns true here
$result = ($median >= $mode);
print "result is $result<br />";
// equality operator
// returns true if left side is equal to right
// returns true here
$result = ($mean == $mode);
print "result is $result<br />";
// not-equal-to operator
// returns true if left side is not equal to right
// returns false here
$result = ($mean != $mode);
print "result is $result<br />";
// inequality operator
// returns true if left side is not equal to right
// returns false here
$result = ($mean <> $mode);
print "result is $result";
?>
The result of a comparison test is always Boolean: either true (1) or false (0
- which doesn't print anything). This makes comparison operators an
indispensable part of your toolkit, as you can use them in combination with a
conditional statement to send a script down any of its multiple action paths.
PHP 4.0 also introduced a new comparison operator, which allows you to test
both for equality and type: the === operator. The following example
demonstrates it:
<?php
/* define two variables */
$str = '10';
$int = 10;
/* returns true, since the variables are the same type and
value */
$anotherInt = 10;
$result = ($anotherInt === $int);
print "result is $result";
?>
A Question of Logic
In addition to the comparison operators I used so liberally above, PHP also
provides four logical operators, which are designed to group conditional
expressions together. These four operators - logical AND, logical OR, logical
XOR and logical NOT - are illustrated in the following example:
<?php
/* define some variables */
$auth = 1;
$status = 1;
$role = 4;
?>
if (condition) {
do this!
}
Let me show you how the if() statement works by combining it with a
form. In this example, the user is asked to enter his or her age.
<html>
<head></head>
<body>
<form action="ageist.php" method="post">
Enter your age: <input name="age" size="2">
</form>
</body>
</html>
<html>
<head></head>
<body>
<?php
// retrieve form data
$age = $_POST['age'];
// check entered value and branch
if ($age >= 21) {
echo 'Come on in, we have alcohol and music awaiting
you!';
}
if ($age < 21) {
echo "You're too young for this club, come back when
you're a little older";
}
?>
</body>
</html>
if (condition) {
do this!
}
else {
do this!
}
This construct can be used to great effect in the last example: we can
combine the two separate if()statements into a single if-else statement.
<html>
<head></head>
<body>
<?php
// retrieve form data
$age = $_POST['age'];
// check entered value and branch
if ($age >= 21) {
echo 'Come on in, we have alcohol and music awaiting
you!';
}
else {
echo "You're too young for this club, come back when
you're a little older";
}
?>
</body>
</html>
Spreading Confusion
If the thought of confusing people who read your code makes you feel warm
and tingly, you're going to love the ternary operator, represented by a
question mark (?). This operator, which lets you make your conditional
statements almost unintelligible, provides shortcut syntax for creating a
single-statement if-else block. So, while you could do this:
<?php
if ($numTries > 10) {
$msg = 'Blocking your account...';
}
else {
$msg = 'Welcome!';
}
?>
You could also do this, which is equivalent (and a lot more fun):
<?php
?>
PHP also lets you "nest" conditional statements inside each other. For
example, this is perfectly valid PHP code:
<?php
if ($day == 'Thursday') {
if ($time == '0800') {
if ($country == 'UK') {
$meal = 'bacon and eggs';
}
}
}
?>
Another, more elegant way to write the above is with a series of logical
operators:
<?php
?>
As you can see, this is simply a form which allows you to pick a day of the
week. The real work is done by the PHP script cooking.php:
<html>
<head></head>
<body>
<?php
// get form selection
$day = $_GET['day'];
// check value and select appropriate item
if ($day == 1) {
$special = 'Chicken in oyster sauce';
}
elseif ($day == 2) {
$special = 'French onion soup';
}
elseif ($day == 3) {
$special = 'Pork chops with mashed potatoes and green
salad';
}
else {
$special = 'Fish and chips';
}
?>
Part 3:
Going Deeper
If you've been paying attention, you remember that in Part Two I gave you a
quick crash course in PHP's basic control structures and operators. I also
showed you how PHP can be used to process the data entered into a Web
form. In this tutorial, I'm going to delve deeper into PHP's operators and
control structures, showing you two new operators, an alternative to the if-
else() family of conditional statements, and some of PHP's more
interesting loops. So keep reading... this is just about to get interesting!
switch (decision-variable) {
case first condition is true:
do this!
case second condition is true:
do this!
... and so on...
}
<html>
<head></head>
<body>
<?php
?>
How do you do this? Simple. All you need to do is assign a name to the form
submit control, and then check whether the special $_POST container
variable contains that name when the script first loads up. If it does, the
form has already been submitted, and you can process the data; if it does
not, that the user has not submitted the form and you therefore need to
generate the initial, unfilled form. Thus, by testing for the presence or
absence of this submit variable, a clever PHP programmer can use a single
PHP script to generate both the initial form, and the output after it has been
submitted, as appropriate.
<html>
<head></head>
<body>
<?php
/* if the "submit" variable does not exist, the form has
not been submitted - display initial page */
if (!isset($_POST['submit'])) {
?>
<?php
}
else {
/* if the "submit" variable exists, the form has been
submitted - look for and process form data */
// display result
$age = $_POST['age'];
if ($age >= 21) {
echo 'Come on in, we have alcohol and music
awaiting you!';
}
else {
echo 'You're too young for this club, come back
when you're a little older';
}
}
?>
</body>
</html>
As you can see, the script contains two pages: the initial, empty form and
the result page generated after hitting the submit button. In order to decide
which page to display, the script first tests for the presence of the
$_POST['submit'] variable. If it doesn't find it, it assumes that the form
has yet to be submitted, and displays the initial list of days. Once the form
has been submitted, the same script will be called to process the form input.
This time, however, the $_POST['submit'] variable will be set, and so PHP
will not display the initial page, but rather the page containing the result
message.
Note that for this to work, your submit button must have a value assigned
to its "name" attribute, and you must check for that value in the primary
conditional statement. And in case you were wondering, the $_SERVER array
is a special PHP variable which always holds server information, including the
path and name of the currently executing script.
Next up, loops.
One by One
For those of you unfamiliar with the term, a loop is a control structure that
enables you to repeat the same set of php
statements or commands over and over again (the actual number of
repetitions can be a number you specify, or depend on the fulfillment of one
or more conditions).
Now, last time out you saw a few comparison and logical operators, which
help in building conditional statements. Since this segment of the tutorial is
going to focus on loops, this is an appropriate time to introduce you to PHP's
auto-increment and auto-decrement operators, which see a lot of use in this
context.
<?php
// define $total as 10
$total = 10;
// increment it
$total++;
// $total is now 11
echo $total;
?>
// define $total as 10
$total = 10;
// decrement it
$total--;
// $total is now 9
echo $total;
?>
These operators are frequently used in loops, to update the value of the loop
counter, speaking of which...
Being Square
The first - and simplest - loop to learn in PHP is the so-called while() loop,
which looks like this:
<html>
<head></head>
<body>
<form action="squares.php" method="POST">
Print all the squares between 1 and <input type="text"
name="limit" size="4" maxlength="4">
<input type="submit" name="submit" value="Go">
</form>
</body>
</html>
This is a simple form which asks the user to enter a number. When the form
is submitted, the PHP script that is invoked should take this number and
print the squares of all the numbers between 1 and the entered value. With
a while() loop, this is simplicity itself:
<html>
<head></head>
<body>
<?php
?>
</body>
</html>
This script uses a while() loop to count forwards from 1 until the values of
$lowerLimit and $upperLimit are equal.
do {
do this!
} while (condition is true)
<?php
$x = 100;
// while loop
while ($x == 700) {
echo "Running...";
break;
}
?>
In this case, no matter how many times you run this PHP script, you will get
no output at all, since the value of $x is not equal to 700. But, if you ran this
version of the script:
<?php
$x = 100;
// do-while loop
do {
echo "Running...";
break;
} while ($x == 700);
?>
you would see one line of output, as the code within the do() block would
run once.
Let's now revise the previous PHP script so that it runs at least once,
regardless of what value is entered into the form:
<html>
<head></head>
<body>
<?php
?>
</body>
</html>
Thus, the construction of the do-while() loop is such that the statements
within the loop are executed first, and the condition to be tested is checked
afterwards. This implies that the statements within the curly braces would be
executed at least once.
Doing it by Numbers
Both the while() and do-while() loops continue to iterate for as long as
the specified conditional expression remains true. But what if you need to
execute a certain set of statements a specific number of times - for example,
printing a series of thirteen sequential numbers, or repeating a particular set
of <td> cells five times? In such cases, clever programmers reach for the
for() loop...
Looks like gibberish? Well, hang in there for a minute...the "counter" here is
a PHP variable that is initialized to a numeric value, and keeps track of the
number of times the loop is executed. Before each execution of the loop, the
"condition" is tested. If it evaluates to true, the loop will execute once more
and the counter will be appropriately incremented; if it evaluates to false,
the loop will be broken and the lines following it will be executed instead.
Here's a simple example that demonstrates how this loop can be used:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
?>
</body>
</html>
The first thing I've done here is define the number to be used for the
multiplication table. I've used 13 here - for no reason other than that it
rhymes with "green".
<html>
<head></head>
<body>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF']; ?>">
Enter number of rows <input name="rows" type="text"
size="4"> and columns <input name="columns" type="text"
size="4"> <input type="submit" name="submit" value="Draw
Table">
</form>
<?php
if (isset($_POST['submit'])) {
echo "<table width = 90% border = '1' cellspacing = '5'
cellpadding = '0'>";
// set variables from form input
$rows = $_POST['rows'];
$columns = $_POST['columns'];
// loop to create rows
for ($r = 1; $r <= $rows; $r++) {
echo "<tr>";
// loop to create columns
for ($c = 1; $c <= $columns;$c++) {
echo "<td> </td> ";
} echo "</tr> ";
}
echo "</table> ";
}
?>
</body>
</html>
As you'll see if you try coding the same thing by hand, PHP's for() loop just
saved you a whole lot of work! And it looks good too - take a look at the
source code of the dynamically generated table, and you'll see that it's nicely
formatted, with line breaks at the end of every table cell and row. This magic
is accomplished by forcing a carriage return with in every call to echo().
Loops are frequently used in combination with one of PHP's more complex
data types, the animal known as the array. That's a whole topic in itself, and
in fact I'm going to discuss it in detail in the next segment of this tutorial.
Then I'm going to show you how arrays, loops and forms all work together
to make the creation of complex Web forms as easy as eating pie. All that
and more in Part Four!
Part 4:
A Big Mistake
Having spent lots of time travelling around the outer landscape of PHP -
learning all about control structures, operators and variables - you're
probably bored. You might even be thinking of dropping out right now, and
instead spending your time more constructively (or so you think) in front of
the idiot box.
That would be a big mistake. And when I say big, I mean humongous.
You see, if you forego this segment of the tutorial for the dubious charms of
Ally McBeal, you're going to miss out on one of PHP's coolest variable types.
It's a little thing called an array, and I'm not exaggerating when I tell you
that once you're on speaking terms with it, you're never going to look at a
PHP script the same way again. But hey, don't take my word for it... toss
that remote aside and come see for yourself!
Fruity Pizza
Thus far, the variables we've discussed contained only a single value, such
as:
<?php
$i = 5;
?>
<?php
// define an array
$pizzaToppings = array('onion', 'tomato', 'cheese',
'anchovies', 'ham', 'pepperoni');
print_r($pizzaToppings);
?>
print_r() is a special function that allows you to take a sneak peek inside
an array. It's more useful for debugging (finding out why your script doesn't
work) than it is for display purposes, but I'll use it here so you can see
what's going on under the surface. You do have your server running and
your browser open, right?
The various elements of the array are accessed via an index number, with
the first element starting at zero. So, to access the element 'onion', you
would use the notation $pizzaToppings[0], while 'anchovies' would be
$pizzaToppings[3] - essentially, the array variable name followed by the
index number enclosed within square braces.
PHP also allows you to replace indices with user-defined "keys", in order to
create a slightly different type of array. Each key is unique, and corresponds
to a single value within the array.
<?php
// define an array
$fruits = array('red' => 'apple', 'yellow' => 'banana',
'purple' => 'plum', 'green' => 'grape');
print_r($fruits);
?>
Eating Italian
The simplest was to define an array variable is the array() function. Here's
how:
<?php
// define an array
$pasta = array('spaghetti', 'penne', 'macaroni');
?>
The rules for choosing an array variable name are the same as those for any
other PHP variable: it must begin with a letter or underscore, and can
optionally be followed by more letters, numbers and underscores.
Alternatively, you can define an array by specifying values for each element
in the index notation, like this:
<?php
// define an array
$pasta[0] = 'spaghetti';
$pasta[1] = 'penne';
$pasta[2] = 'macaroni';
?>
If you're someone who prefers to use keys rather than default numeric
indices, you might prefer the following example:
<?php
// define an array
$menu['breakfast'] = 'bacon and eggs';
$menu['lunch'] = 'roast beef';
$menu['dinner'] = 'lasagna';
?>
You can add elements to the array in a similar manner. For example, if you
wanted to add the element 'green olives' to the $pizzaToppings array,
you would use something like this:
<?php
?>
<?php
// modify an array
$pizzaToppings[4] = 'chicken';
?>
You can do the same using keys. The following statement modifies the
element with the key 'lunch' to a different value:
<?php
// modify an array
$menu['lunch'] = 'steak with mashed potatoes';
?>
<?php
// define an array
$pasta = array('spaghetti', 'penne', 'macaroni');
print_r($pasta);
?>
And you can remove an element from the end of an array using the
interestingly-named array_pop() function.
<?php
// define an array
$pasta = array('spaghetti', 'penne', 'macaroni');
print_r($pasta);
?>
If you need to pop an element off the top of the array, you can use the
array_shift() function:
<?php
// define an array
$pasta = array('spaghetti', 'penne', 'macaroni');
print_r($pasta);
?>
<?php
// define an array
$pasta = array('spaghetti', 'penne', 'macaroni');
print_r($pasta);
?>
print_r($colors);
?>
To do the reverse, you can use the implode() function, which creates a
single string from all the elements of an array by joining them together with
a user-defined delimiter. Reversing the example above, we have:
<?php
// define array
$colors = array ('red', 'blue', 'green', 'yellow');
print $str;
?>
Finally, the two examples below show how the sort() and
rsort()functions can be used to sort an array alphabetically (or
numerically), in ascending and descending order respectively:
<?php
// define an array
$pasta = array('spaghetti', 'penne', 'macaroni');
?>
Retrieving data from an array is pretty simple: all you need to do is access
the appropriate element of the array using its index number. To read an
entire array you simply loop over it, using any of the loop constructs you
learned about in Part Three of this tutorial.
<html>
<head></head>
<body>
My favourite bands are:
<ul>
<?php
// define array
$artists = array('Metallica', 'Evanescence', 'Linkin Park',
'Guns n Roses');
// loop over it and print array elements
for ($x = 0; $x < sizeof($artists); $x++) {
echo '<li>'.$artists[$x];
}
?>
</ul>
</body>
</html>
In this case, I've defined an array, and then used the for() loop to: run
through it, extract the elements using the index notation, and display them
one after the other.
I'll draw your attention here to the sizeof() function. This function is one
of the most important and commonly used array functions. It returns the
size of (read: number of elements within) the array. It is mostly used in loop
counters to ensure that the loop iterates as many times as there are
elements in the array.
If you're using an associative array, the array_keys() and
array_values()functions come in handy, to get a list of all the keys and
values within the array.
<?php
// define an array
$menu = array('breakfast' => 'bacon and eggs', 'lunch' =>
'roast beef', 'dinner' => 'lasagna');
?>
To better understand how this works, consider this rewrite of the previous
example, using the foreach() loop:
<html>
<head></head>
<body>
My favourite bands are:
<ul>
<?php
// define array
$artists = array('Metallica', 'Evanescence', 'Linkin Park',
'Guns n Roses');
// loop over it
// print array elements
foreach ($artists as $a) {
echo '<li>'.$a;
}
?>
</ul>
</body>
</html>
Each time the loop executes, it places the currently-selected array element
in the temporary variable $a. This variable can then be used by the
statements inside the loop block. Since a foreach() loop doesn't need a
counter to keep track of where it is in the array, it is lower-maintenance and
also much easier to read than a standard for() loop. Oh yeah... and it also
works with associative arrays, with no extra programming needed.
<html>
<head></head>
<body>
<?php
// check for submit
if (!isset($_POST['submit'])) {
// and display form
?>
<?php
}
else {
// or display the selected artists
// use a foreach loop to read and display array
elements
if (is_array($_POST['artist'])) {
echo 'You selected: <br />';
foreach ($_POST['artist'] as $a) {
echo "<i>$a</i><br />";
}
}
else {
echo 'Nothing selected';
}
}
?>
</body>
</html>
When the above form is submitted, PHP will automatically create an array
variable, and populate it with the items selected. This array can then be
processed with a foreach() loop, and the selected items retrieved from it.
You can do this with a multi-select list also, simply by using array notation in
the select control's "name" attribute. Try it out for yourself and see... and
make sure you tune in for the next PHP 101 tutorial, same time, same
channel.
Part 5:
Back to School
When you first started reading this series, I promised you that you'd have a
whole lot of fun. If you're the cynical type, you may be feeling that I didn't
keep my promise. After all, how much fun have you really had so far? All
you've done is learn a bunch of theoretical rules, added and subtracted
numbers from each other, learnt primitive decision-making and gone round
and round in the circular funhouse of loops. Heck, if this wasn't a PHP
tutorial, it would be kindergarten...
I hear you.
In this segment of our ongoing saga, I'm going to teach you how to do
something that's definitely not for kids. It involves getting down and dirty
with files on the disk: meeting them (shock!), reading their contents
(shriek!) and (horror of horrors!) writing data to them. All of these exciting
activities will take place under the aegis of PHP's very cool file manipulation
API, which allows you to view and modify file attributes, read and list
directory contents, alter file permissions, retrieve file contents into a variety
of native data structures, and search for files based on specific patterns.
<?php
?>
Run this script through your Web browser, and PHP should return the
contents of the file.
Three different modes are available for use with the fopen() function.
Here's the list:
'r' - opens a file in read mode
Interact with the file via its handle and extract its contents into a
PHP variable
If the fopen() function is successful, it returns a file handle, $fh, which can
be used for further interaction with the file. This file handle is used by the
fread() function, which reads the file and places its contents into a
variable.
You probably haven't see the die() function before, either. This function is
mostly used as a primitive error-handling mechanism. In the event of a fatal
error, such as the file path being invalid or the file permissions being such
that PHP cannot read it, die() terminates script processing and optionally
displays a user-specified error message indicating why it committed suicide.
Different Strokes
An alternative method of reading data from a file is the very cool file()
function, which reads the entire file into an array (remember them?) with
one line of code. Each element of the array then contains one line from the
file. To display the contents of the file, simply iterate over the array in a
foreach() loop and print each element.
<?php
?>
In this example, the file() command opens the file, reads it into an array
and closes the file - all in one, single, elegant movement. Each element of
the array now corresponds to a line from the file. It's easy to print the file's
contents now - just reach for that mainstay of array processing, the
foreach() loop.
<?php
?>
Who am I kidding? I always use the one-line functions noted above instead
of the three-line sequence of fopen(), fread() and fclose(). Laziness
conquers all.
The best way to understand the utility of the include() and require()
functions is with an example. Assume that on your Web site you have a
standard menu bar at the top of every page, and a standard copyright notice
in the bottom. Instead of copying and pasting the header and footer code on
each individual page, PHP gurus simply create separate files for the header
and footer, and import them at the top and bottom of each script. This also
makes a change to the site design easier to implement: instead of manually
editing a gazillion files, you simply edit two, and the changes are reflected
across your entire site instantaneously.
Let's see a real live example of this in action. Create the header in one file,
called header.php:
<html>
<head>
<title><?php echo $page['title'];?></title>
</head>
<body>
<!-- top menu bar -->
<table width="90%" border="0" cellspacing="5"
cellpadding="5">
<tr>
<td><a href="#">Home</a></td>
<td><a href="#">Site Map</a></td>
<td><a href="#">Search</a></td>
<td><a href="#">Help</a></td>
</tr>
</table>
<!-- header ends -->
Next, create the footer with the copyright notice in a second file,
footer.php:
Finally, create a script to display the main content of your site, and
include() the header and footer at appropriate places:
<?php
?>
<?php
?>
Now, when you run the script above, PHP will automatically read in the
header and footer files, merge them with the HTML content, and display the
complete page to you. Simple, isn't it?
Notice that you can even write PHP code inside the files being imported.
When the file is first read in, the parser will look for <?php...?> tags, and
automatically execute the code inside it. (If you're familiar with JavaScript,
you can use this feature to replicate functionality similar to that of the
onLoad() page event handler in JavaScript.)
Writing to Ma
After everything you've just read, you've probably realized that reading a file
is not exactly brain surgery. So let's proceed to something slightly more
difficult - writing to a file.
The steps involved in writing data to a file are almost identical to those
involved in reading it: open the file and obtain a file handle, use the file
handle to write data to it, and close the file. There are two differences: first,
you must fopen() the file in write mode ('w' for write), and second,
instead of using the fread() function to read from the file handle, use the
fwrite() function to write to it. Take a look:
<?php
?>
When you run this script, it should create a file named dump.txt in /tmp,
and write a line of text to it, with a carriage return at the end. Notice that
double quotes are needed to convert into a carriage return.
The fopen(), fwrite() and fread() functions are all binary-safe, which
means you can use them on binary files without worrying about damage to
the file contents. Read more about many of the issues related to binary-safe
file manipulation on different platforms at http://www.php.net/manual/en/
function.fopen.php.
If I've spoiled you by showing you the one-line shortcut functions for file
reads, let me damage you further by introducing you to the
file_put_contents() function, new in PHP 5.0, which takes a string and
writes it to a file in a single line of code.
<?php
?>
Bear in mind that the directory in which you're trying to create the file must
exist before you can write to it. Forgetting this important step is a common
cause of script errors.
Information is Power
PHP also comes with a bunch of functions that allow you to test the status of
a file - for example to find out whether it exists, whether it's empty, whether
it's readable or writable, and whether it's a binary or text file. Of these, the
most commonly used operator is the file_exists() function, which is
used to test for the existence of a specific file.
Here's an example which asks the user to enter the path to a file in a Web
form, and then returns a message displaying whether or not the file exists:
<html>
<head>
</head>
<body>
<?php
// if form has not yet been submitted
// display input box
if (!isset($_POST['file'])) {
?>
<?php
}
// else process form input
else {
// check if file exists
// display appropriate message
if (file_exists($_POST['file'])) {
echo 'File exists!';
}
else {
echo 'File does not exist!';
}
}
?>
</body>
</html>
There are many more such functions. Here's a brief list, followed by an
example that builds on the previous one to provide more information on the
file specified by the user.
<html>
<head>
</head>
<body>
<?php
/* if form has not yet been submitted, display input box */
if (!isset($_POST['file'])) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
Enter file path <input type="text" name="file">
</form>
<?php
}
// else process form input
else {
echo 'File name: <b>'.$_POST['file'] .'</b><br />';
/* check if file exists and display appropriate message
*/
if (file_exists($_POST['file'])) {
// print file size
echo 'File size: '.filesize($_POST['file']).'
bytes<br />';
// print file owner
echo 'File owner:
'.fileowner($_POST['file']).'<br />';
// print file group
echo 'File group:
'.filegroup($_POST['file']).'<br />';
// print file permissions
echo 'File permissions:
'.fileperms($_POST['file']).'<br />';
// print file type
echo 'File type: '.filetype($_POST['file']).'<br /
>';
// print file last access time
echo 'File last accessed on: '.date('Y-m-d',
fileatime($_POST['file'])).'<br />';
// print file last modification time
echo 'File last modified on: '.date('Y-m-d',
filemtime($_POST['file'])).'<br />';
// is it a directory?
if (is_dir($_POST['file'])) {
echo 'File is a directory <br />';
}
// is it a file?
if (is_file($_POST['file'])) {
echo 'File is a regular file <br />';
}
// is it a link?
if (is_link($_POST['file'])) {
echo 'File is a symbolic link <br />';
}
// is it executable?
if (is_executable($_POST['file'])) {
echo 'File is executable <br />';
}
// is it readable?
if (is_readable($_POST['file'])) {
echo 'File is readable <br />';
}
// is it writable?
if (is_writable($_POST['file'])) {
echo 'File is writable <br />';
}
}
else {
echo 'File does not exist! <br />';
}
}
?>
</body>
</html>
Breaking Eggs
So now you know how to read a file, write to it, and test its status. Let's look
at some examples of what you can do with this new-found power.
SPANISH OMELETTE
INGREDIENTS:
- 1 chopped onion
- 1 chopped tomato
- 1/2 chopped green pepper
- 4 beaten eggs
- Salt and pepper to taste
METHOD:
1. Fry onions in a pan
2. Pour beaten eggs over onions and fry gently
3. Add tomatoes, green pepper, salt and pepper to taste
4. Serve with toast or bread
I need a quick way to convert them all into HTML so that they look
presentable on my Web site. We've already established that I'm lazy, so
fuggedaboutme re-creating the recipes in HTML. Instead, I'll have PHP do
the heavy lifting for me:
<html>
<head></head>
<body>
<?php
// read recipe file into array
$data = file('/usr/local/stuff/that/should/be/elsewhere/
omelette.txt') or die('Could not read file!');
/* first line contains title: read it into variable */
$title = $data[0];
// remove first line from array
array_shift($data);
?>
<?php
/* iterate over content and print it */
foreach ($data as $line) {
echo nl2br($line);
}
?>
</body>
</html>
I've used the file() function to read the recipe into an array, and assign
the first line (the title) to a variable. That title is then printed at the top of
the page. Since the rest of the data is fairly presentable as is, I can simply
print the lines to the screen one after the other. Line breaks are
automatically handled for me by the extremely cool nl2br() function, which
converts regular text linebreaks into the HTML equivalent, the <br /> tag.
The end result: an HTML-ized version of my recipe that the world can marvel
at. Take a look:
<html>
<head></head><body>
<h2>SPANISH OMELETTE
</h2>
INGREDIENTS:<br />
- 1 chopped onion<br />
- 1 chopped tomato<br />
- 1/2 chopped green pepper<br />
- 4 beaten eggs<br />
</body>
</html>
If the elegance and creative simplicity of my Spanish omelette recipe has left
you speechless, I'm not surprised - many people feel that way. Until you get
your voice back: Ciao... and make sure you come back to work through Part
Six of PHP 101, which discusses creating your own reusable functions.
Part 6:
A Little Knowledge
If you've been taking your regular dose of PHP 101, you know now enough
about PHP to write simple programs of your own. However, these programs
will be "procedural" or linear - the statements in them will be executed
sequentially, one after another - simply because that's the only programming
style I've used so far.
You know what they say about a little knowledge being a dangerous thing...
as your PHP scripts become more and more complex, it's only a matter of
time before you bump your head against the constraints of the procedural
method, and begin looking for a more efficient way of structuring your PHP
programs.
That's where Part Six of PHP 101 comes in. In this tutorial I'm going to
introduce you to a new way of doing things, where code doesn't run in a
straight line, but twists, leaps and bounds across the landscape of your
script. Most of this activity is accomplished through a programming construct
called a "function", and this tutorial teaches you how to build them (once),
use them (many times), pass them arguments and have them return values,
and generally make your scripts more compact, efficient and maintainable.
In Plain English
Ask a geek to define the term "function", and he'll probably mumble
something about a function being "a block of statements that can be
grouped together as a named entity." Since this is a tutorial on PHP, not an
introductory course in Greek, I'll translate that for you: a function is simply a
set of program statements which perform a specific task, and which can be
"called", or executed, from anywhere in your program.
Every programming language comes with its own built-in functions, and
typically also allows developers to define their own functions. For example, if
I had a profit statement for the year on my desk, and I wanted to inflate
each number by 35%, I could call my neighborhood accounting firm and get
them to do it for me... or I could write a simple PHP function called
cheatTheShareholders() and have it do the work for me (it's faster, plus
PHP doesn't bill by the hour).
There are three important reasons why functions are a Good Thing™. First:
user-defined functions allow you to separate your code into easily
identifiable subsections - which are easier to understand and debug. Second:
functions make your program modular, allowing you to write a piece of code
once and then re-use it multiple times within the same program. And third:
functions simplify code updates or changes, because the change needs only
to be implemented in a single place (the function definition). Functions thus
save time, money and electrons... and I know the electrons at least will
thank you!
<?php
// define a function
function myStandardResponse() {
echo "Get lost, jerk!<br /><br />";
}
// on the bus
echo "Hey lady, can you spare a dime? <br />";
myStandardResponse();
// at the office
echo "Can you handle Joe's workload, in addition to your
own, while he's in Tahiti for a month? You'll probably need
to come in early and work till midnight, but we are
confident you can handle it. Oh, and we can't pay you extra
because of budgetary constraints...<br />";
myStandardResponse();
// at the party
echo "Hi, haven't I seen you somewhere before?<br />";
myStandardResponse();
?>
The first thing I've done in the script above is define a new function, with the
function keyword. This keyword is followed by the name of the function,
which in this case is myStandardResponse(). All the program code
attached to that function is then placed within a pair of curly braces - and
this program code can contain loops, conditional statements, calls to other
user-defined functions, or calls to other PHP functions.
Of course, defining a function is only half of the puzzle; for it to be of any
use at all, you need to "invoke" it. In PHP, as in a million other languages,
this is accomplished by calling the function by its name, as I've done in the
example above. Calling a user-defined function is identical to calling a built-
in PHP function like echo() or explode().
Here's the typical format for a function:
function function_name (optional function arguments) {
statement 1...
statement 2...
.
.
.
statement n...
}
Enter arguments.
To see how this works, look at the following function, which accepts a single
argument and then prints it back after a calculation:
<?php
// define a function
function getCircumference($radius) {
echo "Circumference of a circle with radius $radius is
".sprintf("%4.2f", (2 * $radius * pi()))."<br />";
}
// call a function with an argument
getCircumference(10);
?>
It's also possible to pass more than one argument to a function. This is done
using a comma-separated list, as the following example shows:
<?php
// define a function
function changeCase($str, $flag) {
/* check the flag variable and branch the code */
switch($flag) {
case 'U':
print strtoupper($str)."<br />";
break;
case 'L':
print strtolower($str)."<br />";
break;
default:
print $str."<br />";
break;
}
}
?>
Here, depending on the value of the second argument, program flow within
the function moves to the appropriate branch and manipulates the first
argument.
Note that there is no requirement to specify the data type of the argument
being passed to a function. Since PHP is a dynamically-typed language, it
automatically identifies the variable type and acts on it appropriately.
<?php
// define a function
function getCircumference($radius) {
// return value
return (2 * $radius * pi());
}
You can even use the result of a function inside another function, as
illustrated in this minor revision of the example above:
<?php
// define a function
function getCircumference($radius) {
// return value
return (2 * $radius * pi());
}
?>
Return values need not be numbers or strings alone: a function can just as
easily return an array (remember them?), as demonstrated in the following
example:
<?php
?>
test@test.com
a@x.com
zooman@deeply.bored.org
b@x.com
guess.me@where.ami.net
testmore@test.com
the output of the script above would look like this:
test.com, x.com, deeply.bored.org, where.ami.net,
Note that the return statement terminates program execution inside a
function.
Marching Order
The order in which arguments are passed to a function can be important.
The following example requires that the name is passed as the first
argument, and the place as the second.
<?php
// define a function
function introduce($name, $place) {
print "Hello, I am $name from $place";
}
// call function
introduce("Moonface", "The Faraway Tree");
?>
<?php
// define a function
function introduce($name="John Doe", $place="London") {
print "Hello, I am $name from $place";
}
// call function
introduce("Moonface");
?>
Notice that the function has been called with only a single argument, even
though the function definition requires two. However, since default values
are present for each argument in the function, the missing argument is
replaced by the default value for that argument, and no error is generated.
<?php
// define a function
function someFunc() {
// get the arguments
$args = func_get_args();
?>
<?php
// define a function
function someFunc() {
// get the number of arguments passed
$numArgs = func_num_args();
?>
Going Global
Let's now talk a little bit about the variables used within a function, and their
relationship with variables in the outside world. Usually, the variables used
within a function are "local" - meaning that the values assigned to them, and
the changes made to them, are restricted to the function space alone.
<?php
// define a function
function getDay() {
// define a variable inside the function
$today = "Saturday";
// print the variable
print "It is $today inside the function<br />";
}
?>
When you run this script, here is what you will see:
The reverse is also true: variables defined inside a function cannot be "seen"
outside it. To illustrate, take a look at the next example and its output (or
the lack of it):
<?php
// define a function
function getDay() {
// define a variable inside the function
$today = "Saturday";
}
getDay();
print "Today is $today";
?>
Today is
Depending on the error_reporting you have set up in php.ini, you might also
see an error message:
Notice: Undefined variable: today in x1.php on line 10
Here is a rewrite of the earlier example, this time declaring the $today
variable global:
<?php
// define a function
function getDay() {
// make the variable global
global $today;
PHP also comes with so-called superglobal variables - variables that are
always available, regardless of whether you're inside a function or outside it.
You've already seen some of these special variables in action: the
$_SERVER, $_POST and $_GET variables are all superglobals, which is why
you can access things like the currently-executing script's name or form
values even inside a function.
Superglobals are a Good Thing™, because they're always there when you
need them, and you don't need to jump through any hoops to use the data
stored inside them. Read more about superglobals and variable scope at
http://www.php.net/manual/en/language.variables.predefined.php and
http://www.php.net/manual/en/language.variables.scope.php.
Checking References
Any discussion about variables in and out of functions would be incomplete
without some mention of the difference between "passing by reference" and
"passing by value". So far, all the examples you've seen have involved
passing arguments to a function "by value" - meaning that a copy of the
variable was passed to the function, while the original variable remained
untouched. However, PHP also allows you to pass "by reference" - meaning
that instead of passing a value to a function, you pass a reference to the
original variable, and have the function act on that instead of a copy.
// create a variable
$today = "Saturday";
// call function
setDay($today);
?>
You've already seen this before, and you already know what the output is
going to say:
// create a variable
$today = "Saturday";
// call function
setDay($today);
?>
Notice the ampersand (&) before the argument in the function definition.
This tells PHP to use the variable reference instead of the variable value.
When such a reference is passed to a function, the code inside the function
acts on the reference, and modifies the content of the original variable
(which the reference is pointing to) rather than a copy. If you then try
retrieving the value of the original variable outside the function, it returns
the modified value:
.
It is Tuesday inside the function
It is Tuesday outside the function
Now you understand why I said no discussion about variables would be
complete without mentioning the two ways of passing variables. This, of
course, is what the global keyword does inside a function: use a reference
to ensure that changes to the variable inside the function also reflect outside
it. The PHP manual puts it best when it says "...when you declare a variable
as global $var you are in fact creating a reference to a global variable".
For more examples, read all about references at http://www.zend.com/
manual/language.references.php.
And that just about concludes this tutorial. This time you've taken a big step
towards better software design by learning how to abstract parts of your PHP
code into reusable functions. You now know how to add flexibility to your
functions by allowing them to accept different arguments, and how to obtain
one (or more) return values from them. Finally, you've learned a little bit
about how PHP treats variables inside and outside functions.
In Part Seven, I'll be showing you how to group related functions together
into classes, and also telling you all about the cool new features in the PHP 5
object model. You definitely don't want to miss that one!
Part 7.1:
Alphabet Soup
So now you know how to create your own functions in PHP, and you've spent
the last few days busily inspecting your applications and turning repeated
code fragments into functions. But functions are just the tip of the software
abstraction iceberg. Lurking underneath is a three-letter acronym that
strikes fear into the hearts of most newbie programmers.
OOP.
If you've been programming for a while, you've probably heard the term
OOP before - it stands for Object Oriented Programming, and refers to a
technique whereby you create program "objects" and then use these objects
to build the functionality you need into your program. PHP 5 is very big on
OOP - it comes with a brand-spanking-new object model which finally brings
PHP objects into conformance with standard OOP principles and offers OO
programmers a whole bunch of new goodies to play with.
Wondering how you can get in on this? Well, wonder no more. Your prayers
have been answered.
Over the course of this tutorial, I'm going to take a brief look at PHP's OO
capabilities (both PHP 4 and PHP 5), together with examples and
explanations to demonstrate just how powerful it really is. I'll be covering
most of the basics - classes, objects, attributes and methods - and a couple
of more advanced concepts - constructors, destructors, private methods and
properties, and inheritance. And if you're new to object-oriented
programming, or just apprehensive about what lies ahead, don't worry - I
promise this will be a lot less painful than you think. And unlike dentists, I
don't lie.
Back To Class
Before beginning, though, let's make sure that you have a clear idea of the
concepts involved here.
Once a class has been defined, PHP allows you to spawn as many instances
of the class as you like. Each of these instances is a completely independent
object, with its own properties and methods, and can therefore be
manipulated independently of other objects. This comes in handy in
situations where you need to spawn more than one instance of an object -
for example, two simultaneous database links for two simultaneous queries,
or two shopping carts.
Classes also help you keep your code modular - you can define a class in a
separate file, and include that file only in the scripts where you plan to use
the class - and simplify code changes, since you only need to edit a single
file to add new functionality to all your spawned objects.
Animal Antics
To understand this better, pick an animal, any animal. I pick the bear,
because I like bears. Now ask yourself, can you consider this bear, within the
framework of OOP, as an "object"?
Why not? After all, every bear has certain characteristics - age, weight, sex -
which are equivalent to object properties. And every bear can perform
certain activities - eat, sleep, walk, run, mate - all of which are equivalent to
object methods.
Let's take it a little further. Since all bears share certain characteristics, it is
possible to conceive of a template Bear(), which defines the basic
characteristics and abilities of every bear on the planet. Once this Bear()
("class") is used to create a new $bear ("object"), the individual
characteristics of the newly-created Bear can be manipulated independently
of other Bears that may be created from the template.
Now, if you sat down to code this class in PHP 5, it would probably look
something like this:
<?php
// PHP 5
// class definition
class Bear {
// define properties
public $name;
public $weight;
public $age;
public $sex;
public $colour;
// define methods
public function eat() {
echo $this->name." is eating... ";
}
public function run() {
echo $this->name." is running... ";
}
?>
Given this class, it's now simple to spawn as many Bears as you like, and
adjust the individual properties of each. Take a look:
<?php
// my first bear
$daddy = new Bear;
// give him a name
$daddy->name = "Daddy Bear";
// how old is he
$daddy->age = 8;
// what sex is he
$daddy->sex = "male";
// what colour is his coat
$daddy->colour = "black";
// how much does he weigh
$daddy->weight = 300;
// mommy eats it
$mommy->eat();
// and so does baby
$baby->eat();
// mommy sleeps
$mommy->sleep();
// and so does daddy
$daddy->sleep();
?>
As the illustration above shows, once new objects are defined, their
individual methods and variables can be accessed and modified independent
of each other. This comes in very handy, as the rest of this tutorial will show.
Going Deeper
Now that you've got the concepts straight, let's take a look at the nitty-gritty
of a class definition.
<?php
// PHP 5
// class definition
class Bear {
// more properties
// more methods
}
?>
Every class definition begins with the keyword class, followed by a class
name. You can give your class any name that strikes your fancy, so long as it
doesn't collide with a reserved PHP word. A pair of curly braces encloses all
class variables and functions, which are written as you would normally code
them.
PHP 5 also introduces the concept of visibility to the object model. Visibility
controls the extent to which object properties and methods can be
manipulated by the caller, and plays an important role in defining how open
or closed your class is. Three levels of visibility exist, ranging from most
visible to least visible: public, private and protected. Within the class
definition, you can mark the visibility of a property or method by preceding it
with one of the keywords - public, private, or protected .
By default, class methods and properties are public; this allows the calling
script to reach inside your object instances and manipulate them directly. If
you don't like the thought of this intrusion, you can mark a particular
property or method as private or protected, depending on how much
control you want to cede over the object's internals (more on this shortly).
Since the PHP 4 object model does not include support for visibility, the class
definition above would not work in PHP 4. Instead, you would need to use
the following:
<?php
// PHP 4
// class definition
class Bear {
// define properties
var $name;
var $weight;
var $age;
var $sex;
var $colour;
// define methods
function eat() {
echo $this->name." is eating... ";
}
function run() {
echo $this->name." is running... ";
}
function kill() {
echo $this->name." is killing prey... ";
}
function sleep() {
echo $this->name." is sleeping... ";
}
}
?>
From the above, it should be clear that class properties and methods in PHP
4 are always public ...and there ain't nuttin' you can do about that!
In order to create a new instance of a class, you use the new keyword to
assign the newly created object to a PHP variable.
<?php
?>
In English, the above would mean "create a new object of class Bear() and
assign it to the variable $daddy ".
You can now access all the methods and properties of the class via this
variable. For example, the code
<?php
?>
would mean "assign the value Daddy Bear to the variable $name of this
specific instance of the class Bear()", while the statement
<?php
$daddy->sleep();
?>
would mean "execute the function sleep() for this specific instance of the
class Bear()".
Note the -> symbol used to connect objects to their properties or methods,
and the fact that the $ symbol is omitted when accessing properties of a
class instance.
<?php
// PHP 5
// class definition
class Bear {
// define properties
public $name;
public $weight;
// define methods
public function eat($units) {
echo $this->name." is eating ".$units." units of
food... ";
$this->weight += $units;
}
}
?>
In this case, the $this prefix indicates that the variable to be modified
exists within the class - or, in English, "add the argument provided to eat()
to the variable $weight within this object". The $this prefix thus provides
a convenient way to access variables and functions which are "local" to the
class.
<?php
// create instance
$baby = new Bear;
$baby->name = "Baby Bear";
$baby->weight = 1000;
// retrieve properties
echo $baby->name." weighs ".$baby->weight." units ";
echo $brother->name." weighs ".$brother->weight." units ";
// call eat()
$baby->eat(100);
$baby->eat(50);
$brother->eat(11);
// retrieve new values
echo $baby->name." now weighs ".$baby->weight." units ";
echo $brother->name." now weighs ".$brother->weight." units
";
?>
Under Construction
It's also possible to automatically execute a function when the class is called
to create a new object. This is referred to in geek lingo as a constructor
and, in order to use it, your PHP 5 class definition must contain a special
function, __construct().
For example, if you'd like all newly born bears to be brown and weigh 100
units, you could add this to your class definition:
<?php
// PHP 5
// class definition
class Bear {
// define properties
public $name;
public $weight;
public $age;
public $colour;
// constructor
public function __construct() {
$this->age = 0;
$this->weight = 100;
$this->colour = "brown";
}
// define methods
}
?>
In PHP 4, your constructor must have the same name as the class. Here's
the equivalent code for PHP 4:
<?php
// PHP 4
// class definition
class Bear {
// define properties
var $name;
var $weight;
var $age;
var $colour;
// constructor
function Bear() {
$this->age = 0;
$this->weight = 100;
$this->colour = "brown";
}
// define methods
}
?>
<?php
// create instance
$baby = new Bear;
$baby->name = "Baby Bear";
echo $baby->name." is ".$baby->colour." and weighs ".$baby-
>weight." units at birth";
?>
Hands Off
As noted previously, PHP 5 makes it possible to mark class properties and
methods as private, which means that they cannot be manipulated or
viewed outside the class definition. This is useful to protect the inner
workings of your class from manipulation by object instances. Consider the
following example, which illustrates this by adding a new private variable,
$_lastUnitsConsumed, to the Bear() class:
<?php
// PHP 5
// class definition
class Bear {
// define properties
public $name;
public $age;
public $weight;
private $_lastUnitsConsumed;
// constructor
public function __construct() {
$this->age = 0;
$this->weight = 100;
$this->_lastUnitsConsumed = 0;
}
// define methods
public function eat($units) {
echo $this->name." is eating ".$units." units of
food... ";
$this->weight += $units;
$this->_lastUnitsConsumed = $units;
}
?>
?>
In a similar way, class methods can also be marked as private - try it out
for yourself and see.
Extending Yourself
Two of the best things about OOP, whether in PHP 4 or in PHP 5, are
extensibility and inheritance. Very simply, this means that you can create a
new class based on an existing class, add new features (read: properties and
methods) to it, and then create objects based on this new class. These
objects will contain all the features of the original parent class, together with
the new features of the child class.
<?php
// PHP 5
// class definition
class Bear {
// define properties
public $name;
public $weight;
public $age;
public $sex;
public $colour;
// constructor
public function __construct() {
$this->age = 0;
$this->weight = 100;
}
// define methods
public function eat($units) {
echo $this->name." is eating ".$units." units of
food... ";
$this->weight += $units;
}
// constructor
public function __construct() {
parent::__construct();
$this->colour = "white";
$this->weight = 600;
}
// define methods
public function swim() {
echo $this->name." is swimming... ";
}
}
?>
The extends keyword is used to extend a parent class to a child class. All
the functions and variables of the parent class immediately become available
to the child class. This is clearly visible in the following code snippet:
<?php
In this case, the final call to $tom->swim() will fail and cause an error,
because the Bear() class does not contain a swim() method. However,
none of the calls to $bob->run() or $bob->kill() will fail, because as a
child of the Bear() class, PolarBear() inherits all the methods and
properties of its parent.
Note how the parent class constructor has been called in the PolarBear()
child class constructor - it's a good idea to do this so that all necessary
initialization of the parent class is carried out when a child class is
instantiated. Child-specific initialization can then be done in the child class
constructor. Only if a child class does not have a constructor, is the parent
class constructor automatically called.
You can do this in PHP 4, too. Here's a PHP 4 version of the PolarBear class
definition:
<?php
// PHP 4
// constructor
function PolarBear() {
parent::Bear();
$this->colour = "white";
$this->weight = 600;
}
// define methods
function swim() {
echo $this->name." is swimming... ";
}
}
?>
To prevent a class or its methods from being inherited, use the final
keyword before the class or method name (this is new in PHP 5 and will not
work in older versions of PHP). Here's an example, which renders the
Bear() class un-inheritable (if that's actually a word):
<?php
// PHP 5
// class definition
final class Bear {
// define properties
// define methods
}
?>
<?php
// PHP 5
// class definition
class Bear {
// define properties
public $name;
public $weight;
public $age;
public $sex;
public $colour;
// constructor
public function __construct() {
$this->age = 0;
$this->weight = 100;
$this->colour = "brown";
}
// destructor
public function __destruct() {
echo $this->name." is dead. He was ".$this->age."
years old and ".$this->weight." units heavy. Rest in peace!
";
}
// define methods
public function eat($units) {
echo $this->name." is eating ".$units." units of
food... ";
$this->weight += $units;
}
?>
Here, once the script ends, no reference will exist for $daddy, and so the
destructor will be called automatically. The output would look like this:
<?php
// PHP 5
// base class
class Bear {
public $name;
public $weight;
// constructor
public function __construct() {
}
// define methods
public function eat() {
}
// derived class
class GrizzlyBear extends Bear {
public function kill() {
}
}
?>
And now consider the following script, which uses get_class() and
get_parent_class() to retrieve the class name from an instance:
<?php
?>
You can view all the properties exposed by a class with get_class_vars(),
and all its methods with get_class_methods() function. To view
properties of the specific object instance, use get_object_vars() instead
of get_class_vars(). Here is an example:
<?php
// create instance
$joe = new GrizzlyBear;
$joe->name = "Joe Bear";
$joe->weight = 1000;
?>
Class properties:
Array
(
[name] =>
[weight] =>
)
Class methods:
Array
(
[0] => kill
[1] => __construct
[2] => eat
[3] => run
[4] => sleep
)
Instance properties:
Array
(
[name] => Joe Bear
[weight] => 1000
)
Access Denied
And now that you know the basics of how objects work in PHP, let's wrap this
up with a real-world example. Consider the following userAuth() class, which
exposes methods to validate a user login using an encrypted password file
such as /etc/passwd or .htaccess, both of which are used on Unix systems
(i.e. most of the Internet). I'll assume here that the passwords in the
password file are encrypted with MD5, and use a 12-character salt beginning
with $1$:
<?php
// PHP 5
// class definition
class userAuth {
// define properties
public $username;
private $passwd;
private $passwdFile;
private $_resultCode;
// constructor
// must be passed username and password
public function __construct($username, $password) {
$this->username = $username;
$this->passwd = $password;
$this->_resultCode = -1;
}
// used to set file to read for password data
public function setPasswdFile($file) {
$this->passwdFile = $file;
}
?>
Most of this should be clear to you from the examples in previous pages. In
case it isn't, the following script should help you understand what's
happening:
<?php
// create instance
$ua = new userAuth("joe", "secret");
// set password file
$ua->setPasswdFile("passwd.txt");
// perform authentication
$ua->authenticateUser();
// check result code and display message
switch ($ua->getResultCode()) {
case -1:
echo "Could not find your user account";
break;
case 0:
echo "Your password was incorrect";
break;
case 1:
echo "Welcome, ".$ua->username;
break;
}
?>
There's a lot more you can do with objects, especially in PHP 5; I've
restrained myself here because I didn't want to confuse you too much with
talk of overloading, abstract classes and static methods. If you're interested,
however, drop by http://www.php.net/manual/en/language.oop.php for
more. And make sure you come back for Part Eight of PHP 101, because I'm
going to show you how to hook your scripts up to a MySQL database.
Part 8.1:
Mix and Match
One of the most compelling things PHP has going for it is its support for a
variety of database management systems, including MySQL, PostgreSQL,
Oracle and Microsoft Access. By virtue of this support, PHP developers can
create sophisticated data-driven Web applications at a fraction of the time
and cost required by competing alternatives. And nowhere is this more clear
than in PHP's longtime support of MySQL, the very fast, very reliable and
very feature-rich open-source RDBMS.
By using PHP and MySQL together, developers can benefit from huge savings
on the licensing costs of commercial alternatives, and also leverage off the
tremendous amount of thought PHP and MySQL developers have put into
making sure that the two packages work together seamlessly and smoothly.
And since both PHP and MySQL are open-source projects, when you use the
two of them together you know you're getting the most up-to-date
technology available. And that's always a good thought to go to bed with.
In this issue of PHP 101, I'm going to show you how to use PHP to extract
data from a database, and use that data to dynamically build a Web page. In
order to try out the examples in this tutorial, you'll need a working MySQL
installation, which you can obtain from the MySQL Web site at http://
www.mysql.com/. If you have some knowledge of SQL (Structured Query
Language, the language used to interact with a database server) you'll find it
helpful, but it's not essential.
Building Blocks
In order to use MySQL and PHP together, your PHP build must include
support for MySQL. On UNIX, this is accomplished by adding the --with-
mysql option to the configure script when building PHP on UNIX, and
pointing PHP to the MySQL client libraries. On Windows, the MySQL client
libraries are built in to PHP 4 and activated by default. In PHP 5, pre-built .dll
files are included with the Windows distribution. Read more about this at
http://www.php.net/manual/en/ref.mysql.php.
Unix users should note that PHP 4 ships with a set of MySQL client libraries,
which are activated by default; however, PHP 5 no longer bundles these
libraries due to licensing issues, so you need to obtain, install and activate
them yourself. They're included with the MySQL distribution, and are
installed automatically when you install MySQL. To activate the MySQL
extension, ext/mysql, add the --with-mysql option to PHP's configure
script. For more information on this change, read http://www.php.net/
manual/en/faq.databases.php#faq.databases.mysql.php5.
And finally (as if all that wasn't quite confusing enough) PHP 5 also comes
with a new MySQL extension, called ext/mysqli (MySQL Improved). You
can use this new extension to access the new features in MySQL 4.1.2 or
better, and to gain the benefits of improved speed and security. To activate
this extension on UNIX, add the --with-mysqli option to PHP's
configure script, and point PHP to the mysql_config program that comes
with MySQL 4.1 and above. For Windows users, a pre-built version of ext/
mysqli is included in the win32 PHP distribution. Read more about this at
http://www.php.net/manual/en/ref.mysqli.php.
To figure out which extension you need, use the following rule of thumb:
In case you were wondering, this tutorial covers both ext/mysql and ext/
mysqli, so you actually get two for the price of one. Keep reading, and let
me introduce you to MySQL.
Animal Magnetism
Every MySQL database is composed of one or more tables. These tables,
which structure data into rows and columns, are what lend organization to
the data.
+----+-----------+----------+
| id | country | animal |
+----+-----------+----------+
| 1 | America | eagle |
| 2 | China | dragon |
| 3 | England | lion |
| 4 | India | tiger |
| 5 | Australia | kangaroo |
| 6 | Norway | elk |
+----+-----------+----------+
As you can see, a table divides data into rows, with a new entry (or record)
on every row. The data in each row is further broken down into cells (or
fields), each of which contains a value for a particular attribute of the data.
For example, if you consider the record for the country "India", you'll see
that the record is clearly divided into separate fields for record number,
country name and national animal.
The rows within a table are not arranged in any particular order - they can
be sorted alphabetically, by number, by name, or by any other criteria you
choose to specify. It is therefore necessary to have some method of
identifying a specific record in a table. In the example above, each record is
identified by a unique number; this unique field is referred to as the primary
key for that table.
You use the Structured Query Language, SQL, to interact with the MySQL
server and tell it to create a table, mark a field as primary, insert records,
edit records, retrieve records... basically, anything that involves
manipulating the data or the database. To see how this works, examine the
following SQL, which creates the table above:
CREATE DATABASE testdb;
Once the data has been imported, run a quick SELECT query to check that
everything is working as it should be:
Hello Database!
Now, let's use PHP to do exactly the same thing. You could use PHP to set up
the database from the start, but as ours already exists we'll simply fire a
SELECT query at the database 'testdb', and display the results in an HTML
page:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// open connection
$connection = mysql_connect($host, $user, $pass) or die
("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM symbols";
// execute query
$result = mysql_query($query) or die ("Error in query:
$query. ".mysql_error());
// close connection
mysql_close($connection);
?>
</body>
</html>
$host = "localhost";
$user = "test";
$pass = "test";
$db = "testdb";
?>
2. To begin communication with a MySQL database server, you need to
open a connection to that server. All communication between PHP and
the database server takes place through this connection. In order to
initialize this connection, PHP offers the mysql_connect() function:
<?php
?>
This function must be passed the name of the database to be used for
all subsequent queries. An optional second argument here is the link
identifier; if no identifier is specified, the last opened link is assumed.
If you have two or more database connections open simultaneously,
it's a good idea to specify the link identifier as the second argument to
mysql_select_db() - and indeed to all other mysql_* functions in
the script, so that PHP doesn't get confused about which connection to
use where.
4. The next step is to create the query and execute it. This is
accomplished with the mysql_query() function. <?php
?>
This function also needs two parameters: the query string and the link
identifier for the connection. Again, if no link identifier is specified, the
last opened link is used. Depending on whether or not the query was
successful, the function returns true or false; a failure can be caught
via the ...or die() clause of the statement, and the
mysql_error() function can be used to display the corresponding
error message.
5. If mysql_query() is successful, the result set returned by the query
is stored in the variable $result. This result set may contain one or
more rows or columns of data, depending on your query. You can
retrieve specific subsets of the result set with different PHP functions,
including the one used here - the mysql_fetch_row() function -
which fetches a single row of data as an array called $row. Fields in
that row can then be accessed using standard PHP array notation.
Each time you call mysql_fetch_row(), the next record in the result
set is returned. This makes mysql_fetch_row() very suitable for use
in a while() or for() loop.
<?php
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
}
}
?>
Notice that the call to mysql_fetch_row() is wrapped in a
conditional test, which first checks to see if any rows were returned at
all. This information is provided by the mysql_num_rows() function,
which contains the number of rows returned by the query. Obviously,
you can only use this function with queries that return data, like
SELECT or SHOW.It is not appropriate for use with INSERT, UPDATE,
DELETE or similar queries.
There are several other alternatives to mysql_fetch_row(), which
will be explained a little later.
6. Finally, since each result set returned after a query occupies memory,
it's a good idea to use the mysql_free_result() function to free up
the used memory. After the result set is freed, if no further queries are
to be run, you can close the connection to the MySQL server with
mysql_close(). <?php
mysql_free_result($result);
mysql_close($connection);
?>
Different Strokes...
You can also use PHP's mysql_fetch_row() and list() functions to
obtain a simple array of values, and then assign these values to different
variables - a variation of the technique in the previous section. Take a look
(only the while() loop changes):
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// open connection
$connection = mysql_connect($host, $user, $pass) or die
("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM symbols";
// execute query
$result = mysql_query($query) or die ("Error in query:
$query. ".mysql_error());
// close connection
mysql_close($connection);
?>
</body>
</html>
In this case, the list() function is used to assign different elements of the
result set to PHP variables, which are then used when rendering the page.
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// open connection
$connection = mysql_connect($host, $user, $pass) or die
("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM symbols";
// execute query
$result = mysql_query($query) or die ("Error in query:
$query. ".mysql_error());
// close connection
mysql_close($connection);
?>
</body>
</html>
Notice that in this case, field values are accessed using the field name
instead of the index.
Of all the alternatives, however, the function I like the most is the
mysql_fetch_object() function, which returns each row as an object
(remember them from Part Seven?) with properties corresponding to the
field names:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// open connection
$connection = mysql_connect($host, $user, $pass) or die
("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "SELECT * FROM symbols";
// execute query
$result = mysql_query($query) or die ("Error in query:
$query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
while($row = mysql_fetch_object($result)) {
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->country."</td>";
echo "<td>".$row->animal."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// close connection
mysql_close($connection);
?>
</body>
</html>
Here, each $row object is created with properties corresponding to the field
names in that row. Row values can thus be accessed using standard
object->property notation.
If you're the type that likes to have your cake and eat it too, you will
probably enjoy the mysql_fetch_array() function, which returns both an
associative array and a numerically-indexed array, a combination of the
mysql_fetch_row() and mysql_fetch_assoc() functions. Read about it
at http://www.php.net/manual/en/function.mysql-fetch-array.php.
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// open connection
$connection = mysqli_connect($host, $user, $pass, $db) or
die ("Unable to connect!");
// create query
$query = "SELECT * FROM symbols";
// execute query
$result = mysqli_query($connection, $query) or die ("Error
in query: $query. ".mysqli_error());
// close connection
mysqli_close($connection);
?>
</body>
</html>
As you can see, this looks a lot like the code written for ext/mysql. The
only real difference - at least to the naked eye - is the fact that function
names now begin with mysqli_* instead of mysql_*. Of course, there are a
whole bunch of differences under the hood: ext/mysqli is faster, more
secure and more powerful than regular ext/mysql, and also includes
support for prepared statements, bound result sets, multiple simultaneous
queries, transactions and a whole bunch of other cool stuff.
You can also use ext/mysqli in an object-oriented way, where each task -
connecting, querying, fetching - is actually a method of the mysqli()
object:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// create query
$query = "SELECT * FROM symbols";
// execute query
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
?>
</body>
</html>
Here, the new keyword is used to instantiate an object of class mysqli, and
pass the object constructor connection information (including the database
name). The resulting object, stored in the variable $mysqli, then exposes
methods and properties to perform the tasks of querying, fetching and
processing rows, and handling errors.
If you look closely at the two scripts above, you'll notice the numerous
similarities between the function and method names, and the structure of
the script. Of the two, though, the object-oriented method is recommended,
especially in light of the new object model in PHP 5.
Part 8.2:
Surgical Insertion
So now you know how to execute a SELECT query to retrieve a result set
from the database. However, you can also use PHP's MySQL API for queries
that don't return a result set - for example, an INSERT or UPDATE query.
Consider the following example, which demonstrates this by asking for user
input through a form and then INSERT-ing that data into the database:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>
<?php
}
else {
// form submitted
// set server access variables
$host = "localhost";
$user = "test";
$pass = "test";
$db = "testdb";
// open connection
$connection = mysql_connect($host, $user, $pass) or die
("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select
database!");
// create query
$query = "INSERT INTO symbols (country, animal) VALUES
('$country', '$animal')";
// execute query
$result = mysql_query($query) or die ("Error in query:
$query. ".mysql_error());
// close connection
mysql_close($connection);
}
?>
</body>
</html>
Here, the user is first presented with a form asking for a country and its
national animal.
Once the form is submitted, the form input is used inside to create an
INSERT query, which is then sent to the database with the mysql_query()
method. Since mysql_query() returns a Boolean value indicating whether
the query was successful or not, it is possible to check whether the INSERT
took place and return an appropriate message:
There are two new functions in the example above. The
mysql_escape_string() function escapes special characters (like quotes)
in the user input so that it can be safely entered into the database; while the
mysql_insert_id() returns the ID generated by the previous INSERT
query (useful only if the table into which the INSERT occurs contains an
AUTO_INCREMENT field). Both these functions are also available in ext/
mysqli.
Wiping Out
Obviously, you can also do the same thing with other data manipulation
statements. This next example demonstrates how to use a DELETE
statement with PHP to selectively delete items from the table. For variety,
I'm going to use ext/mysqli this time around:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// execute query
if ($mysqli->query($query)) {
// print number of affected rows
echo $mysqli->affected_rows." row(s) affected";
}
else {
// print error message
echo "Error in query: $query. ".$mysqli->error;
}
}
// query to get records
$query = "SELECT * FROM symbols";
// execute query
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td><a href=".$_SERVER['PHP_SELF']."?
id=".$row[0].">Delete</a></td>";
echo "</tr>";
}
}
// free result set memory
$result->close();
}
else {
// print error message
echo "Error in query: $query. ".$mysqli->error;
}
// close connection
$mysqli->close();
?>
</body>
</html>
Looking Inside
PHP comes with a bunch of functions designed to tell you everything you
would ever want to know about the MySQL client and server, their version
numbers, the total number of databases available, the tables inside each
database, the processes running... you name it, and it's probably there.
Here's an example which uses them to give you a big-picture view of what's
going on inside your MySQL RDBMS:
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// open connection
$connection = mysql_connect($host, $user, $pass) or die
("Unable to connect!");
// close connection
mysql_close($connection);
?>
</body>
</html>
Oops!
All done? Nope, not quite yet - before you go out there and start building
cool data-driven Web sites, you should be aware that both MySQL extensions
come with powerful error-tracking functions which can speed up
development time. Take a look at the following example, which contains a
deliberate error in the SELECT query string:
<?php
// connect
$connection = mysql_connect("localhost", "test", "test") or
die("Invalid server or user");
mysql_select_db("testdb", $connection) or die("Invalid
database");
// query
$query = "SELECT FROM symbols";
// result
$result = mysql_query($query,$connection);
// disconnect
mysql_close($connection);
?>
<?php
// query
$query = "SELECT FROM symbols";
// execute query
$result = $mysqli->query($query);
// disconnect
$mysqli->close();
?>
And in case you were wondering why I haven't used object syntax for these
two functions in the script above, it's actually very simple: I can't. You see, if
there is an error in connecting to the server, the mysqli() object will not be
created, and so methods and properties related to that object will not exist.
For this reason, to debug connection errors in ext/mysqli, you must
always use the procedural, rather than the object, notation.
And that's about all I have for this issue of PHP 101. In Part Nine I'm going
to tell you all about PHP 5's built-in DBMS alternative, the very cool SQLite
database engine. Don't miss it!
Part 9.1:
Hard Choices
If you've been paying attention, you now know how to use PHP's MySQL API
to perform queries and process result sets. You might even have started
thinking about how to re-program your site to run off a MySQL database. All
of this is a Good Thing - it means you're getting comfortable with using
PHP's database support to power your applications - but there's still a little
further to go.
As you saw in Part Eight, enabling MySQL support in PHP 5.0 is not as simple
as it used to be. Instead of supporting MySQL out of the box, PHP now
requires you to make all kinds of decisions about versions and libraries
before allowing you to hook your scripts up to a MySQL database. If you're
lazy (and deep down, we both know you are), you might instead prefer to
try a simpler option: the SQLite database engine.
Built-in SQLite support is new to PHP 5.0, and offers users a lightweight
database system that is fast, efficient and gets the job done. Since it's
enabled by default in PHP 5.0, it provides a viable alternative to MySQL; you
can use it out of the box, without spending time on version checks and
library downloads; just install PHP 5 and start typing. That's why I'm
devoting a whole tutorial to it - so get out of bed, make yourself some coffee
and let's get started!
This small size shouldn't deceive you, however - according to the official
SQLite Web site, SQLite supports databases up to 2 terabytes in size and is
actually faster than MySQL in certain situations. SQLite database files are
easily portable, and SQLite databases created on Windows work fine on *NIX
platforms and vice-versa.
In order to use SQLite and PHP together, your PHP build must include
SQLite. This is enabled by default in both the UNIX and Windows versions of
PHP 5. Read more about this at http://www.php.net/manual/en/
ref.sqlite.php. If you're a PHP 4.x user, though, don't lose heart - you can
still use SQLite, by manually downloading and installing php_sqlite.dll from
http://snaps.php.net (Windows) or the latest tarball from http://
pecl.php.net/package/SQLite (UNIX). You don't need to download anything
else; the SQLite 'client' is its own engine.
Here's an example, which sets up the table I'll be using in this tutorial:
C:\WINDOWS\Desktop\sqlite>sqlite library.db
SQLite version 2.8.15
Enter ".help" for instructions
sqlite> create table books (
...> id integer primary key,
...> title varchar(255) not null,
...> author varchar(255) not null
...>);
sqlite> insert into books (title, author) values ('The Lord
Of The Rings', 'J.R.R. Tolkien');
sqlite> insert into books (title, author) values ('The
Murders In The Rue Morgue', 'Edgar Allen Poe');
sqlite> insert into books (title, author) values ('Three
Men In A Boat', 'Jerome K. Jerome');
sqlite> insert into books (title, author) values ('A Study
In Scarlet', 'Arthur Conan Doyle');
sqlite> insert into books (title, author) values ('Alice In
Wonderland', 'Lewis Carroll');
sqlite> .exit
Once the data has been imported into the database file library.db, run a
quick SELECT query to check if everything is working as it should:
sqlite> select * from books;
Anatomy Class
Now, use PHP to communicate with SQLite, generate the same result set and
format it as an HTML page. Here's the code:
<html>
<head></head>
<body>
<?php
// set path of database file
$db = $_SERVER['DOCUMENT_ROOT']."/../library.db";
// execute query
$result = sqlite_query($handle, $query) or die("Error in
query: ".sqlite_error_string(sqlite_last_error($handle)));
// if rows exist
if (sqlite_num_rows($result) > 0) {
// get each row as an array
// print values
echo "<table cellpadding=10 border=1>";
while($row = sqlite_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}
// all done
// close database file
sqlite_close($handle);
?>
</body>
</html>
$db = $_SERVER['DOCUMENT_ROOT']."/../library.db";
$handle = sqlite_open($db) or die("Could not open
database");
?>
?>
This function also needs two parameters: the database handle and the
query string. Depending on whether or not the query was successful,
the function returns true or false; in the event of a failure, the
sqlite_error_string() and sqlite_last_error() functions can
be used to display the error that took place.
3. If sqlite_query() is successful, the result set returned by the query
is stored in the variable $result. You can retrieve the records in the
result set with the sqlite_fetch_array() function, which fetches a
single row of data as an array called $row. Fields in that record are
represented as array elements, and can be accessed using standard
index notation. Each time you call sqlite_fetch_array(), the next
record in the result set is returned. This makes
sqlite_fetch_array() very suitable for use in a while() loop, in
much the same way as mysql_fetch_row() was used earlier.
<?php
if (sqlite_num_rows($result) > 0) {
echo "<table cellpadding=10 border=1>";
while($row = sqlite_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
The number of records returned by the query can be retrieved with the
sqlite_num_rows() function. Or, if what you're really interested in is
the number of fields in the result set, use the sqlite_num_fields()
function instead. Of course, these are only applicable with queries that
actually return records; it doesn't really make sense to use them with
INSERT, UPDATE or DELETE queries.
4. Once you're done, it's a good idea to close the database handle and
return the used memory to the system, with a call to
sqlite_close(): <?php
sqlite_close($handle);
?>
In PHP 5 you can also use the SQLite API in an object-oriented way, wherein
each of the functions above becomes a method of the SQLiteDatabase()
object. Take a look at this next listing, which is equivalent to the one above:
<html>
<head></head>
<body>
<?php
// set path of database file
$file = $_SERVER['DOCUMENT_ROOT']."/../library.db";
// if rows exist
if ($result->numRows() > 0) {
// get each row as an array
// print values
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch()) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}
// all done
// destroy database object
unset($db);
?>
</body>
</html>
Different Strokes
As with the MySQL API, PHP's SQLite API offers you more than one way to
skin a cat. For example, you can retrieve each row as an object with the
sqlite_fetch_object() method, and access field values by using the
field names as object properties. Here's an example:
<html>
<head></head>
<body>
<?php
// set path of database file
$db = $_SERVER['DOCUMENT_ROOT']."/../library.db";
// execute query
$result = sqlite_query($handle, $query) or die("Error in
query: ".sqlite_error_string(sqlite_last_error($handle)));
// if rows exist
if (sqlite_num_rows($result) > 0) {
// get each row as an object
// print field values as object properties
echo "<table cellpadding=10 border=1>";
while($obj = sqlite_fetch_object($result)) {
echo "<tr>";
echo "<td>".$obj->id."</td>";
echo "<td>".$obj->title."</td>";
echo "<td>".$obj->author."</td>";
echo "</tr>";
}
echo "</table>";
}
// all done
// close database file
sqlite_close($handle);
?>
</body>
</html>
Another option is to retrieve the complete result set in one fell swoop with
the sqlite_fetch_all() function. This function retrieves the complete set
of records as an array of arrays; each element of the outer array represents
a record, and is itself structured as an array whose elements represent fields
in that record.
<html>
<head></head>
<body>
<?php
// set path of database file
$db = $_SERVER['DOCUMENT_ROOT']."/../library.db";
// execute query
$result = sqlite_query($handle, $query) or die("Error in
query: ".sqlite_error_string(sqlite_last_error($handle)));
// all done
// close database file
sqlite_close($handle);
In all the previous examples, the database remained open while the result
set was processed, because records were retrieved one after another with
the sqlite_fetch_array() or sqlite_fetch_object() functions. The
example above is unique in that the database can be closed before the result
set array is processed. This is because the entire result set is retrieved at
once and stored in the $data array, so there really isn't any need to leave
the database open while processing it.
<html>
<head></head>
<body>
<?php
// set path of database file
$db = $_SERVER['DOCUMENT_ROOT']."/../library.db";
// execute query
$result = sqlite_query($handle, $query) or die("Error in
query: ".sqlite_error_string(sqlite_last_error($handle)));
// if a row exists
if (sqlite_num_rows($result) > 0) {
// get the value of the first field of the first row
echo sqlite_fetch_single($result);
}
// all done
// close database file
sqlite_close($handle);
?>
</body>
</html>
<html>
<head></head>
<body>
<?php
// set path of database file
$db = $_SERVER['DOCUMENT_ROOT']."/../library.db";
// execute query
$result = sqlite_query($handle, $query) or die("Error in
query: ".sqlite_error_string(sqlite_last_error($handle)));
// if rows exist
if (sqlite_num_rows($result) > 0) {
echo "<table cellpadding=10 border=1>";
// check for more rows
while (sqlite_has_more($result)) {
// get first field from each row
// print values
$row = sqlite_fetch_single($result);
echo "<tr>";
echo "<td>".$row."</td>";
echo "</tr>";
}
echo "</table>";
}
// all done
// close database file
sqlite_close($handle);
?>
</body>
</html>
You can, of course, do the same thing using object notation in PHP 5.
However, you need to know that sqlite_has_more() is one function that
really doesn't translate to its object method name; in an OO script, you
would need to call $result->valid();.
// execute query
$result = $db->query($query) or die("Error in query");
// if rows exist
if ($result->numRows() > 0) {
echo "<table cellpadding=10 border=1>";
// check for more rows
while ($result->valid()) {
// get first field from each row
// print values
$row = $result->fetchSingle();
echo "<tr>";
echo "<td>".$row."</td>";
echo "</tr>";
}
echo "</table>";
}
// all done
// destroy database object
unset($db);
?>
</body>
</html>
Part 9.2:
Not My Type
Whilst on the topic of INSERT, remember my statement a couple pages back
about how SQLite is typeless and so you can insert values of any type into
any field? There is one important exception to this rule: a field marked as
INTEGER PRIMARY KEY. In SQLite, fields marked as INTEGER PRIMARY
KEY do two important things: they provide a unique numeric identifier for
each record in the table, and if you insert a NULL value into them, SQLite
automatically inserts a value that is 1 greater than the largest value already
present in that field.
Since the books table used in the previous example already contains such a
field (the id field), it's clear that every INSERT into it with a NULL value for
that field generates a new record number. If you'd like to retrieve this
number, PHP has a way to do that too - just use the
sqlite_last_insert_rowid() function, which returns the ID of the last
inserted row (equivalent to the mysql_insert_id() function in PHP's
MySQL API).
To see this in action, update the if() loop in the middle of the previous
script to include a call to sqlite_last_insert_rowid(), as follows:
<?php
?>
If you need to, you can also find out how many rows were affected using the
sqlite_changes() function - try it for yourself and see!
<?php
// insert records
sqlite_query($handle, "INSERT INTO books (title, author)
VALUES ('The Lord Of The Rings', 'J.R.R. Tolkien')") or
die("Error in query:
".sqlite_error_string(sqlite_last_error($handle)));
?>
<?php
// create database
$db->query("CREATE TABLE books (id INTEGER PRIMARY KEY,
title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT
NULL)") or die("Error in query");
// insert records
$db->query("INSERT INTO books (title, author) VALUES ('The
Lord Of The Rings', 'J.R.R. Tolkien')") or die("Error in
query");
// all done
// destroy database object
unset($db);
?>
<?php
// version
echo "SQLite version: ".sqlite_libversion()."<br />";
// encoding
echo "SQLite encoding: ".sqlite_libencoding()."<br />";
?>
<?php
// execute query
$result = sqlite_query($handle, $query) or die("Error in
query: ".sqlite_error_string(sqlite_last_error($handle)));
// all done
// close database file
sqlite_close($handle);
?>
If your appetite has been whetted, you can read more about the things PHP
can do with SQLite in Zend's PHP 5 In Depth section.
And that's about all I have for you in this tutorial. More secrets await you in
Part 10 of PHP 101, so make sure you come back for that one!
Part 10:
Patience Pays
Now that you've used PHP with MySQL and SQLite, you probably think you
know everything you need to get started with PHP programming. In fact, you
might even be thinking of cutting down your visits to Zend.com altogether,
giving up this series for something flashier and cooler...
You see, while built-in database support makes programming with PHP easy,
it isn't the only thing that makes PHP so popular. An easy-to-use XML API
and new exception handling mechanism (in PHP 5), support for pluggable
modules, and built-in session management are just some of the many other
features that make PHP rock. And all these capabilities are going to be
explored, in depth, right here in this very series, if you can just find it in
yourself to hang around a little longer. So close your eyes, take a deep
breath, and read on to find out all about this tutorial's topic: sessions and
cookies.
Party Time
Maybe you heard this at the last party you went to: "HTTP is a stateless
protocol, and the Internet is a stateless development environment".
In simple language, all this means is that HTTP, the HyperText Transfer
Protocol that is the backbone of the Web, is unable to retain a memory of
the identity of each client that connects to a Web site, and therefore treats
each request for a Web page as a unique and independent connection, with
no relationship whatsoever to the connections that preceded it. This
"stateless environment" works great so long as you're aimlessly surfing the
Web, but it can cause a serious headache for sites that actually depend on
the data accumulated in previous requests. The most common example is
that of an online shopping cart - in a stateless environment, it becomes
difficult to keep track of all the items you've shortlisted for purchase as you
jump from one catalog page to another.
Sessions and cookies thus provide an elegant way to bypass the stateless
nature of the HTTP protocol, and are used on many of today's largest sites to
track and maintain information for personal and commercial transactions.
Typically, you use a session to store values that are required over the course
of a single visit, and a cookie to store more persistent data that is used over
multiple visits.
PHP has included support for cookies since PHP 3.0, and built-in session
management since PHP 4.0. Both these features are enabled by default, so
you don't have to do anything special to activate them. Instead, scroll down
and take a look at your first session.
<?php
// initialize a session
session_start();
// print value
echo "You have viewed this page " . $_SESSION['counter'] .
" times";
?>
To see how this works, request the script above through your browser a few
times. You will notice that the counter increases by 1 on each subsequent
page load. If you open up two browser windows and request the same page
in each one, PHP will maintain and increment individual session counters for
each browser instance. The session ID is used to identify which client made
which request, and recreate the prior saved environment for each individual
session. This also means that if you visit one (or more) other Web sites
during the same session and then return to the script above without shutting
down your browser in the interim, your previous session will be retrieved
and recreated for you.
If the example above doesn't work as advertised, check to make sure that
the session.save_path variable in your php.ini file points to a valid
temporary directory for your system. This value is hard-wired to /tmp by
default, so if you're trying the example on a Windows system, you will need
to edit it to C:\Windows\temp (or your system's temporary directory).
Remember Me
Here's another example, this one asking you to log in and then storing your
login name and session start time as two session variables. This information
is then used to display the total number of minutes the session has been
active.
<?php
// initialize a session
session_start();
?>
<html>
<head></head>
<body>
<?php
if (!isset($_SESSION['name']) && !isset($_POST['name'])) {
// if no data, print the form
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>"
method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="Enter
your name">
</form>
<?php
}
else if (!isset($_SESSION['name']) &&
isset($_POST['name'])) {
// if a session does not exist but the form has been
submitted
// check to see if the form has all required values
// create a new session
if (!empty($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['start'] = time();
echo "Welcome, " . $_POST['name'] . ". A new
session has been activated for you. Click <a href=" .
$_SERVER['PHP_SELF'] . ">here</a> to refresh the page.";
}
else {
echo "ERROR: Please enter your name!";
}
}
else if (isset($_SESSION['name'])) {
// if a previous session exists
// calculate elapsed time since session start and now
echo "Welcome back, " . $_SESSION['name'] . ". This
session was activated " . round((time() -
$_SESSION['start']) / 60) . " minute(s) ago. Click <a
href=" . $_SERVER['PHP_SELF'] . ">here</a> to refresh the
page.";
}
?>
</body>
</html>
It's important to note that the call to session_start() must appear first,
before any output is generated by the script (assuming you're not using
PHP's output buffering feature, which you can read about at http://
www.php.net/manual/en/ref.outcontrol.php). This is because the PHP
session handler internally uses in-memory cookies to store session data, and
the cookie creation headers must be transmitted to the client browser before
any output. If you ever see an error like this in one of your session-enabled
pages:
it's usually because somewhere, somehow, some output has found its way to
the browser before session_start() was called. Even a carriage return or
a blank space outside the PHP tags surrounding session_start() can
cause this error, so watch out for them.
As noted previously, every session has a unique session ID, which PHP uses
to keep track of different clients. This session ID is a long alphanumeric
string, which is automatically passed by PHP from page to page so that the
continuity of the session is maintained. To see what it looks like, use the
session_id() function, as in this simple example:
<?php
// initialize a session
session_start();
// print session ID
echo "I'm tracking you with session ID " . session_id();
?>
When the user shuts down the client browser and destroys the session, the
$_SESSION array will be flushed of all session variables. You can also
explicitly destroy a session - for example, when a user logs out - by calling
the session_destroy() function, as in the following example:
<?php
// initialize a session
session_start();
// then destroy it
session_destroy();
?>
In case you were wondering if you read that right - yes, before you can call
session_destroy() to destroy a session, you must first call
session_start() to recreate it.
<?php
// initialize a session
session_start();
?>
You can read more about sessions and session handling functions at http://
www.php.net/manual/en/ref.session.php.
Before you start using cookies, there are a few things you should be aware
of:
More information on cookies can be obtained from Netscape, the people who
originally invented them. Visit http://www.netscape.com/newsref/std/
cookie_spec.html for the Netscape cookie specification.
It's important to remember that, since cookies are stored on the user's hard
drive, you as the developer have very little control over them. If a user
decides to turn off cookie support in his or her browser, your cookies will
simply not be saved. Therefore, avoid writing code that depends heavily on
cookies; and have a backup plan ready in case cookie data cannot be
retrieved from the client.
With that caveat out of the way, let's look at some simple cookie-handling
code in PHP.
<?php
if (!isset($_COOKIE['visited'])) {
// if a cookie does not exist
// set it
setcookie("visited", "1", mktime()+86400, "/") or
die("Could not set cookie");
echo "This is your first visit here today.";
}
else {
// if a cookie already exists
echo "Nice to see you again, old friend!";
}
?>
To see how this works, request the page above through your browser a
couple of times. The first time around, because no cookie has yet been set,
the first message will be displayed. On all subsequent attempts, because the
cookie has already been set, the client will be recognized and the second
message will be displayed. Note that this works even if you terminate the
browser instance, restart it and visit the page again - a marked difference
from what happened in the session examples you saw earlier.
The setcookie() function accepts six arguments: the name of the cookie,
its value, its expiry date, the domain, the path for which it is valid, and a
Boolean value indicating its security state. As noted previously, only the
name and value are mandatory, although the example above specifies both a
top-level directory and an expiry date for the cookie (1 day) with the
mktime() function, which works like the time() function described
previously.
Cookie values are automatically sent to PHP from the client, and converted
to key-value pairs in the $_COOKIE variable, a superglobal array similar to
$_SESSION. Values can then be retrieved using standard associative array
notation, as in the example above. Note that, as with sessions, calls to
setcookie() must take place before any output is generated by the script,
or else you'll see an error like this:
<?php
if (!isset($_POST['email'])) {
// if form has not been submitted
// display form
// if cookie already exists, pre-fill form field with
cookie value
?>
<html>
<head></head>
<body>
</body>
</html>
<?php
}
else {
// if form has been submitted
// set cookies with form value and timestamp
// both cookies expire after 30 days
if (!empty($_POST['email'])) {
setcookie("email", $_POST['email'], mktime()
+(86400*30), "/");
setcookie("lastsave", time(), mktime()+(86400*30),
"/");
echo "Your email address has been recorded.";
}
else {
echo "ERROR: Please enter your email address!";
}
}
?>
</body>
</html>
In this case, the value entered into the form is stored as a cookie called
email, and automatically retrieved to pre-fill the form field on all subsequent
requests. This technique is frequently used by Web sites that require the
user to enter a login name and password; by automatically pre-filling the
username field in the login box with the value used in the last successful
attempt, they save the user a few keystrokes.
This example also demonstrates how you can set more than one cookie for a
domain, by calling setcookie() multiple times. In the example above, the
time at which the data was entered is stored as a second cookie, and used to
calculate the time elapsed between successive entries.
To remove a cookie from the client, simply call setcookie() with the same
syntax you used to originally set the cookie, but an expiry date in the past.
This will cause the cookie to be removed from the client system. Here's an
example:
<?php
// delete cookie
setcookie("lastsave", NULL, mktime() - 3600, "/");
?>
Access Granted
As I said at the beginning of this tutorial, cookies and sessions are two
different ways of making data "persistent" on the client. A session retains
data for the duration of the session, while a cookie retains values for as long
as you need it to. With that in mind, let's now look at an example that uses
them both.
+-------+-----------------------------------------------+
| name | pass |
+-------+-----------------------------------------------+
| sue | 9565d44fd0fe4db59f073eea1db70f3ea258e10b |
| harry | 6e74234b8b552685113b53c7bff0f386c8cef8cf |
| louis | 6817dda51b64b8190029490d2811a4d9cb9cd432 |
| sam | bd17f8243e771a57cfbb06aa9a82bbf09fd2d90b |
| james | 792ec9b44d432c947ac6775b2b52326e9d08512f |
+-------+-----------------------------------------------+
with a unique username field and a password field created with the SHA1()
function, here's the PHP script that does all the hard work:
<?php
if (isset($_POST['name']) || isset($_POST['pass'])) {
// form submitted
// check for required values
if (empty($_POST['name'])) {
die ("ERROR: Please enter username!");
}
if (empty($_POST['pass'])) {
die ("ERROR: Please enter password!");
}
// open connection
$connection = mysql_connect($host, $user, $pass) or die
("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select
database!");
// create query
$query = "SELECT * FROM users WHERE name = '" .
$_POST['name'] . "' AND pass = SHA1('" . $_POST['pass'] .
"')";
// execute query
$result = mysql_query($query) or die ("Error in query:
$query. " . mysql_error());
// close connection
mysql_close($connection);
}
else {
// no submission
// display login form
?>
<html>
<head></head>
<body>
<center>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF']; ?>">
Username <input type="text" name="name" value="<?php
echo $_COOKIE['username']; ?>">
<p />
Password <input type="password" name="pass">
<p />
<input type="submit" name="submit" value="Log In">
</center>
</body>
</html>
<?php
}
?>
Here, the values entered into the login box are integrated into a MySQL
SELECT query, which is executed on the user table. If both username and
password match, a single record will be returned, indicating that
authentication succeeded; if they don't, no records will be returned,
indicating that authentication failed.
Of course, this isn't enough by itself. While the script above performs
authentication and initializes both a session and a cookie if the user's
credentials are validated, a security check must also be carried out on each
of the restricted pages. Without this check, any user could bypass the login
screen and simply type in the exact URL to each page to view it.
Since it is clear from the previous script that the session variable
$_SESSION['auth'] can only exist if the user's credentials have been
validated, it suffices to check for the presence of the $_SESSION['auth']
variable at the top of each restricted page, and grant access if that check
returns true. Here's how:
<?php
// start session
session_start();
if (!$_SESSION['auth'] == 1) {
// check if authentication was performed
// else die with error
die ("ERROR: Unauthorized access!");
}
else {
?>
<html>
<head></head>
<body>
This is a secure page. You can only see this if
$_SESSION['auth'] = 1
</body>
</html>
<?php
}
?>
Pretty neat, huh? Only authenticated users will be able to see this page,
because only their clients will have a session with the $_SESSION['auth']
variable in it. Everyone else will simply see an error message.
That's about it for this tutorial. In Part Eleven, I'll be telling you all about
SimpleXML, the new XML processing toolkit that comes bundled with PHP 5.
Make sure you come back for that!