Transcript Lecture 7

IS2803
Developing Multimedia
Applications for Business (Part 2)
Lecture 7: Working with PHP and Access I
Rob Gleasure
[email protected]
robgleasure.com
IS2803

Today's lecture
 Login exercise
Exercise

We begin today from where we left off last week, i.e. save a new
copy of the following saved as lecture7a.php
<!DOCTYPE">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-5">
<title>Lecture 7 main page</title>
</head>
<body>
<h2> Welcome to Lecture 7 </h2>
<?php
session_start();
if(isset($_SESSION['username'])) {
echo "Hello ".$_SESSION['username']."<br>";
/*
use the echo command to add a button with the text 'Logout' after the
line that says Hello to the user */
echo '</form>';
}else{
// add a method of 'post' and an action that points to lecture7b.php to this form
echo '<form>';
echo '
Enter Login name <input type="text" name="login_name">';
echo '
Enter password <input type="password" name="pword">';
echo '
<input type="submit" value ="login" />';
echo '</form> ';
}
?>
</body>
</html>
Exercise

… and the following saved as lecture7b.php
<!DOCTYPE">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-5">
<title>Lecture 7 login handler</title>
</head>
<body>
<?php
//start the session
// create a variable called $username and set it to store the value posted as 'login_name'
$username = $_POST["login_name"];
$pword = $_POST["pword"];
// create an instance of the ADO connection object
// create a connection string in this example the URL for our database is 'Q:/RGleasure/IS4428/lab13.accdb‘
// open the connection to the database
// create a variable to store a SQL query that selects every field where the Username is the same as $username
// create a variable $rS to store the new record returned when the SQL query is executed
if (!$rS->EOF && $rS->Fields(2) == $pword){
// create a session variable called 'username' to store the user's name
}
// redirect back to lecture7a.php
?>
</body>
</html>
Exercise

Lastly, save the following saved as lecture7c.php
<!DOCTYPE>
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-5">
<title>Lecture 7 logout handler</title>
</head>
<body>
<?php
//start the session
//destroy session variables for this user
//redirect back to lecture7a.php
?>
</body>
</html>
Exercise

Create an MS Access database in htdocs called lecture7.accdb

Create a new table in this database called Users and add two text
fields, one with the heading UName and one with the heading
PWord

Add some test users with UNames and PWords
Exercise

In lecture7c.php, add the following
 A line to start the session
 A line to destroy the session
 A line to redirect back to lecture7a.php
Exercise

In lecture7b.php, add the following
 A line to create an instance of the ADO connection object
 A line to create a connection string
 A line to open the connection to the database
 A line to create a String that represents a SQL query that selects
every field where the Username is the same as $username
 Inside the if-loop
 A line to start the session
 A line to create a session variable called 'username' to store
$username
 A line to redirect back to lecture7a.php
Exercise

In lecture7a.php, we want to add a logout button to form that
displays 'Hello' and the user's name

Add a method of 'post' and an action that points to lecture7c.php to
this form

Use the echo command to add a submit button with the text 'Logout'
after the line that says Hello to the user
Exercise

How could you add client-side validation?