Lab 4: Basic PHP Tutorial, Part 2

Similar documents
5.6 Rational Equations

Basic Uses of JavaScript: Modifying Existing Scripts

Café Soylent Green Chapter 12

CS4604 Prakash Spring 2016! Project 3, HTML and PHP. By Sorour Amiri and Shamimul Hasan April 20 th, 2016

Café Soylent Green Chapter 12

Lecture 6: More Arrays & HTML Forms. CS 383 Web Development II Monday, February 12, 2018

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 03 Minor Subject

Decisions, Decisions. Testing, testing C H A P T E R 7

How to Create a NetBeans PHP Project

Creating and Updating Websites In-Service Training Supplement

CMSC 201 Spring 2017 Lab 12 Recursion

CAL 9-2: Café Soylent Green Chapter 12

BEGINNER PHP Table of Contents

Authoring World Wide Web Pages with Dreamweaver

Form Processing in PHP

Enhancing Web Pages with JavaScript

HTML 5 Form Processing

A QUICK GUIDE TO PROGRAMMING FOR THE WEB. ssh (then type your UBIT password when prompted)

CMSC 201 Fall 2016 Lab 13 More Recursion

First Simple Interactive JSP example

Developing Online Databases and Serving Biological Research Data

CS 051 Homework Laboratory #2

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Some things to watch out for when using PHP and Javascript when building websites

E-Business Systems 1 INTE2047 Lab Exercises. Lab 5 Valid HTML, Home Page & Editor Tables

Lab 5: Dreamweaver CS5, Uploading your Web site

Café Soylent Green Chapter 1

Submitting Assignments

Table of contents. Pure PHP/ASP Upload AddOn Pack Manual DMXzone

USQ/CSC2406 Web Publishing

STA 303 / 1002 Using SAS on CQUEST

PIC 40A. Lecture 19: PHP Form handling, session variables and regular expressions. Copyright 2011 Jukka Virtanen UCLA 1 05/25/12

Fasthosts Customer Support An Introduction to PHP Scripting

Introduction to web development and HTML MGMT 230 LAB

Café Soylent Green Chapters 4

Online Grant Application Guide

Programming Lab 1 (JS Hwk 3) Due Thursday, April 28

Introduction. Installation. Version 2 Installation & User Guide. In the following steps you will:

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

GradeConnect.com. User Manual

CSC 101: Lab #1 HTML and the WWW Lab Date: Tuesday, 1/26/2010 Report Due Date: Friday, 1/29/2010 3:00pm

Adobe Dreamweaver CS3 English 510 Fall 2007

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

How to Download and Install R The R software can be downloaded from: Click on download R link.

Eclipse Environment Setup

Lecture 10: Working with Files. CS 383 Web Development II Monday, March 12, 2018

How To Upload Your Newsletter

Web Focused Programming With PHP

Things to note: Each week Xampp will need to be installed. Xampp is Windows software, similar software is available for Mac, called Mamp.

Lecture 3: Web Servers / PHP and Apache. CS 383 Web Development II Monday, January 29, 2018

Web Engineering (Lecture 08) WAMP

Writing Perl Programs using Control Structures Worked Examples

Beginning HTML. The Nuts and Bolts of building Web pages.

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Content - Creating New Topics (6/2016)

CMSC 201 Spring 2018 Lab 01 Hello World

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

CSC 101: Lab #1 Introduction and Setup Due Date: 5:00pm, day after your lab session

Create-A-Page Design Documentation

Web Design Competition File Upload Tutorial

Lecture 5. Defining Functions

1) Log on to the computer using your PU net ID and password.

COMP s1 Lecture 1

Agenda. My Introduction. CIS 154 Javascript Programming

Introduction to Programming in Turing. Input, Output, and Variables

FAU. How do I. Post course content? Folders

cwhois Manual Copyright Vibralogix. All rights reserved.

HHH Instructional Computing Fall

CS 1110: Introduction to Computing Using Python Loop Invariants

Adding Pages. Adding pages to your website is simple and powerful! In just a few minutes you can create a page that: Highlights a special event

Download the script. Download and Configure Formmail.PHP

Website Development (WEB) Lab Exercises

Easy Chair Online Conference Submission, Tracking and Distribution Process: Getting Started

Variables: Objects in R

PHP with data handling

YCL Session 4 Lesson Plan

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Dreamweaver 101. Here s the desktop icon for Dreamweaver CS5: Click it open. From the top menu options, choose Site and New Site

PrintShop Mail Web. Web Integration Guide

Web-CAT Guidelines. 1. Logging into Web-CAT

Lab 2 Building on Linux

Parent Instructions for Accessing Elmira Heights Parent Portal (SchoolTool)

WEBD 236 Lab 5. Problem

PHP Online Training. PHP Online TrainingCourse Duration - 45 Days. Call us: HTML

Table of contents. DMXzone Universal Form Validator ASP DMXzone.com

Using Dreamweaver CS6

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

ScholarOne Manuscripts Author Guide

Roxen Content Provider

Part 2 Uploading and Working with WebCT's File Manager and Student Management INDEX

Adding Pages. Adding pages to your website is simple and powerful! In just a few minutes you can create a page that: Highlights a special event

Dreamweaver CS6. Table of Contents. Setting up a site in Dreamweaver! 2. Templates! 3. Using a Template! 3. Save the template! 4. Views!

CMSC 201 Fall 2018 Lab 04 While Loops

Our First Programs. Programs. Hello World 10/7/2013

Using IDLE for

Programming with Python

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

5.5 Complex Fractions

Transcription:

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.

CSC-111: Introduction to Information Technology page 2 1. Using conditional statements and the query string to determine page content (file: 1ifelse.php) $score = $_GET['grade']; if ($score < 60) echo "You fail. Study harder next time!"; else if ($score <= 100) echo "You pass. Good job!"; else echo "invalid input given"; 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.

CSC-111: Introduction to Information Technology page 3 2. User-defined functions (file: 2adder.php) <html> <head></head> <body> function adder($num1, $num2){ return $num1 + $num2; $number1 = $_POST[ number1 ]; $number2 = $_POST[ number2 ]; if($number1 && $number2){ echo "<h3>the sum of ". $number1. " and ". $number2. " is: ". adder($number1, $number2). ".</h3>"; <h2>please input 2 numbers to add together.<br> <form action=2adder.php method=post> <input type=text length=5 name=number1> + <input type=text length=5 name=number2> <input type=submit value="equals"><br> </form> </body> </html> 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.

CSC-111: Introduction to Information Technology page 4 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). session_start(); $_SESSION['count']++; echo "<h4>the value of session variable <i>count</i> is ". $_SESSION['count']. ".</h4>"; <HTML> <HEAD><TITLE>Session Variable Test</TITLE></HEAD> <BODY> Click <b>refresh</b> a few times to see what happens. Then click here to open the file <i><a href="7sessiontest2.php">7sessiontest2.php</a></i>. 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. session_start(); $_SESSION['count']++; echo "<h4>the value of session variable <i>count</i> is ". $_SESSION['count']. " on this page.</h4>"; <HTML> <HEAD><TITLE>Session Variable Test</TITLE></HEAD> <BODY> Note that the value of <i>count</i> has persisted across both pages. </BODY> </HTML> 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.

CSC-111: Introduction to Information Technology page 5 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.