Introduction to PHP


It's hard to argue that PHP Hypertext Preprocessor (PHP) has taken the web development world by storm. PHP is a lightweight, easy-to-learn, and quickly deployed open-source scripting language with a number of convenient features. The language's lineage is Perl, C, and Java, so many developers familiar with those languages will find making the switch very easy.

PHP works very well with many database solutions, but it is almost always mentioned in the same breath as the open source MySQL database (discussed with greater detail in the next chapter). PHP can run on Windows, Unix, and Mac servers; it supports IIS, Apache, and other Web servers. As for functionality, PHP 5 has limited support for object-oriented programming, nesting of modules, and custom functions. It has a rich set of built-in tools that let you access XML files, mail servers, database archives, Java APIs and objects, the server's file system, and a lot more.

In this section, you'll get a firm grasp on PHP. You'll learn how to install it, how to create a simple page using PHP, understand how IIS and PHP work together, and how to configure a site for use with PHP. Finally, you'll learn about the many features that Dreamweaver exposes for working with PHP as well as configuring Dreamweaver to work with PHP and MySQL together.

Installing PHP

Installing PHP is simply a matter of visiting the PHP website, downloading an installer, and running it. Configuring PHP to work in conjunction with Dreamweaver and MySQL is a bit more involved, but that's a process we'll get to later in the chapter. For now, let's walk through the process of installing PHP using the installer so that we can create and process a simple PHP page in the next section. To install PHP, follow these steps:

1.

Open a browser and navigate to the site http://www.php.net.

2.

On the site, click the Downloads link located near the top of the page in the main navigation bar.

3.

On the Downloads page, click the PHP 5.0.4 installer link located in the Windows Binaries category. You are redirected to a site mirror to begin your download.

NOTE

As of this writing, PHP's current version is 5.0.4, with a release date of March 31, 2005. Don't worry if your version is different; as long as the major version is 5, everything should function smoothly.

4.

Click any of the country-based links to begin your download. When the download completes, proceed to the next step.

5.

Locate the php-5.0.4-installer.exe file and double-click it to begin the PHP installation.

6.

When the Welcome screen appears, click Next.

7.

Agree to the license agreement by clicking the I Agree button.

8.

The next screen allows you to pick from either a standard or advanced installation. Choose the standard installation and click Next.

9.

Choose the default folder in the root of your C:\ drive for PHP to install to. By default, PHP attempts to install to C:\PHP. This directory is as good as any, so click Next.

10.

Choose the default SMTP virtual mail service that PHP should use when dynamically sending emails. Use Localhost as the mail server, but enter your own custom email address in the "From" text box

11.

Choose the web server to which you'll be installing PHP. Unless you're using Windows Server 2003, choose the Microsoft IIS 4 or higher option. If you're specifically using Windows Server 2003, click the Microsoft IIS 6 or higher option. Click Next.

12.

You'll be directed to click Next one last time to begin the installation procedure. Click Next to begin.

When you've completed the installation, the installer alerts you that script mapping has been set. Essentially, the installer configures IIS so that files with the .php extension are processed using the php-cgi.exe executable located in the C:\PHP folder. Similar to the way asp.dll, asp_isapi.dll, and jrun.dll process their respective pages, php-cgi.exe is the executable used to process PHP pages.

Aside from defining script mappings in IIS, the installer places necessary files in the C:\PHP folder. The installer also creates an important configuration file in the C:\Windows folder called php.ini. More on configuring this file in the next chapter.

Creating a Simple PHP Page

Now that you've had a formal introduction to PHP and you've learned how to install it, let's walk through the process of creating a simple PHP page. Not only will this process help familiarize you with the technology, you'll also get an understanding of how IIS handles the processing of a dynamic PHP pages in conjunction with the php-cgi.exe executable. To create a simple PHP page, follow these steps:

1.

Create a new folder in C:\Inetpub\wwwroot called DorknozzlePHP.

2.

