Publishing with PHP
- What is PHP?
- What can I use PHP for?
- I've never programmed before. Should I bother with PHP?
- How can I write PHP?
- What about PHP and the UVM magicscript template?
- Where should I place my PHP code to get it to work?
- What useful built-in information does PHP have that I can use in my script?
- I want to make my script load as my default page. How can I do this?
- What about JSP and ASP?
- What about Perl?
- PHP resources and useful links
- I'm getting a SAFE MODE warning when I try to view my page
- It says I'm getting a "Parse error" on line X - what is that, and how do I fix it?
- I'm getting a "Failed opening" error in Unknown on line 0 - what does that mean?
- I'm getting a "403 Forbidden" error - how can I fix that?
- I'm getting a "404 Document Not Found" error - what's wrong?
- I'm having trouble with setting permissions and / or accessing my scripts in UNIX - who can help?
What is PHP?
PHP is a "server-side cross-platform HTML embedded scripting language" that you can use to enhance your website by providing dynamic content. It also has some limited Object Oriented capabilities. If the above definition doesn't mean anything to you, here's a brief rundown:
Server-side: the script resides on the server and is executed from there (unlike Java Applets or Java Script which are client-side and are executed on the user's home computer). This reduces processor load on the client's computer and protects the actual code from being viewed by end users (who could potentially steal or abuse such information).
Cross-platform: PHP will work under multiple operating systems. For clients (users), this means that no matter what operating system they're using (Windows, Linux, MacOS, etc.), the same script will execute in the same way every time. This adds versatility to PHP's traits.
HTML embedded: PHP code can exist in documents that also contain straight HTML.
Object Oriented: PHP code can be modularized to organize data into logical groups of variables (and functions that act on those variables) that represent real-world entities. This allows for more intuitive programs structures, cleaner code, and easier code-reuse, among other things.
What can I use PHP for?
You can use PHP to:
- Collect, manipulate, and store information from people who visit your website;
- Gather statistics on people who visit your website;
- Interface with UVM's online databases (using read-only access), and your own personal or departmental database (using read/write access);
- Create interactive websites and web-based applications;
- Do a bunch of other stuff - use your imagination!
I've never programmed before. Should I bother with PHP?
Like most questions, the answer to this one is "it depends." If you enjoy solving problems, feel comfortable working with computers, and like the idea of writing computer code, then you should try it out. PHP is a relatively easy language to learn, if a little stylistically sloppy (as opposed to languages like C that are very strict). If on the other hand, you feel uncomfortable with the idea of writing computer programs, don't honestly enjoy solving problems, and sitting in front of a computer for potentially many hours trying to fix bugs in a script sounds quite boring to you, you will probably get frustrated with PHP and not enjoy it very much at all.
Keep in mind that if you are somebody that wants a script written for your site but don't think you'd be interested in writing one yourself, there are people whom you could hire to write one for you. Please note that the people at the Web Team will only be able to help create or modify existing scripts that have a wide-reaching impact at UVM. They are not equipped to write scripts to process departmental form data, or other specialized applications.
How can I write PHP?
PHP application scripts are designated by the "php" file extension. So if you were to create a PHP script that just prints the text "hello world," you might want to name it "helloworld.php." (You don't need to name it helloworld.php, but I'm calling it this for the sake of example). This way, the server knows to interpret your script using the PHP interpreter (because of the php extension), and also so that you as well as users know what kind of information is contained inside the file.
Generally, PHP scripts are written using text editors. The best choices for text editors are probably pico if you're on a UNIX system, EditPlus if you're using a Windows machine, or BBEdit if you're on a Mac. You can really use whatever text editor you want, but be certain that it will not generate any "funny" characters in your document. This means that Microsoft Word or any other run-of-the-mill word processor are not good choices! Windows Notepad will work fine in a pinch. In any case, you should try to use a text editor that lists line numbers - this will provide an invaluable service when debugging your programs, as errors are reported by the line number on which they were encountered.
A very basic PHP script (using the helloworld.php example from above) might look like this:
<?php print "hello world."; ?>
which might be called from the following URL:
and would display the following to the screen:
However, you will certainly want to write scripts that do more than just write "hello world" to the screen - besides, if this is all you wanted to do, you could just create an HTML file to do it, and with less fuss! The entire point of using scripts instead of plain HTML files is that scripts are interactive while HTML files are not. We can do many things with scripts because of variables.
Variables are data structures that store dynamic information - that is, their values can change and may be manipulated by the script. Users have the ability to change the values of any variables that are not acquired from other sources (e.g., the User's IP address - $REMOTE_ADDR - is acquired from server resources, so users cannot change this information like other variables). For example, if we wanted to pass information to the helloworld.php script that displays the user's first name, we might modify the script in the following way:
<?php print "hello world. Your first name is $firstname."; ?>
Note that a $ placed immediately before a word (in this case, firstname) indicates that the word is in fact the name of a variable, and that the program should reference whatever value is stored within. The code above would now be called from the following URL:
Note the parameter that's attached to the end of the script's filename. The ? indicates that parameters are being passed, which is then followed by N sets of variable=value pairs, each separated by an ampersand (&). In this case, this parameter is assigning the value "john" to the variable "firstname." FYI, this is representative of a URL that is generated by a FORM GET method.
The script, being passed a value for the variable firstname, would display the following to the screen:
hello world. Your first name is john.
Note that in the above examples, zoouser should be replaced by your Zoo username.
What about PHP and the UVM magicscript template?
It is easy to run PHP code on pages that run under the UVM magicscript template. If you want your default page to be a PHP script, you should create a file "default.php" that will replace the main body file "default.html," and it should be stored in the same directory as default.html. In default.php, you can enter HTML as well as PHP code, and it will execute correctly under the template. Note that if you have a default.php file in your directory, it will be opened instead of default.html (i.e., default.php has precedence over default.html).
You can also call other PHP scripts (i.e., not default.php) explicitly from the magicscript, so that they will not appear as the first page that users see when they visit your website. If for instance, you have the magicscript installed in your public_html directory and you want to run a script thisscript.php from within the template, you might load it using the following URL:
Note that Page is the name of the variable that's passed to the magicscript, and contains the value "thisscript.php." Incidentally, if you open the magicscript without specifying a value for Page, default.php (or default.html) is loaded.
Where should I place my PHP code to get it to work?
Your PHP files may be stored anywhere that ordinary HTML files would be stored. This includes your public_html directory and any of its subdirectories.
What useful built-in information does PHP have that I can use in my script?
There are a LOT of global variables that you can use to gather basic information about those who access your website, as well as information about the server you're on, etc. The names of the more frequently used variables are given below. For a complete list of global variables and other PHP settings, see http://www.uvm.edu/phpinfo.php.
$REMOTE_ADDR The User's IP Address $HTTP_USER_AGENT Info on the User's Web Browser and OS $PHP_AUTH_USER The User's Zoo Username (only available when using DCE/DFS authentication) $REQUEST_URI The URL (with parameters) that called the current script $SCRIPT_NAME The URL (without parameters) that called the current script
I want to make my script load as my default page. How can I do this?
If you want your script to be your default page for a website that uses the UVM magicscript template, click here. Otherwise, for websites that do not use the magicscript template, read on.
The best way to make it so that you can load your script (say homepage.php) as the default page (e.g., http://www.uvm.edu/~zoouser/ instead of http://www.uvm.edu/~zoouser/homepage.php) is by performing one of two different actions.
The first is to rename your script to index.php. The server looks for the index file when you open a website without specifying a file to open. If it finds one, it will open it; otherwise, it shows a list of all files and directories within the specified location. You can do this by executing the following command:
mv yourscript.php index.php
The second method is to create a symbolic link to your script. This is good if you don't want to rename your script for whatever reason. You can do this by executing the following command:
ln -s yourscript.php index.php
Where yourscript.php is your script's filename.
What about JSP and ASP?
JSP (Java Server Pages) and ASP (Active Server Pages) are not supported on www.uvm.edu.
What about Perl?
Perl is supported on www.uvm.edu. Perl scripts are those that have a "pl" extension (e.g., yourscript.pl). In order to make a Perl script available on the web, you must place it into your cgi directory (accessible at http://cgi.uvm.edu/cgi-bin/zoouser/script.pl where zoouser is your Zoo username). If you do not have a cgi directory, you can create one. Just follow the instructions outlined at Using CGI Scripts .
I'm getting a SAFE MODE warning when I try to view my page.
This means that your script is trying to access a file or directory to which it does not have permission to access. This will happen when your script tries to open a file that is owned by another user. This is a security feature that by default is enabled to prevent others from accessing your personal information, but may be disabled on certain directories should your department have good reason to allow it. If your department has encountered this warning and it is a persistent problem, you may want to contact the IT at firstname.lastname@example.org to disable safe mode for your scripts directory.
It says I'm getting a "Parse error" on line X - what is that, and how do I fix it?
Parse errors are caused by bad PHP code. All it means is that you have an invalid statement (or multiple invalid statements) in your code that you need to fix before PHP can execute the commands. With each parse error, you will be given a line number and a short verbose description of the error that should be helpful in locating and solving the problem.
Generally these kind of errors are caused by misspelling a function, not sending enough (or sending too many) parameters to a function, not having a semicolon at the end of a statement, too many or not enough parentheses, etc.
I'm getting a "Failed opening" error in Unknown on line 0 - what does that mean?
These warnings indicate that the permissions on your script are not set correctly. Be certain to set world read permissions on your script by typing the following in your script directory:
chmod 644 yourscript.php
where yourscript.php is the name of your script.
I'm getting a "403 Forbidden" error - how can I fix that?
This error is caused by directory permissions not being set correctly, resulting in your web browser not being able to access your script. You can fix this problem by going to the parent directory of your scripts directory (so if your scripts are located in public_html/scripts/, you would go to public_html/), and typing the following command:
chmod 755 scriptsdir
where scriptsdir is the name of your scripts directory.
I'm getting a "404 Document Not Found" error - what's wrong?
Look closely at the URL in your web browser - there is something wrong with it. This error occurs when you have requested a document or script that does not exist on the server. Keep in mind that the URL is case insensitive except for the document or script name. This means that you can change the case of the whole URL until you get to the last "/" - after that, everything's case sensitive. For example:
are completely different scripts in the eyes of the UNIX operating system. If you're sure that your script is being spelled correctly and is all in the right case, check to make sure that your URL is pointing to the right directory.
I'm having trouble with setting permissions and / or accessing my scripts in UNIX - who can help?
You should contact the UVM Computing Helpline at email@example.com or by calling 656-2604. They will be able to assist you with setting file and directory permissions on Zoo, or with troubleshooting other basic problems with your script files. They will NOT help you write the code in your PHP scripts! If you need help writing the code in your PHP scripts, please see the resources above.
- I'm getting a SAFE MODE warning when I try to view my page.
Last modified December 05 2011 10:03 AM