Note:
Demonstrate the first part before leaving LAB today. The instructor/lab aid will record a score for you. Complete the remainder for Sunday Night Due Date.
Overview:
Create a single php application that will return multiple different pages to the user based upon their input. All your work is to be done in the file index.php. The PHP application will use a query variable called $cmd to determine which page to render. This $cmd variable is created by read the query string url. Recall anything after a ? in the url will be passed as variables to the php program.
You would invoke these pages like:
.index.php?cmd=page1 and index.php?cmd=page2
Here is a code snippet that shows how you will read the variable and make decisions as to what output to render.
<?php if ($cmd==page1){
?>
HTML for page1
<?php
}
else if ($cmd == page2) {
?>
HTML for page2
<?php
} else {
?> default
<?php }?>
Where $cmd is a variable that is sent to the server from the different links.
Steps:
- There is a video showing my working solution that you can use to help understand functionality.
- In your git working copy on ceclnx01, create a directory called php1. The file inside this directory shall be called index.php
- Edit index.php to include Create a bootstrap PHP page with a Header, Left Menu and a Main Content Area.
- In the Left Menu place the following three links:
- <a href=index.php?cmd=page1>Random Numbers</a>
- <a href=index.php?cmd=page2>Images</a>
- <a href=index.php?cmd=page1>Input Form</a>
- <a href=index.php>Home</a>
- Using your browser view this page and get the HTML correct before adding any html.
- Even though you have created a php file, there is no php code yet. At the top of the index.php file put in the following code:
<?php
/*
YOUR COMMENT BLOCK HERE (name, date, assignment..)
*/
session_start(); //create a session
//initialize number of visits session variable
$numVisits = 0; if (isset($_SESSION[num])) { $_SESSION[num]++;
$numVisits = $_SESSION[num];
} else {
$numVisits = 0;
$_SESSION[num] = 0;
}
//see if the cmd get variable is passed into the program.
$cmd = ;
if (isset($_GET[cmd])) {
$cmd =htmlspecialchars($_GET[cmd]);
if ($cmd != page1 && $cmd != page2 && $cmd != page3) {
$cmd = ;
}
}
//YOU WILL PUT YOUR FORM HANDLING CODE HERE
?>
Make sure you understand this code.
5) Now inside the section for your main content area, put in code to display the following: a) Default Page
Display, in a well formatted table, 5 different interesting values from the $_SERVER superglobal.
- page1
for page 1, use PHP to In this area use PHP to generate 100 random numbers with each number having different font colors.
- STOP HERE for the In CLASS LAB AND DEMONSTRATE TO INSTRUCTOR!!!!!!d) page2
Use PHP to randomly select and insert one of three image tags to files stored on your server.
- load 3 images into a subdirectory
- create an array in your code with the images name
- use a random variable to select one of the image names to put in the image tag. e) page3
- Display a simple HTML form that simply asks the user for their name. On submit the form will submit back to index.php. Blank input is legal.
- In the section above labeled YOU WILL PUT YOUR FORM HANDLING CODE
HERE put in code that will look to see if the user submitted a form with their name.
- assuming your input tag is <input type=text name=user> then the value would be $_GET[user];
- Make sure to use htmlspecialchars to protect this variable.
- Assign the submitted value to the session variable called user iii) At the bottom of the menu section display
1) the users name (from the session variable and obviously only if the session value is set) 2) The number of visits.
- Make sure to commit all your code to GIT
- Make sure it is clean, well commented, well indented
- Submit working links via canvas to:
- your index.php file
- ii) your gitlab repository
Reviews
There are no reviews yet.