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:
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:
|
4. | Save your work as sample.php in the new folder named C:\Inetpub\wwwroot\DorknozzlePHP. |
TIPIf 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.

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.
<h1><? echo "This is PHP writing text to the browser dynamically" ?></h1>
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.NOTEWhen 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.TIPAs 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.NOTEAlthough 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.