 
PHP To The Rescue
PHP was designed to intercept the request for a web page,
and process the page first in the PHP pre-processor, and then
pass the adjusted page to the Apache server. If PHP does not
see any PHP script tags, then the page is passed without interference
to the server. PHP pages must be run from a web server, and cannot be tested locally like a standard HTML page.
Not only must your PHP page be on a web server to run, but ALL requirements for a PHP page to run must be met. These may include:
- The web server must be configured to run PHP pages
- You must upload the file into the proper directory
- Your page may need to be error free, or the server may need to be enabled to display errors
- Your page must have the proper file extension (.php)
PHP can be installed on a Windows machine, and even be set up to run side by side with other types of server pages such as JSP or ASP. Most commonly, one server side language is featured.
Developing With PHP
PHP pages can be built and edited with a simple text editor. If you are using a Windows machine, it can be as simple an application as Notepad. However there are several free editors that are excellent. My favorite (free) all purpose editor is Crimson. Free PHP specific editors include PHP Designer and DevPHP. Commercial products such as Dreamweaver also allow PHP (and other language) dynamic development.
If you are using a Mac, I have heard people use TextEdit, which is built in and free but not perfect. Another alternative is TextWrangler. There are several alternatives: Mac Editor Discussion
Uploading Your Files
Since our pages should be on the server to run properly, we need to be able to upload the files to the correct directory. For most Unix/Linux PHP servers, the directory is named public_html, referring to the fact that the files that go there are visible to the public, and are generally HTML files.
A program designed to upload files frequently uses a file transfer standard called File Transfer Protocol (FTP). For general file upload purposes I suggest the Windows FTP program smartFTP. For Macs, I understand Fetch is an alternative.
Some servers require Secure FTP (sometimes called SFTP), since all data sent over the internet is clear text (visible to anyone) by default. To keep others from viewing your passwords, a Secure FTP is recommended, where it is supported by the hosting company that provides your server space.
The server we are using for this class requires a Secure FTP application. Therefore, for Windows I recommend WinSCP (Secure Copy & FTP) (select standalone application). See the following discussion for Mac Software Mac SecureFTP Alternatives
Command Line Access
The last major software task will allow us to work with our files and directories, and later with the command line access to MySQL, the open source database we will be using for this class. The software I recommend for this task is puTTY On a Mac, I understand there is no need as there is a built in Secure Shell (SSH) in OSX: SSH on a Mac
Writing PHP
PHP code can be interspaced between regular HTML. As long as the script tags are opened and closed properly, there is no conflict.
<td> <? print "Place some PHP here!"; ?> </td>
There is a danger working with server side programming regarding the visible source code of the HTML page. While in a straight HTML environment we can grab source code, make changes and re-upload a page, remember that in PHP you have dynamic code, not a static page, and you may overwrite all your work!
Our First Pages
PHP has a function that will the PHP and Apache Versions, and all the settings selected for the PHP.INI file:
Here is an example that contains HTML interspersed with PHP code:
In our next example we used one of the date functions from the server. Note that the server time may be in a different timezone
than the user:
Now that you are using a server side language, we should keep the copyright info up to date on our web pages! To do this we query the server for the current year and insert it into the HTML:
<i>MyWebsite, © 2002 - <? print date('Y'); ?> </i>
The example looks like the bottom of this web page! Remember the keywords "print" and "echo" are synonomous and print the data to the page.
Since PHP script tags are the same as XML tags, to be able to print the script tags (instead of attempting to execute them as PHP) write the script tags WITH PHP:
<? php print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
Note the above example uses quotation marks inside the string. To be able to distinguish between the end of string and a quotation mark that we wish to print, we place an escape character, the "\" before every example of a quotation mark that needs to be printed to the screen.
IMPORTANT! Note that in the above example the last script tag is split up, and concatenated:
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?" . ">"
The 2 strings involved are split above for effect. They are concatenated by the period. The SECOND question mark is the end of the first string, and the ">" is a separate string. To attach them would be to inform the pre-processor of the end of the string prematurely.
Single Print Quote
An alternative way to use a single line (for PHP) but to allow the developer to write code in a way that is more familiar is shown below:
The advantage of this way to intersperse HTML inside of PHP is that you no longer need to escape the double quotes. This is because you selected the single quotes as indicator of the end of the string.
The XML tag at the top of the page is more simple in this manner:
<? php print '<?'xml version="1.0" encoding="iso-8859-1 ?"' . '>'; ?>
Safe Server Side Programming Habits
With normal HTML programming we are used to working with
individual pages. With server side code it is very common
to have many pages make up our site. We could have functions
included in many pages, dynamic navigational elements that
change on the pages and pages that only interface with the
database or send email, and are never visible to the user.
With all the extra pages possible, we must take steps to
be certain our project is well documented, from the start.
With our work, we will require the developer (that's you!)
track the purpose and contingencies of each page. (See Assignment
1 of the Homework Page).
Since PHP will not run unless the .php extension is used, always use the .php extension on your pages so you can easily add PHP code to a page at a later time without updating links.
Server Directory Structure
Careful use of the directory structure is an advantage to a developer. Below
is the suggested directory structure for your site, with some
descriptions of the purpose and contents of the directories:
| Directory Name |
Contents |
Note |
| / (the root, no sub folder) |
index.php page, main linked php pages |
End all pages with .php extension, for possible later
additional code. Remember the folder for web files is called "public_html". |
| /images/ |
all image files for the site, spacer GIFs, etc. |
Customary isolation of image files |
| /inc/ |
All included files, functions, etc. |
These pages are not visible, and will not be linked |
| /rwx/ |
all read/write/executable files |
Isolate any files writable files as the directory that
is writable presents security issues |
| /toolpages/ |
All test pages we are using for reference |
Separate these files from the necessary site files |
| /examples/ |
all example files we are working on |
ITC280 examples go here |
Zephir Server
PHP has been enabled on both the "edison" and "zephir" servers for SCCC. However, our MySQL databases will only be available via zephir. Please use Zephir to do your class work. Zephir has been set up to REQUIRE secure FTP.
When using WinSCP, the hostname is zephir.seattlecentral.edu. For the User Name use your student name (first initial, first 6 chars of your last name, with a possible replacement of a couple of numbers at the end). The password is the last 6 digits of your student ID. Remember the "public_html" folder represents the root of your web space. The root of your webspace can be viewed on the web (for example) at:
http://zephir.seattlecentral.edu/~username/
Where "username" is your login to Zephir. Note the tilde (~) before your user name. Any sub folders would need to typed in from here. Daily Folders
On your development machine, to keep from overwriting work,
and to store work on previous versions of dynamic pages, I
suggest keeping daily directories from which to work. This
means you create a different directory every day, on your
main development machine, with today's date, for example:
/09192004/
The above example would be for the 19th of September, 2004.
All of the dated directories should go under a single main
folder, perhaps named "Development", and stored
in a likely place for backup, perhaps "My Documents".
All work for the day would go into that folder, with work
for the next day to be stored in a folder with the next date
on it. A time may come in which you need to research all versions
of a particular page, and then you can search over all the
folders in your development environment. Backing up the contents
of all your daily folders and burning them to CDs at regular
intervals is advised.
Developer Habits
Backup your files. Backing up files in a server side
programming environment is so important that I will require
this for the class. Backing up your files does not mean storing
your only copy of a file on a floppy disk. By backup, you
must have a second copy of all critical files in more than
one physical location.
Document your code with extensive comments. At this
stage, it is difficult to imagine including too much documentation
in your code. Make sure there is a basic description of the
page functionality, with dependent pages (if any) listed as
well.
Always complete your curly braces before you fill in the
applicable code. It is difficult to troubleshoot for this
issue, if you don't start with this habit early.
Never work directly on a floppy disk/Network Drive/Thumb Drive. Work on a hard
drive, and copy your data to a floppy/Thumb drive, and to the server,
thereby fulfilling your "backup" requirement. Never work (in ANY language or even with a critical WORD document), directly on a volatile drive, as you may lose ALL your work!
Create your PHP Spellbook. Create a folder or book
with printed copies of all important examples and pages you
are working on. Having a printed copy allows you quick access
to work you have already created, and allows you to highlight,
or draw arrows or notation on a page in a manner not possible
in electronic form. The Spellbook is not a requirement for
the course, but is highly recommended.
|