PHP for Domino Developers, Part 2, Files

Jake Howlett, 6 October 2003

Category: LAMP; Keywords: LAMP PHP MySQL

This article is the second part of a series of articles that aim to introduce the LAMP platform to Domino Developers. In the first we looked at how to get MySQL (the M of LAMP) up and ready to be used as our database. In this article I will look at PHP (the P of LAMP) and how we get working with what, to us, is probably the most interesting of the four technologies involved.

PHP is the language we will use to write all our code and it will do all the work in our applications. It will create the dynamic content in the pages that the user sees as well as interacting with the MySQL backend to enter and retrieve data. It is a server-side language. It works by parsing a page of HTML markup for any PHP tags. When it finds them the PHP engine evaluates the content and replaces it with normal HTML. To look at the source of a PHP page in the browser will show no trace of any PHP code.

Creating a PHP Page:

A PHP page is nothing more than a simple text file, just like its poorer HTML counterpart. The difference is that HTML files are static, while PHP allows us to make our pages dynamic, just like we're used to with Domino.

How you create the pages is your choice. There's a load of IDEs you can choose from. Personally, I like to keep it simple and use my favourite text editor, TextPad, as in the shot below. Shown is a simple example of a PHP page:

Simple PHP Page

The result being a page that tells the user what their IP address is. Don't worry about the $_SERVER bit too much. Just notice the <?php opening tag and ?> closing tag. These are what PHP looks for whenever it parses a page that ends in .php.

Out of interest, the equivalent in Domino would be a form with a self-referencing computed field on it. Something like this:

Simple Domino Form

You've probably noticed something already. It's easier with Domino, where there's hardly any coding required. However, notice something more subtle. With the PHP page we have complete control over the whole of the HTML that gets produced. Not important to a lot of us but just think of what this means. Think of all the hacks you've seen to try and get Domino to render just the way you like. Think, for one, of the infamous "No Documents found" message. Now think of what you could do if all the HTML was created by yourself. Anything!

We will get to talking more about PHP later on. For now we need to worry ourselves with getting the pages on to the web server. Unlike Domino, where the whole of a website is usually contained within one NSF file, PHP sites are normally made up of many folders and separate files.

Adding your PHP pages to the server:

If you're not convinced that Domino is easier, you soon will be. With Domino, we're all familiar with the nice GUI IDE called Domino Designer. This lets you create and edit forms directly on the webserver or you can develop locally and replicate to the server at the push of a button. With PHP you need to get your files on to server manually. This normally mean using an FTP client. Which obviously means you need an FTP server running at the other end. Let's get this bit out of the way first.

As in the first article, I am assuming you are using a separate server and that it's Linux (Red Hat 9 in particular). If so, you should have an FTP server installed by default. It may not be running though. To check, login to the Linux box and open the Service Configuration panel. Find the FTP daemon and make sure it's running. In my case it's called VSFTPd and wasn't enabled by default. Add a tick to the side of it to make sure it starts whenever the machine does. In the image below it's enabled and running.

Red Hat Service Configuration panel

The server is now ready for you to upload files to it. Before you do this you need to make sure you have the access to do so. If you haven't already, add a user account for yourself to the Linux server. I did this and used the username jakehowl. Now we need to make sure that this user has permission to upload files to the Apache server's HTML directory. In my case, this folder is /var/www/html. You can see in the image below that I am the owner of that folder:

Apache HTML direcotry ownership

To find out, I issued two shell commands in a Terminal console. First, the command cd /var/www change to the folder that contains the Apache HTML directory. Second, the command ls -la to list all the files in this folder, along with their ownership details. Obviously the files had different owners when I first installed the server and I had to make the changes myself. To do this I had to issue the following command:

chown jakehowl html

This changes the owner of the HTML folder to me. Alternatively, if your server is on a local network and security isn't your main concern, you could simply allow everybody write permissions to that folder. This requires a command like this:

chmod o+w html

This adds write permissions for the set of users known as "other". i.e. anybody who's not the owner or in the specified group. However you choose to do it, you need to make sure you can upload files to this directory from an FTP client.

For an FTP client, I use Cute FTP Pro. There are free alternatives and you can even ftp from the DOS prompt if you feel confident. Whatever you use, the setup and principles and pretty much the same. I will use Cute FTP for the sake of demonstration. First thing you need to do is add a new "site".

New FTP site in Cute FTP

Whatever you use, you will see something similar to the following dialog where you have to enter the server's address and the user name you want to connect as. If you gave everybody write access, then you can leave the Username field blank and connect using the Anonymous login method.

New FTP site in Cute FTP

As an option you can tell Cute FTP to automatically go to Apache's HTML folder when it logs in. You can also switch your local folder to wherever you are creating your PHP files.

New FTP site in Cute FTP

When you've filled in all the details, click connect and you should see something like below. On the left is your local machine and a list of PHP files in your working directory. On the right is the remote server and all the files in the Apache web directory. To move your files to the server it's a simple case of double-clicking them.

FTP site connected in Cute FTP

You now have a fully working web development setup. You're ready to start creating PHP pages. We will get to this bit in the next article. However, if you can't wait and want to test your setup, save this file to your local machine, FTP it to the server and then open the file through the browser. For example, if you add a file called test.php to the root of /var/www/html on a server called epswebsvr01, then the URL is simply http://epswebsvr01/test.php. This test.php page uses a PHP function called phpinfo() which tells you everything you never wanted to know about your install. At least, it's a good way of testing your PHP config is working. You should see something like this:

Test PHP page

If you don't see this then your PHP/Apache configuration is incomplete. Either PHP isn't installed or Apache isn't properly configured to process PHP pages. Either way, it's beyond the scope of these articles to troubleshoot your installation. If you have problems, turn to one of the many help forums out there. Just like with the Notes.net forums, the LAMP community can be just as welcoming to newcomers.

Summary:

Yet again, I've finished an article about PHP and not really talked much about any actual code. Sorry about this. I know most of you are competent enough to work out the basics of MySQL and FTP on your own. However, I really want these articles to be for Domino Developers. To do this I am assuming zero knowledge outside of Domino, in terms of web server basics.

Even as a Domino Developer who has dabbled in other web technologies in the past, there are still things that catch me out. There are a few things that I've covered in these first two articles that I hope will save some of you at least a few hours of soul-searching.

Now that we have the basics of MySQL and FTP covered we can start getting our hands dirty. The next article will look at using PHP to connect to MySQL databases. The article after that will look at creating what we know as forms. Then views. Then searching. And so on. Bit by bit I hope to cover all the basics of a Domino website as you would create them using PHP and MySQL. All subsequent articles will assume you have read, understood and implemented the content of these first two articles.

What I've yet to decide is how to go about the next set of articles. I am thinking of building a simple demo website, bit by bit, as we go along. What I can't decide is what that application should be. Something familiar to us all? Maybe something along the lines of the Personal Journal template (journal5.ntf).