Utilizing PHP and HTML
For this lab we will save some cookie data to the computer, and then in a new browser session, read the data. Attached is a zip file with a simple form with three fields on it, email address, favorite food and year you were born. Put in the code for the action script and the name attributes on the inputs. In the action script, save these values to cookies that will live for 15 minutes. This will mean setting an expiration time for 15 minutes.
Write a second program that will read these three cookies and display the values.
Your email:
Your favorite food: Pizza
Year you were born: 2000
I should be able to close the browser, then run your second program in a new browser and see the data. But if i wait 15 minutes, it should be gone and I won't see it.
You will have three files:
lastname-lab7.html
lastname-lab7-set.php
lastname-lab7-read.php
This will give you an idea of how easy it to to save your information and use it for tracking based on what you enter into a form.
The code for the sample:
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"/> | |
<style> | |
</style> | |
<title>Lab 7 - Cookies</title> | |
</head> | |
<body> | |
<h2>Your Name - IT 2600 - Lab 7</h2> | |
<form> | |
<table> | |
<tr> | |
<td>Email</td><td> <input type="text"></td> | |
</tr> | |
<tr> | |
<td>Favorite Food </td><td><input type="text"></td> | |
<tr> | |
</tr> | |
<td>What Year Were You Born</td><td> <input type="text"></td> | |
</tr> | |
</tr> | |
<td colspan="2"><center><input type="submit" value="SUBMIT"</center></td> | |
</tr> | |
</table> | |
</form> | |
</body> | |
</html> | |
I have implemented php files which will stores the cookies to the computer and lastname-lab7-read.php file is used to examine whether cookies are expired or not. It will print the cookie values if they are not expired.
lastname-lab7.html :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<style>
</style>
<title>Lab 7 - Cookies</title>
</head>
<body>
<h2>Your Name - IT 2600 - Lab 7</h2>
<form action="lastname-lab7-set.php" method="post">
<table>
<tr>
<td>Email</td>
<td> <input type="text" name="email" required></td>
</tr>
<tr>
<td>Favorite Food </td>
<td><input type="text" name="food" required></td>
</tr>
<tr>
<td>What Year Were You Born</td>
<td> <input type="text" name="year" required></td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" name="submitButton" value="SUBMIT"></center></td>
</tr>
</table>
</form>
</body>
</html>
lastname-lab7-set.php:-
<?php
// if click on submit button
if(isset($_POST["submitButton"])){
// store form values in variables
$email = $_POST["email"];
$food = $_POST["food"];
$year = $_POST["year"];
// set cookie for email ( duration : 15 minutes = 900 seconds)
setcookie("email", $email, time()+900);
// set cookie for food ( duration : 15 minutes = 900 seconds)
setcookie("food", $food, time()+900);
// set cookie for year ( duration : 15 minutes = 900 seconds)
setcookie("year", $year, time()+900);
echo "Cookies are saved to the computer";
}
?>
lastname-lab7-read.php :-
<?php
// check whether cookies are expired or not?
if(!isset($_COOKIE["email"]) && !isset($_COOKIE["food"]) && !isset($_COOKIE["year"])) {
// cookie is expired or not set
echo "Cookie named email is expired!";
echo "<br>Cookie named food is expired!";
echo "<br>Cookie named year is expired!";
} else {
echo "Your email: ".$_COOKIE["email"];
echo "<br>Your favorite food: ".$_COOKIE["food"];
echo "<br>Year you were born: ".$_COOKIE["year"];
}
?>
The cookies names are "email", "food" and "year".
Output:-
1> lastname-lab7.html (HTML form) :-
2> lastname-lab7-set.php (store cookies to the computer) :-
3> lastname-lab7-read.php (print cookie values) :-
4> lastname-lab7-read.php (When cookies are expired) :-
I hope you will understand the above php scripts.
Do you feel needful and useful then please upvote me.
Thank you.
Get Answers For Free
Most questions answered within 1 hours.