CSC-111: Introduction to Information Technology page XXX
Rewrite to use ISSET
Also to use two pages all the time.
Lab 4: Basic PHP Tutorial, Part 2
This lab activity provides a continued overview of the basic building blocks of the PHP server-side scripting language. Once again, your task is to thoroughly study the code examples and experiment with them in order to understand the underlying concepts.
PHP examples
Recall from last week that all of the code in the grey boxes can be placed in a file with the extension .php and uploaded to a web server supporting PHP without modification. Your lab activity for today involves doing this to see what the code produces, then experimenting with the code to understand it thoroughly. All code examples can be found in the OUT folder with the file name indicated. Additional examples of the same concepts can be found at the CSC-111 web site (cs.furman.edu/~ktreu/csc111/examples.html) and in the lecture notes.
Create a folder called lab4 inside of your csc111 folder from last week. Copy all of the files for this lab (from the OUT folder) to this folder. Then use WinSCP to connect to your CS department web server account and upload the entire lab4 folder into your www/csc111 directory. You will then be able to run all of the files using the URL http://cs.furman.edu/~yourID/csc111/lab4/
Remember that none of this code will work unless it is first loaded to the CS department server. That is inherent in the concept of server-side scripting.
For each of the following examples, follow these steps:
o Upload the code to the web server. (You just did this.)
o Read the code and the description very carefully. Take your time. Ask questions. Make sure you understand what’s going on with the example.
o Use your browser to try out the code and see what it does.
o Use Dreamweaver or another text editor to make the suggested changes to the example to demonstrate that you know what’s happening.
o Remember that after making the changes you have to upload the file using WinSCP again.
0. Pre-lab exercise.
Your first task is simply to make an index page for your lab4 folder. Use Dreamweaver to make a new file called index.php and save it in your lab4 folder. Title the page “CSC-111 PHP Lab, Part 2” and create a bulleted list of links to the files that you just uploaded. Upload the index page to your lab4 folder on the server with WinSCP. Then use your browser to test the page and make sure all of the links work. When they do, proceed with the following tests and experiments.
1.
Using conditional statements and the query string to determine page content (file: 1ifelse.php)
This example reviews if-else control statements from last week, and also takes a first hands-on look at the GET method. Recall that $_GET is a variable in PHP that looks in the querystring for input. That input can either be placed there using a form, or it can simply be coded into the URL. The code above checks the URL querystring and, depending on the value of $_GET[‘grade’], prints an appropriate message. After you upload this file, try these different URLs with querystrings added:
· http://cs.furman.edu/~<yourID>/csc111/lab4/1ifelse.php?grade=95
· http://cs.furman.edu/~<yourID>/csc111/lab4/1ifelse.php?grade=45
· http://cs.furman.edu/~<yourID>/csc111/lab4/1ifelse.php?grade=101
Everything after the question mark in each URL is called a querystring. It’s actually giving the page a value that it needs. You access querystring values using the $_GET array. Do you see how it works?
EXPERIMENTS:
· First, try other values for “grade” in the querystring. Including no value at all. Then try changing the word “grade” to something else. What happens?
· Modify the example so it assigns a letter grade rather than just pass/fail. It should assign an “A” for 90-100, “B” for 80-89, etc. Be sure to test thoroughly when you’re done.
· NOTE: The link to this example on your index page should include a querystring with a value. In other words, when you click on the link for this problem, it should give an answer other than “invalid input given”.
2.
User-defined functions (file: 2adder.php)
Last week we saw how to use a function that PHP defines for us (the date function). Here we see how to create a function of our own to do our work for us. This is done with the keyword function, followed by the name of the function and a pair of parentheses. The parentheses contain the parameters, or data that is expected by the function. Note that PHP functions require the use of the return command. This is how you send back a value to the place the function was called. The function in this example looks like this:
function adder($num1, $num2){
return $num1 + $num2;
}
Here again we see the use of an HTML form, and a page that processes itself. (Remember the “hello” example from last week.)
EXPERIMENTS:
· Make the program subtract instead. Do this by writing an additional function called subtracter, rather than erasing the existing function. Once you have the new function, you simply have to change the function call, calling subtracter instead of adder.
· Finally, add a multiplier function and call it instead.
· Modify the program to use the get method and the $_GET array instead. (Then note how the querystring is used. Further note that you no longer need to use the form if you don’t want to.)
· Try having the addition done by a second PHP page. You might even find this simpler than the given example.
3. Session variables (files: 3sessiontest.php and 3sessiontest2.php)
A more advanced feature of PHP is the session variable. A session variable is a special kind of variable that maintains its value from page to page (that is, as a browser clicks from page to page in the same browsing session).
In this example we see the creation of session variable count, expressed as $_SESSION[‘count’]. The build-in PHP function session_start() tells the PHP processor that session variables are being used on this page. The “++” code simply adds one to the value of the session variable. Thus the result you get when you reload the page.
The next example includes a second use of the same session variable. Its value is the same as it was on the page that linked to this one.
Session variables are extremely useful in certain situations. An example might be a site on which users have greater access to data if they have a membership of some kind. When they log in, a session variable can be used to indicate that they are a member, thus allowing them to browse the site without having to log in again every time they change pages.
By the time you’ve reached this point, you should thoroughly understand the examples we’ve covered. If not, go back and experiment some more with the code. Try different things to see what works and what doesn’t. Ask questions.
When you’re satisfied, make sure your index page is uploaded and working, and go ahead and get started on the postlab activity.