Open Notepad (we'll get to Dreamweaver later).

3.

In the Notepad document, add the following code:

 <html> <head> <title>Sample PHP Page</title> </head> <body> <h1>This is plain text</h1> <h1><? echo "This is PHP writing text to the browser dynamically" ?></h1> </body> </html> 

4.

Save your work as sample.php in the new folder named C:\Inetpub\wwwroot\DorknozzlePHP.

TIP

If you plan on trying out numerous server-side technologies, it's beneficial to create the folder with the Dorknozzle name followed by the three-letter server-side technology you plan to use. This convention prevents you from continuously having to redefine the site for every server-side technology.


To test your work, open the browser and type the URL http://localhost/DorknozzlePHP/sample.php in the address bar. As you can see from Figure 22.15, two lines of text appear in the browser.

Figure 22.15. Two lines of text appear in the browser.


Although this result might not seem all that awe inspiring, think again: You've just created your first dynamic web page using PHP. Look at the code again and see whether you can pick apart the dynamic portions of the page. For the most part, 8 of the 9 lines in the page are plain old HTML that IIS does nothing with. It simply lets the browser parse the HTML tags and presents to the user the text inside them. Line 7, however, works a bit differently:

 <h1><? echo "This is PHP writing text to the browser dynamically" ?></h1> 

In this case, we use what's called a code render block to dynamically display the text This is PHP writing text to the browser dynamically within a <h1> HTML tag. IIS, recognizing that this code render block exists, intercepts the request and calls for help from the php-cgi.exe located in the C:\PHP folder. The php-cgi.exe file processes the request and interprets the code render block to mean, "echo, or print out, the text This is PHP writing text to the browser dynamically" on the page. This response is sent back to IIS and ultimately back to the browser, formatted using the <h1> tag. Although the process may seem complex, it happens so fast that a user rarely even knows that a dynamic page is being used.

See how easy that was? In this example, we manually wrote PHP code. The beauty in using Dreamweaver is that you don't have to write code at all. Dreamweaver writes all the necessary code for you. More on this later. For now, let's focus on getting our project configured so that we can use PHP to create dynamic Dorknozzle pages.

Configuring Dorknozzle to Run Under PHP

Up to this point, we've been working primarily from C:\Dorknozzle\<chapterfolder>. From now on, we'll work from C:\Inetpub\wwwroot\DorknozzlePHP (assuming that you're using PHP). Because this is the case, we need to reconfigure the site definition to point to the new folder we've created. Furthermore, we need to configure the Testing Server category in the Site Definition window to provide Dreamweaver with information specific to the technology you plan on using for the defined site. To configure Dorknozzle to run under PHP, follow these steps:

1.

If you haven't downloaded the files for this chapter, do so now. Open the folder for Chapter 22, copy the contents out of the folder, and paste them into the newly created DorknozzlePHP folder in C:\Inetpub\wwwroot. Then you will have all the assets, images, library items, and an index.php page you need to get started. After you've copied the files over, you should have Assets, Images, and Library folders including a CSS file, the index.php page, and the sample.php page you created in the previous section.

2.

Open Dreamweaver if you haven't already done so and choose the Manage Sites option from the Site menu.

3.

Select the existing Dorknozzle site from the list and click Edit. The Site Definition for Dorknozzle dialog appears.

4.

In the Site Name text box, rename the site from Dorknozzle to DorknozzlePHP.

5.

Browse to the C:\Inetpub\wwwroot\DorknozzlePHP\ folder in the Local Root folder category. Leave everything else as is.

6.

Switch to the Testing Server category.

7.

Select the PHP MySQL option from the Server Model category.

8.

Choose the Local/Network option from the Access menu. New properties become available that allow you to configure the location of the remote folder as well as the URL prefix for the remote folder.

9.

Browse to the same C:\Inetpub\wwwroot\DorknozzlePHP\ path in the Remote Folder text box. In most cases, this value is pre-populated when you select the Local/Network option from the Access menu.

NOTE

When working with web applications, the ideal scenario is that you'll have a development machine, a production machine (server), and a testing server. Realistically, not everyone can afford another server just for testing purposes. Most of us will rely on the development machine to function as a testing server as well. For this reason, we'll simply configure our testing server with the same information as our development machine. If you do have a dedicated testing server, you'd probably still choose the Local/Network option from the Access tab; however, rather than finding the machine locally, you'd browse to it on the network.

TIP

As you've see in the Access menu, other options are available for defining remote access to the testing server. As when you select the Remote Info category, when you select FTP and WebDAV, you're presented with options for configuring the Host, Host Directory, Login, Password, and so on. The RDS option is specific to ColdFusion; don't worry about that option when you're working with PHP.

10.

Enter the URL to the Dorknozzle site in the URL Prefix text box. This value should read http://localhost/dorknozzlephp/. Remember that localhost, like every other domain name (such as yahoo.com and google.com) is a URL accessible from a browser. Rather than accessing the site from a server miles away, the browser knows that localhost (and the IP address 127.0.0.1 associated with it) is the URL for the local instance of IIS (your computer). Because this is the case, it doesn't have to look too far for your files.

11.

Click OK to close the Site Definition for DorknozzlePHP window.

12.

Click Done to close the Manage Sites dialog.

You're now ready to begin building dynamic Dorknozzle pages using Dreamweaver and PHP!

Using PHP in Dreamweaver

Depending on the server technology you decide to use, features exposed in Dreamweaver's interface will change. For the most part, however, you can bank on the fact that the following features are always available when working with any server-side technology:

  • The Insert Bar: A visual representation of objects available in the Insert menu, the Application category in the Insert bar allows you to visually insert various types of dynamic objects onto your page.

  • The Insert menu: Use the Application Objects and PHP Objects submenus in the Insert menu to insert both generic application objects as well as PHP-specific objects.

  • The Application Panel: Divided into four tabs (Databases, Bindings, Server Behaviors, and Components), the Application panel provides the means for connecting to and accessing database data, binding that data to elements on the page, and also accessing the various application objects that were also available from the Insert menu. When working with dynamic pages in Dreamweaver, you should always have this panel open.

    NOTE

    Although the Components panel is available as an option, it is inactive under the PHP server model. For the most part, this option is available only under the ASP.NET and ColdFusion server models.


  • The Tag Chooser: If you want to make fine-tuned changes to your PHP code, you can choose specific tags from the Tag Chooser. Available by choosing the Tag option from the Insert menu, you can choose an option from the Tag Chooser to open the Properties dialog for the particular tag, allowing you to further customize attributes of the tag.

  • The Reference Panel: If you ever need help regarding the various PHP objects, application objects, or tags, you can reference them from the Reference panel, available by choosing the Reference option from the Window menu.

Earlier, I mentioned that most of the dynamic functionality built into Dreamweaver is the same regardless of the server-side technology you decide to use. This becomes obvious with the Application Objects submenu in the Insert menu and the Application category in the Insert bar. The objects listed in these menus expose generic functionality that remains consistent regardless of server-side technology. What does change is the option below the Application Objects option in the Insert menu. This option (which varies from ASP Object, ASP.NET Objects, ColdFusion Objects, and PHP Objects, depending on which server-side technology you decide to use), changes and displays specific content accordingly.




Macromedia Dreamweaver 8 Unleashed
Macromedia Dreamweaver 8 Unleashed
ISBN: 0672327600
EAN: 2147483647
Year: 2005
Pages: 237
Authors: Zak Ruvalcaba

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net