Here's how it works: Redirect /aboutus http://www.example.com/about Any requests for URLs that begin with /aboutus will be redirected to the /about directory.. You can even specify wheth
Trang 1Using Apache Access Control Files
the <meta> tag tells it to This is how many people handle these sorts of cases
There's one obvious problem, though Let's say you had many pages in the aboutus directory You'd have to create pages like this one to replace each of them The other, slightly less obvious problem, is that these tags mess with the Back button Unless users are careful, they'll go back to the redirect page, get redirected to the new page again, go back again, get redirected again, and on and on To get back past a page that uses a <meta> tag to redirect users, you have to click the Back button twice in rapid succession
Using the Redirect directive, you can solve this problem much more elegantly Here's how it works:
Redirect /aboutus http://www.example.com/about
Any requests for URLs that begin with /aboutus will be redirected to the /about directory Apache will also take everything after the path specified in the Redirect directive and append it to the target URL
So, a request for /aboutus/management.html will be redirected to http://www.example.com/about/
management.html This makes the redirection completely transparent to the user You can even specify whether the redirect is permanent or temporary like this:
Redirect temp /aboutus http://www.example.com/about
Redirect permanent /aboutus http://www.example.com/about
Redirect seeother /aboutus http://www.example.com/about
You can indicate that /aboutus is gone without redirecting the user like this:
Redirect gone /aboutus
That's a more civilized thing to do than letting your users run into 404 errors
If you need to match something other than the beginning of the URL, you can use RedirectMatch, which uses regular expressions as the matching part of the URL There's no space here to discuss regular
expressions, but I'll give you one example of how RedirectMatch works Let's say you replace all of your html files on your site with .php files so that you can use PHP includes for navigation Here's the rule:
RedirectMatch (.*)\.html$ http://www.example.com$1.php
Let me break that down It basically says that any URL ending in .html should be redirected to the same URL on the server http://www.example.com except that the .html ending should be replaced with .php First, let's look at the URL to match, from end to beginning It ends with $, which indicates that the
string to be matched must be at the end of the URL The \.html is the string to match The \ is in there
to indicate that the . should actually appear in the URL, and not be treated as a regular expression
metacharacter The (.*) says "match everything up to the .html." The .* matches everything, and the parentheses indicate that the regular expression should remember what was matched In the target URL, the part of the URL that was matched is retrieved and plugged in
file:///G|/1/0672328860/ch19lev1sec3.html (4 von 4) [19.12.2006 13:50:11]
Trang 2Summary
Throughout this book, I've talked about HTML, JavaScript, images, and other techniques that work
regardless of whether your web pages live on a web server, on a CD-ROM, or in a work directory on your local hard drive There's more to web servers, though, than just serving up static web pages when users request them, and this lesson was just the tip of the iceberg when it comes to exploring the
additional capabilities of web server software I discussed some popular web application development platforms You probably don't need to know all of them, but if you do much web development,
eventually you'll wind up using one of them or another I also talked about how you can take advantage
of server features such as server-side includes, access control, and redirection features built into the server to make your life as a web developer easier
The key here is to take advantage of the features your web server offers to save yourself work and
provide a better experience for your users You should look into the web server that your pages will reside on for more ways to take advantage of it
file:///G|/1/0672328860/ch19lev1sec4.html [19.12.2006 13:50:11]
Trang 3Workshop
This workshop contains some questions about HTML validation, as well as a quiz and exercises that will refresh your memory on what you've learned
Q&A
Q How do I figure out which web server I use?
A If you don't know which web server your web pages reside on, you can ask the system administrator responsible for making sure it's up and running; he'll certainly know Or you can use Netcraft's site to figure out the server software and operating system for your site Just go to http://uptime.netcraft.com/up/graph/
Enter the hostname of the computer where your pages are stored to get information about the software running on your server
Q Does using the web application platforms require me to be a programmer?
A No more so than learning to use Dynamic HTML or JavaScript There are some really simple things you can do to take advantage of these platforms, and at the same time you can write really complex applications as well, like the ones that power huge sites like Amazon.com and Yahoo!
Quiz
1. What's the relationship between the web server and CGI programs?
2. What are the two standard languages associated with programming in Active Server
Pages?
3. What are the two types of includes in the J2EE world?
4. In PHP, how do the require() and include() functions differ?
file:///G|/1/0672328860/ch19lev1sec5.html (1 von 2) [19.12.2006 13:50:11]
Trang 45. When using server-side includes, how do virtual and file includes differ?
Quiz Answers
1. CGI programs are standalone programs that are invoked by the web server They
produce data in a format that the web server understands how to send to the browser
2. The two standard languages that can be used with ASP are VBScript and JavaScript
3. In the J2EE world, there are compile-time includes and runtime includes Code in
compile-time includes can interact with code in the including file Runtime includes must be independent
4. If the file cannot be included when you use the require() function to include a file in
PHP, the page fails to load If include() is used, PHP proceeds with a warning
5. When you use virtual includes with SSI, the file can exist anywhere under the
document root When you use file, the file must be in or below the current directory
Exercises
1. Find out which web server you use and figure out how to apply includes to your
website
2. If you have a web application environment available for your server, find a site that
offers free sample programs and take a look at some
file:///G|/1/0672328860/ch19lev1sec5.html (2 von 2) [19.12.2006 13:50:11]
Trang 5Lesson 20 Understanding Server-Side Processing
Lesson 20 Understanding Server-Side Processing
At this point, you've learned how to publish websites using HTML This lesson takes things a step further and explains how to build dynamic websites using scripts on the server Rather than trying to explain several of the platforms described in Lesson 19, "Taking Advantage of the Server," I'm focusing on PHP
in this lesson PHP is the most common scripting platform provided by web hosts, can be easily installed
on your own computer, and is completely free It's also easy to get started with Even if you wind up developing your applications using some other scripting language, you can apply the principles you'll learn in this lesson to those languages
In this Lesson
● How PHP works
● How to set up a PHP development environment
● The basics of the PHP language
● How to process form input
● Using PHP includes
file:///G|/1/0672328860/ch20.html [19.12.2006 13:50:12]
Trang 6How PHP Works
How PHP Works
Lesson 19 provided a brief introduction to PHP You learned that PHP is a language that enables you to embed code processed by the server in your web pages Normally, when a user submits a request to the server for a web page, the server reads the HTML file and sends its contents back in response If the request is for a PHP file and the server has a PHP interpreter installed, then the server looks for PHP code in the document, executes it, and includes the output of that code in the page in place of the PHP code Here's a simple example:
<html>
<head><title>A PHP Page</title</head>
<body>
<?php echo "Hello world!"; ?>
</body>
</html>
If this page is requested from a web server that supports PHP, the HTML sent to the browser will look like this:
<html>
<head><title>A PHP Page</title</head>
<body>
Hello world!
</body>
</html>
When the user requests the page, the web server determines that it is a PHP page rather than a regular HTML page If a web server supports PHP, it usually treats any files with the extension .php as PHP
pages Assuming this page is called something like hello.php, when the web server receives the request,
it scans the page looking for PHP code and then runs any code it finds As you learned in Lesson 19, PHP code is set apart from the rest of a page by PHP tags, which look like this:
<?php your code here ?>
Whenever the server finds those tags, it treats whatever is within them as PHP code That's not so
different from the way things work with JavaScript, where anything inside <script> tags is treated as JavaScript code The main difference is that all your <script> tags are supposed to be placed within the page header, whereas PHP tags can occur anywhere within a page
In the example, the PHP code contains a call to the echo function This function prints out the value of whatever is passed to it In this case, I passed the text "Hello world!" to the function, so that text is included in the page The concept of functions should also be familiar to you from the lesson on
JavaScript Just like JavaScript, PHP lets you define your own functions or use functions built into the language echo is a built-in function
Statements in PHP, as in JavaScript, are terminated with a semicolon (You can see the semicolon at the end of the statement in the example.) There's no reason why you can't include multiple statements
within one PHP tag, like this:
file:///G|/1/0672328860/ch20lev1sec1.html (1 von 2) [19.12.2006 13:50:12]
Trang 7How PHP Works
<?php
echo "Hello ";
echo "world!";
?>
PHP also provides a shortcut if all you want to do is print the value of something to a page Rather than using the full PHP tag, you can use the expression tag, which just echoes a value to the page Rather than using this:
<?php echo "Hello world!"; ?>
You can use this:
<?= "Hello world!" ?>
Replacing php with = enables you to leave out the call to the echo function and the semicolon This style
of tag is referred to as a short tag.
file:///G|/1/0672328860/ch20lev1sec1.html (2 von 2) [19.12.2006 13:50:12]
Trang 8Getting PHP to Run on Your Computer
Getting PHP to Run on Your Computer
Before you can start writing your own PHP scripts, you'll need to set up a PHP environment The easiest approach is probably to sign up for a web hosting account that provides PHP support Even if you do so, though, there are some advantages to getting PHP to work on your own computer You can edit files with your favorite editor and then test them right on your own computer rather than uploading them in order to see how they work You'll also be able to work on them even if you're not online Finally, you can keep from putting files on a server that your users will be able to see without your having tested them first
To process PHP pages, you need the PHP interpreter and a web server that works with the PHP
interpreter The good news is that PHP and the most popular web server, Apache, are both free, open-source software The bad news is that getting PHP up and running can be painful, mainly because it's usually installed as an add-on to the Apache web server So you have to get Apache to work, install PHP, and then get it to work with Apache
Fortunately, if you're a Windows or Mac user, someone else has done this hard work for you A tool
called XAMPP, available for both Windows and Mac OS X, bundles up versions of Apache, PHP, and
MySQL (a database useful for storing data associated with web applications) that are already set up to work together You can download it at http://www.apachefriends.org/en/xampp.html
If you're a Mac user, you also have the option of using MAMP, another free package that combines
Apache, PHP, and MySQL It can be downloaded from http://www.mamp.info
Once you've installed XAMPP (or MAMP), you just have to start the application in order to get a web server up and running that you can use to develop your pages To test your PHP pages, you can put them in the htdocs directory inside the XAMPP install directory For example, if you wanted to test the
hello.php page I talked about earlier, you could put it in the htdocs directory To view it, just go to
http://localhost/hello.php
If that doesn't work, make sure that XAMPP has started the Apache server If you're using MAMP, the steps are basically the same Just put your pages in the htdocs folder, as with XAMPP
file:///G|/1/0672328860/ch20lev1sec2.html [19.12.2006 13:50:12]
Trang 9The PHP Language
The PHP Language
When you think about the English language, you think about it in terms of parts of speech Nouns name things, verbs explain what things do, adjectives describe things, and so on Programming languages are similar A programming language is made up of various "parts of speech" as well In this section, I'm going to explain the parts of speech that make up the PHP languagecomments, variables, conditional statements, and functions
Comments
Like HTML and JavaScript, PHP supports comments PHP provides two comment stylesone for single-line comments and another for multiple comments (If you're familiar with comments in the C or Java
programming language, you'll notice that PHP's are the same.) First, single-line comments To start a single-line comment, use // or # Everything that follows either on a line is treated as a comment Here are some examples:
// My function starts here
$color = 'red'; // Set the color for text on the page
# $color = 'blue';
$color = $old_color; # Sets the color to the old color
// $color = 'red';
The text that precedes // is processed by PHP, so the second line assigns the $color variable On the third line, I've turned off the assignment by commenting it out PHP also supports multiple-line
comments, which begin with /* and end with */ If you wanted to comment out several lines of code, you could do it like this:
/*
$color = 'red';
$count = 55; // Set the number of items on a page
// $count = $count + 1;
*/
PHP will ignore all the lines inside the comments Note that you can put the // style comment inside the multiline comment with no ill effects You cannot, however, nest multiline comments This is illegal:
/*
$color = 'red';
$count = 55; // Set the number of items on a page
/* $count = $count + 1; */
*/
Note
The generally accepted style for PHP code is to use // for singleline comments rather than #
file:///G|/1/0672328860/ch20lev1sec3.html (1 von 8) [19.12.2006 13:50:14]
Trang 10The PHP Language
Variables
Variables just provide a way for the programmers to assign a name to a piece of data In PHP, these names are preceded by a dollar sign ($) Therefore, you might store a color in a variable called $color or
a date in a variable named $last_published_at Here's how you assign values to those variables:
$color = "red";
$last_published_at = time();
The first line assigns the value "red" to $color; the second returns the value returned by the built-in PHP function time() to $last_published_at That function returns a timestamp represented as the
number of seconds since what's called the "Unix epoch."
One thing you should notice here is that you don't have to indicate what kind of item you'll be storing in
a variable when you declare it You can put a string in it, as I did when I assigned "red" to $color You can put a number in it, as I did with $last_published_at I know that the number is a timestamp, but as far as PHP is concerned, it's just a number What if I want a date that's formatted to be displayed rather than stored in seconds so that it can be used in calculations? I can use the PHP date() function Here's
an example:
$last_published_at = date("F j, Y, g:i a");
This code formats the current date so that it looks something like "March 18, 2006, 8:47 pm." As you can see, I can change what kind of information is stored in a variable without doing anything special It just works The only catch is that you have to keep track of what sort of thing you've stored in a
variable when you use it For more information on how PHP deals with variable types, see http://www php.net/manual/en/language.types.type-juggling.php
Despite the fact that variables don't have to be declared as being associated with a particular type, PHP does support various data types, including string, integer, and float (for numbers with decimal points) Not all variable types work in all contexts One data type that requires additional explanation is the array data type
Arrays
The variables you've seen so far in this lesson have all been used to store single values Arrays are data structures that can store multiple values You can think of them as lists of values, and those values can
be strings, numbers, or even other arrays To declare an array, use the built-in array function:
$colors = array('red', 'green', 'blue');
This declaration creates an array with three elements in it Each element in an array is numbered, and
that number is referred to as the index For historical reasons, array indexes start at 0, so for the
preceding array, the index of red is 0, the index of green is 1, and the index of blue is 2 You can
reference an element of an array using its index, like this:
$color = $colors[1];
file:///G|/1/0672328860/ch20lev1sec3.html (2 von 8) [19.12.2006 13:50:14]