Learn about...
Connection details have been emailed to you.
In lab, I will demonstrate how to use WinSCP to connect to our remote server.
Here are the server details:
login.html
in your lab4
folderlogin.html
fileaction
= "login.php" and method
= "post"
to the form
tagform
tag, add a label
tag with the content "Username:"label
tag, add an input
tag
with the attributes type
= "text" and name
= "user"label
tag and inside the form
tag, add another label
tag with the content "Password:"label
tag, add an input
tag
with the attributes type
= "password" and name
= "pswd"label
tag and inside the form
tag, add an
input
with the attributes type
= "submit" and value
= "Login"login.html
is saved to your lab4
folder.login.php
in your lab4
folder.php
file extension.<? $u = $_POST['user']; if ($u == '') { echo '<p>The submitted username was blank</p>'; } ?>
$u
, set the variables $p
to the
password that was sent from the posted form. Hint: The $_POST array index
is the name of the form element you wish to getif
statement, add another if
statement to
echo a paragraph indicating that the password was blank. Use the existing if
statement has a model''
by using the ==
operatorif
statement, add an if else
statement to
do the following:
$p
and $u
are not blank, echo a paragraph
that say "User $u is logged in"login.html
page$u
is logged in".$variable.
'</a>'
lab4
folder to your remote serverusername.siencs.com/lab4
or
username.breimer.net/lab4
to test your login script.
When an HTML form has an action
attribute, clicking the submit
button
triggers an HTTP request and uses the value of the action
as the URL.
Typically, the value will be a relative URL, which implies that the form (HTML file)
and script (PHP file) are in the same folder on the server.
Show your instructor login.html
working on the web server.
The login form (HTML file) and the login script (PHP file) can be combined into on PHP script. This allows error messages to be placed directly in the form and the user does not have to ever click a link to go back to the form.
betterlogin.php
in your lab4
folder.php
file extension.<? function make_login_form($u = '', $u_error = '') { echo ' <form action="betterlogin.php" method="post"> <label> <b>Username: '.$u_error.'</b> <input type="text" name="user" value="'.$u.'"> </label> <input type="submit" value="Login"> </form>'; } ?> <!DOCTYPE html> <html> <meta charset="UTF-8"> <title>Login</title> <style> label { padding: 10px; display: block;} form { padding: 10px; border: 1px solid #888; width: 300px; overflow: auto;} input[type='submit'] { float: right; } </style> <body> <? if ($_POST) { $u = $_POST['user']; if ($u != '') { echo '<p>User '.$u.' is logged in.</p>'; } else { $u_error = ''; if ($u == '') { $u_error = 'Must enter username'; } make_login_form($u, $u_error); } } else { make_login_form(); } ?> </body> </html>
make_login_form
so that it also generates
a label
and input
for the passwordmake_login_form
function. $_POST
variable
is not null, otherwise we print a form with no values or error message.lab4
folder to your remote serverusername.siencs.com/lab4
or
username.breimer.net/lab4
to test your login script.The script that you just created implements a type of recursion. The script generates a form. When the form is submitted, we call the same script again. The form has to handle the base case, which is an initial request for the form with no posted values. Once the form is displayed, clicking the submit button makes a 2nd HTTP request that includes posted values. If the posted values are valid, we will eventually authenticate a user. If the posted values are blank or invalid, we regenerate the form with the previously submitted values and any error messages.
Show your instructor betterlogin.php
working on the web server.
Rather than hardcode radio buttons and drop down menus (option selects), we can write flexible functions that can also display error messages and remember previously submitted selections.
bestlogin.php
in your lab4
folder.php
file extension.bestlogin.php
filelab4
folder to your remote serverusername.siencs.com/lab4
or
username.breimer.net/lab4
to test the bestlogin.php
scriptmake_radio_group
.
This function generates a group of radio buttons from any array of indices/keys and values.$options = ['1'=>'Author', '2'=>'Editor', '3'=>'Admin'];
<div> <label><input type="radio" name="role" value="1" >Author</label> <label><input type="radio" name="role" value="2" > Editor</label> <label><input type="radio" name="role" value="3" > Admin</label> </div>
make_radio_group
as a model, creating a function called make_option_group
that generates an option select menu. Note that it will
take the same three parameters as the make_radio_group
function,
but it will return the proper HTML to generate a drop-down menu instead
of a group of radio buttons.<select name="color"> <option value="" ></option> <option value="#f00">red</option> <option value="#0f0">green</option> <option value="#00f">blue</option> </select>
$colors = [''=>'', '#f00'=>'red', '#0f0'=>'green', '#00f'=>'blue'];
name
attribute is located for an option select menu and
note that you do not need to specify label
tags or use the type
attribute.
make_login_form
near the $option
array, define a new associative array of colors where the key/index
is the 3 digit hex color code and the value is the name of the color (see above).make_login_form
, call your make_option_group
function and pass the color array to generate
a color dropdown to your login form. Instead of passing the name
"role" pass the name "color", otherwise the form will not submit the color value properly.$_POST
variables just like you read the username, password and role. Remember that the option group menu is called "color"make_login_form
functionmake_login_form
function for the color value and new error message.Show your instructor bestlogin.php
working on the web server.
Create a zip file of your lab4
folder called lab4.zip
and submit the file in Blackboard.
If applicable, in the comment area of Blackboard put your partner's name.
The deliverable must be submitted in Blackboard by midnight on the day before your next scheduled lab meeting. If you wish to work alone, you can submit the deliverable yourself. However, you are encouraged to work with your partner and you can submit together. When submitting as a lab pair, be sure to include your partner's name in the comment area when you submit via Blackboard.
Outside of lab, you are only permitted to work with your lab partner. Do not share your work with any other student.