| CIS 209 | Web Page Scripting Languages |
ASP and PHP EXAMPLES - SIDE BY SIDE
Download ASP.NET Web Matrix at http://www.asp.net/webmatrix/. ASP.NET Web Matrix is a community-supported, easy-to-use WYSIWYG application development tool for ASP.NET. New features include: Access database support, J# support, design time enhancements including improved table editing and user-control rendering, many bug fixes, and much more! Best of all? It's absolutely free!
Great language reference for VBScript from DevGuru.com at http://www.devguru.com/Technologies/vbscript/quickref/functions.html.
Up to this point, we've created client-side scripts using JavaScript and VBScript. Client-side scripts are interpreted by a scripting engine that is built into the browser. Client-side scripts use the objects built into the browser's Document Object Model.
In this chapter we'll take a look at how to create server-side applications using Active Server Pages (ASP) and PHP. You need to have access to a web server that supports ASP in order to work through the examples in this chapter. Both Personal Web Server (PWS) and Internet Information Server (IIS) support ASP and are available free from Microsoft. However, Personal Web Server only runs on Windows 9x. Windows 2000 has IIS built in. Also, Windows XP Professional edition has IIS. Windows XP Home Edition does not include IIS. In order to run your PHP scripts, you simply need any Web server with PHP installed. You can set up Apache (free, open source Web server that runs on Windows or Linux) or use IIS.
Our class web server supports ASP and PHP. You can upload your files to your account on our class web server and run all your scripts. Developers often set up a web server on their own computer so that they can quickly test and debug their scripts, without having to upload them to the server. If you want to set up your computer at home to operate as a web server running ASP for testing purposes, here's some information.
Information on how to obtain and set up an ASP compliant Web server such as Personal Web Server (PWS) can be found in the Before You Begin section of our textbook.
If you are running Windows 95 or Windows 98, install Personal Web Server. PWS ships with programs including Visual InterDev, FrontPage, Visual Studio
and Windows 98. PWS allows programmers to preview their server-side scripts on
their computer, without having to connect to the Internet. To install PWS,
insert your Windows 98 CD, click START, then click RUN, then type
d:\add-ons\pws\setup.exe and
press ENTER. If your CD-ROM drive letter is different, substitute it for D. Or,
PWS can be downloaded from Microsoft's web site at
http://www.microsoft.com/msdownload/ntoptionpack/dlwiz.asp?StepNumber=1.
If you are running Windows 2000 or Windows XP Professional, you've already got Internet Information Server on your computer. You may need to Add/Remove Windows components if it is not installed. Click on START, SETTINGS, CONTROL PANEL, then double-click ADD/REMOVE PROGRAMS, then click on ADD/REMOVE WINDOWS COMPONENTS.
UPDATE: With Windows 7, IIS 7.5 is available, but not installed by default. Here are the installation instructions: http://technet.microsoft.com/en-us/library/cc731911.aspx.
Once you've got PWS or IIS running, you can download and install the PHP Windows binaries at http://www.php.net/downloads.php.
There are some issues with installing PWS on Windows ME. However, THIS LINK should provide some help in getting it to work.
If you are running Windows XP HOME Edition, and want to set up a Web server to test your scripts, Microsoft says you should buy the Professional edition (or here). However, http://www.15seconds.com provides a non-supported or tested solution (or here).
UPDATE: Looking for a Free, Open-Source, .NET Web Server? Check Cassini at ASP.NET. Here's what 4GuysFromRolla have to say about Cassini. Also, check out the ASP.NET Web Matrix Project.
Keep in mind that you do not HAVE to set up a web server on your computer at home or work. You can FTP your scripts to our class server to run them. Setting up a web server on your home or work computer will allow you to test and debug your scripts locally, saving the time of having to upload them to our class server first.
Oracle on the pros and cons of the two most popular means of building Web applications at http://otn.oracle.com/pub/articles/hull_asp.html.
HTML is a markup language. Browsers interpret HTML tags. The <SCRIPT> tag tells the browser to process the client-side script using the scripting engine that is built into the browser.
An Active Server Page (ASP) or a PHP page is a Web page that contains server-side script. ASP and PHP pages may also contain HTML and client-side scripts, but only the server-side scripts are executed on the server. When the server has finished processing the server-side script, it sends the HTML and client-side script to the browser to be interpreted on the client. ASP pages carry the file extension .asp, as opposed to .htm or .html. PHP pages carry the file extension .php, as opposed to .htm or .html. Since the server-side script is processed on the server, the source code is never accessible to the visitor. Therefore, ASP can be used to implement security rules within a Web site. Because server-side scripts are not executed within a browser, they cannot access the browser's Document Object Model. However, they can access the ASP Model, which includes many built-in objects that allow you to create more dynamic and interactive Web pages.
When the Web server recognizes the .asp file extension, it sends the request to the ASP engine. The ASP engine is actually a dynamic-link library called asp.dll, which is installed on the Web server. When the Web server recognizes the .php extension, it sends the request to the PHP engine. The PHP engine is actually a dynamic-link library called php4ts.dll, which is installed on the Web server.
After the request has been passed to the ASP engine, the ASP engine executes
scripts in the Global Application File. The Global Application File is named
global.asa and resides in the root directory of the web application.
Then, SSI files are assembled to be placed in the Web page. Next, the ASP engine
sends scripts to the scripting engine to be processed. Server-side scripts are
interpreted, not compiled like C or VB. However, ASP.NET is no longer an
interpreted language, rather it is a compiled language - compiled common
language code executing on the server. Finally, the Web server sends the file
to the browser using HTTP. PHP operates the same way to the programmer and
users.
Server-side scripts are browser independent because server-side scripts are never sent to the browser; they are always executed on the Web server.
Here are some differences between ASP and ASP.NET - http://www.tutorial-web.com/asp.net.
Just as we can integrate client-side scripts with objects built into the browser's Document Object Model, we can integrate our server-side scripts with the built-in objects from the ASP Object Model. PHP uses functions to accomplish the same thing.
There are many companies developing third party objects that can be access from your ASP code. You can also create your own custom objects in Visual Basic and other languages. These objects must be installed and registered on the Web server. Many custom PHP functions can be found on the Internet for download.
Any text editor or Visual editor, such as Notepad, FrontPage, Visual Studio, or Microsoft Script Editor can be used to create Active Server Pages or PHP pages. To view an ASP page or a PHP page, you must view the page via a Web server that supports ASP or PHP.
If you've set up your home or work computer as a server, be sure to view your ASP/PHP pages in your browser using the HTTP protocol. For example, type http://localhost or http://127.0.0.1 or http://YourComputerName from your browser's address line. This causes your server software to interpret your scripts on your web server, before sending the output to your browser. If you attempt to view your page locally on your hard drive, the server scripts will not run.
If you do not set up a server on your home or work computer, you can upload your ASP or PHP files to our class server and view them in your browser at http://www.halharris.com/282/ftp/A01/aspfile.asp or http://www.halharris.com/282/ftp/A01/phpfile.php (change A01 to your account name).
With ASP, Server-side scripts are written inside block script tags or inline script
tags. Block script tags use the same script tag as client-side script. However,
to distinguish a client-side script from a server-side script, you must use the
runat attribute with the assigned value server. Below is the syntax for adding a
sever-side script to an ASP page.
<script language="vbscript" runat = "server">
action and control statements
</script>
Inline script tags are used to delimit inline scripts. The ASP inline script tags
<% %>, which will be run before block script tags, are used to indicate inline
script. PHP uses the inline script tags <? ?>. Below is the syntax form adding an inline server-side script to an ASP
and PHP page.
ASP EXAMPLE:
<%
action and/or control statements
%>
or
PHP EXAMPLE:
<?
action and/or control statements
?>
In our study so far, as we studied client-side scripts, we used the document object write method to send output to the browser.
In ASP server-side scripts, the response object is used to send output from the Web server to the browser. The response object contains a method called write, which sends the output to the browser. Below is the syntax for the write method.
ASP USES:
<% response.write expression %>
PHP USES:
<? echo expression ?>
For ASP, all the VBScript techniques that you have learned so far can be used in ASP server-side scripts. The following example demonstrates how we can use the write method to display a value of a server variable in the browser.
<% @language = "vbscript" %>
<html><head><title>Server side script example - Using ASP</title></head>
<body><h1>
<% dim strHello %> (Book Error:
can't dim & asgn value on same line in vbs)
<% strHello = "Hello World" %>
<% response.write strHello %>
</h1></body></html>
The above code is placed in a text file and saved with the extension .asp in this EXAMPLE. When you click the example, you are running this ASP page from the server. Click on VIEW, SOURCE when you run the example. Notice that the browser only receives the output of the script run on the server. We do not see the source code since it is not sent to the browser.
For PHP, many of the JavaScript techniques that you have learned so far can
be used in PHP server-side scripts, with a few differences. The following
example demonstrates how we can use the echo command to display a
value of a server variable in the browser.
<html><head><title>Server side script example - Using
PHP</title></head>
<body><h1>
<? $strHello = "Hello World" ?>
<? echo $strHello ?>
</h1></body></html>
The above code is placed in a text file and saved with the extension .php in this EXAMPLE. When you click the example, you are running this PHP page from the server. Click on VIEW, SOURCE when you run the example. Notice that the browser only receives the output of the script run on the server. We do not see the source code since it is not sent to the browser.
We can also use the write method to send HTML tags and client scripts to the browser. For example, we could use:
<% response.write "<h1>" & strHello & "</h1>" %>
to send the HTML header tags to the browser. The equal sign (=)
is a shortcut key that can be used to replace response.write in
your code. We can save time by writing:
<% ="<h1>" & strHello & "</h1>" %>
However, you can only use the shortcut key for one statement. If the script
contains more than one statement, you cannot use the shortcut key. We could use
<% =Var1 %>, <% =Var2 %>, <% =Var3 %>, etc.
Your code runs faster if you do not bounce in and out of server scripts, so you
will probably not use this shortcut often. Also, ASP.NET does not allow the
inline script tags, so it is a good idea to get in the habit of not using them
and mixing HTML and VBScript commands. ASP.NET requires any HTML that appears
within functions to be written to the screen using the response.write function.
We can do the same thing in PHP with the following example:
<? echo "<h1>" . $strHello . "</h1>" ?>
Note: There are four differences in the ASP and PHP code in this example:
<? ?> and ASP uses the inline script tags
<% %>Server-side scripting is similar to client-side scripting. You can use any valid VBScript or JavaScript statement within a server-side script delimiter. However, the MsgBox function would not be useful as a server-side script since no one is sitting at the server to answer the message.
The following sample code uses server-side script to display the current weekday name, month name, day, and year. Remember since this is server-side script, it uses the server’s date and time.
<% response.write WeekdayName(Weekday(Date)) & ", " & _
MonthName(Month(Date)) & " " & _
Day(date) & ", " & _
Year(Now())
%>
We can do the same thing with PHP using the date function with the following code (see PHP Date Manual at http://us4.php.net/manual/en/function.date.php):
<?
$today = date("l, F d, Y"); (NOTE: l is a lower case L)
echo $today;
?>
Long VBScript statements can be broken into multiple lines by using the line
continuation character (_).
|
Syntax of Code |
Explanation |
|
|
<response.write WeekdayName(Weekday(Date)) & ", " & |
The Weekday
function is used to obtain the index number of the day of the week. |
|
|
MonthName(Month(Date)) & " " & _ |
The Month
function is used to obtain the index number to a name. |
|
|
Day(date) & ", " & _ |
The Day function retrieves the day of the month. |
|
|
Year(Now()) |
The year is obtained using the Now function instead of the Date function because when the Year function is applied to the Date function it returns the number “1899” when the actual year is “2000”. |
|
|
%> |
The ending server-side script tag |
|
You can also use control statements on the Web server to specify which HTML
is sent to the browser. BUT, we really don't want to break in and out of
server-side scripts as the book demonstrates in the following example.
<% if intA <> 0 then %>
<font color=”ff0000”>
<h1>The variable is not equal to zero so we print in RED</h1>
<% else %>
<font color=”00FF00”>
<h1>The variable is equal to zero so we print in Green</h1>
<% end if %>
If the variable intA is not equal to zero only the red print will appear, and if
the variable is equal to zero then just the green print will appear.
Check out the following EXAMPLE from our text on pages 256-258, which illustrates how to use basic VBScripting techniques in a server-side script.
Instead, the following example demonstrates a better way:
<%
if intA <> 0 then
response.write "<font color='ff0000'>"
response.write "<h1>The variable is not equal to zero so we print in RED</h1>"
else
response.write "<font color='00FF00'>"
response.write "<h1>The variable is equal to zero so we print in Green</h1>"
end if
%>
Actually, we might use a conditional expression to make rows in a table appear in alternating colors. For example, if we wanted to display data from a database in an HTML table and wanted alternating rows to have a white background and a light gray background, we could use the following code: (except that in this example, We used a For ... Next loop since we didn't actually load data from a database table)
<%
response.write "<table border=0 cellpadding=3>"
response.write " <tr>"
response.write " <td bgcolor='#00309c'><font color=white>The
is the field heading"
response.write " <td bgcolor='#00309c'><font
color=white>Heading for field #2"
response.write " <td bgcolor='#00309c'><font
color=white>Heading for field #3"
for x=1 to 10
if int(x/2)=x/2 then
'We have an even row (make it gray)
response.write " <tr>"
response.write " <td bgcolor='eeeee0'>x=" &
x & ", x/2=" & x/2 & ", int(x/2)=" & int(x/2)
response.write " <td bgcolor='eeeee0'>This
data is in a gray row."
response.write " <td bgcolor='eeeee0'>This
field is also gray in this row."
else
'We have an odd row (make it white)
response.write " <tr>"
response.write " <td bgcolor='white'>x=" &
x & ", x/2=" & x/2 & ", int(x/2)=" & int(x/2)
response.write " <td bgcolor='white'>This
data is in a white row"
response.write " <td bgcolor='white'>This
field is also white in this row."
end if
next
response.write "</table>"
%>
The same thing can be done with PHP with the following code:
<?
echo "<table border=0 cellpadding=3>";
echo " <tr>";
echo " <td bgcolor='#00309c'><font color=white>The is the field heading";
echo " <td bgcolor='#00309c'><font color=white>Heading for field #2";
echo " <td bgcolor='#00309c'><font color=white>Heading for field #3";
for ($x=1; $x <="10"; $x++) {
if (intval($x/2)==($x/2)) {
// We have an even row (make it gray)
echo " <tr>";
echo " <td bgcolor='eeeee0'>\$x=" . $x . ", \$x/2=" . $x/2 .
", intval(\$x/2)=" . intval($x/2);
echo " <td bgcolor='eeeee0'>This data is in a gray row.";
echo " <td bgcolor='eeeee0'>This field is also gray in this
row.";
} else {
// We have an odd row (make it white)
echo " <tr>";
echo " <td bgcolor='white'>\$x=" . $x . ", \$x/2=" . $x/2 .
", intval(\$x/2)=" . intval($x/2);
echo " <td bgcolor='white'>This data is in a white row";
echo " <td bgcolor='white'>This field is also white in this
row.";
}
}
echo "</table>";
?>
Note: There are nine differences in the ASP and PHP code in this example:
<? ?> and ASP uses the inline script tags
<% %> echo to write information to the browser. ASP uses document.write intval() function to get the integer of a variable,
ASP uses the int() function. { } == , and place code between
braces { }In addition to all of the scripting language objects, properties, and methods, server-side scripts have access to all of the built in ASP objects, properties, and methods. You can access built-in ASP objects in much the same way that you access objects in the Document Object Model. Objects and their methods and properties are referred to using dot notation.
response.write "..."
The following example uses the request object and the response object. This EXAMPLE demonstrates how to combine basic scripting techniques with ASP built-in objects.
|
Syntax of Code |
Explanation |
|
|
if request.form("txtName" <> "admin" or _ request.form("txtPass") <> "pass" then |
This line of code checks the login name and password from the browser. If it is not equal to the correct name and password then the form is shown again. |
|
|
<% else %> <h3 align="center">Welcome Back!</h3> <% end if%> |
If the correct password and login name are sent from the browser then Welcome Back will be displayed. |
|
Here is a PHP EXAMPLE that does the same thing.
Forms can be processed on the same page, or on a different ASP or PHP page. In the
example above, the action is 282ln07builtinObjects.asp or
282ln07builtinObjects.php which will call the same
page. The form method is POST. In order to use the Form collection (
request.form("txtName") ) to retrieve the value of a field on a form, you
must use the POST method as opposed to the GET method. If we used the GET
method, we'd have to use request.QueryString("txtName")
in ASP or $_GET['txtName'] in PHP . In PHP we used the $_POST['txtName']
rather than using the forms collection.
WATCH OUT! The GET method would expose the password to the public!
Check out this EXAMPLE on GET vs. POST. Check out the Forms Introduction by Charles Carroll.
The session object begins when the browser makes its first request for an ASP page. When the user closes the browser, the session ends, and the session variable no longer exists. The session can also end when the session timeout is reached.
|
Syntax of Code |
Explanation |
|
|
Session.Timeout = 30 |
This changes the timeout property to 30 minutes from the default 20 minutes. When the timeout limit is reached, the session will end, and all session-related information will be lost. |
|
|
Session.Contents("sStart") = Now() |
This assigns the current date and time to a session variable called sStart. |
|
|
strPathInfo = Request.ServerVariables("PATH_INFO") |
This virtual path returned from the server is returned from the PATH_INFO server variable and stored in the strPathInfo variable. |
|
|
strPhysicalPath = Server.MapPath (strPathInfo) |
The MapPath method is applied to the virtual path stored in the strPathInfo variable to retrieve the physical path. |
|
|
The virtual path to this file is: <br> <% = strPathInfo %> |
Displays the virtual path to this file. |
|
|
The physical path to this file is: <br> <% = strPhysicalPath %> |
Displays the physical path to this file. |
|
|
The session timeout has been set to: <br> <% = session.timeout %> minutes |
Displays the new timeout property. |
|
|
Your session started at: <br> <% = Session.Contents("sStart") %> |
Displays the time the session was started. |
|
|
Your session ID is: <br> <% = Session.SessionID %> |
This displays the session ID number. The SessionID property of the session is one way to identify the client to the Web server. |
|
The following EXAMPLE shows how to use the request object along with another built-in ASP object called the session object.
Many languages contain a method to include the contents of one file in another file. Server-side include files (SSI) are often used to insert code that can be reused for other Web pages, for example headers and footers. The server-side include file may contain header or footer information, style information, logos and images, constants, other expressions, formulas, or meta tag information. This content may be composed of HTML tags, text, or client- or server-side scripts. A server-side include file containing information that is displayed on the top of the Web page is often named “head.inc” or “header.inc”.
The SSI file is included before the ASP engine interprets the page. Therefore, we can't use ASP code to decide which SSI file to include within a Web page.
PHP include files are very similar. Let's look at both.
ASP |
PHP |
Creating the SSI File - ASPThe sample include file below (
The example is a simple mix of HTML and inline script. It should be saved
with an .inc extension and usually SSI files will be in their own directory
named, |
Creating the SSI File - PHPThe sample include file below (
The example is a simple mix of HTML and inline script. It should be saved
with an .inc extension and usually SSI files will be in their own directory
named, |
Including SSI Files in an ASP PageTo instruct the Web server to include a server-side include file, use an
We can specify a physical path by using the keyword file, or specify a virtual path with the keyword virtual. We use the keyword VIRTUAL to use a virtual path.
Physical paths can be an absolute path or a relative path. Virtual paths identify the location of the include file starting from the root of the Web directory. |
Including SSI Files in an PHP PageTo instruct the Web server to include a server-side include file, use an
|
Creating the Include Statement in the ASP PageThe include file above (
The The following LINK
will demonstrate the |
Creating the Include Statement in the PHP PageThe include file above (
The The following LINK
will demonstrate the |
Check out these links! There are LOTS of FANTASTIC resources on the Internet! Lots of sites that offer free support for ASP developers! Many have code that you can download and modify for your own use! Have fun!
And, here are some PHP Links with great tutorials and example code. Have fun!