Question

Utilizing PHP and HTML For this lab we will save some cookie data to the computer,...

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>

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I am trying to make a contact form to give input on another browser window. I...
I am trying to make a contact form to give input on another browser window. I am only getting the php code. I am trying to figure out why it won't post. Her is my html and php files: <!DOCTYPE html> <html> <head> <title>Contact</title> <link rel="stylesheet" href="main.css"> </head> <body>    <h1>Contact Form</h1> <nav> <ul> <li><a href="index.html">Home</a></li> <li><a href="page2.html">My PodCast</a></li> <li><a href="page3.html">Contact Me</a></li> <li><a href="page4.html">Resources</a></li> <li><a href="addressbook.html">Address Book</a></li> <li>Contact Form</li> </ul> </nav> <center><h2>Please fill out form</h2> <div id='container'> <div id='header'></div> <center><h3>Contact</h3>...
<!doctype html> <html lang=”en”>   <head>     <title>Principal Calculation Form</title>     <meta charset="utf-8"/>         
<!doctype html> <html lang=”en”>   <head>     <title>Principal Calculation Form</title>     <meta charset="utf-8"/>                   <script src="code.js"></script>   </head>      <body onload="setFocus();">     <div class="container">                                       <div class="main">                                                                                <form action="#" method="post" name="form_name" id="form_id" class="form_class" >                                                                           <h2>--Principal Loan Form--</h2>                                                                                                                                                      <h5>Disclaimer: This software by no mean will precisely predict your mortgage through your lender company.  This Software will only assume the four items below in order to make an educated guess or prediction to your exact mortgage interests amount and the duration of your loan from your lender. <img...
<?php    if(isset($_GET['submit'])){ //sanitize the input        /* Check the error from the input: if...
<?php    if(isset($_GET['submit'])){ //sanitize the input        /* Check the error from the input: if input from user is empty -> get an error string variable if input is not empty -> use preg_match() to match the pattern $pattern = "/^[1-9][0-9]{2}(\.|\-)[0-9]{3}(\.|\-)[0-9]{4}$/"; -> if it's a matched, get a success string variable */           } ?> <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link...
Using Notepad++, copy the code presented in Listing 26.2, "A Form That Displays Data in a...
Using Notepad++, copy the code presented in Listing 26.2, "A Form That Displays Data in a Pop-Up Window," in the subsection "Displaying Data From a Form" in the section "Accessing Form Elements With JavaScript" in Ch. 26, "Working with Web-Based Forms of Sams Teach Yourself HTML, CSS and JavaScript All In One. Save the file using the name Week4ValidationExample.html. Test your code and debug as necessary. Change the name of the display() function to myDisplay() where my is your first...
8- Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following...
8- Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following HTML tags to design your webpage <h1>…</h1> , <h3>… </h3> , <h6>… </h6> , <p>…</p> , <b> …</b> , <i>…</ i>, <a…..> </a>, <img…..> , <table>….</table> , <div> …</div>, <form>…</ form>, <input type=“text”>, and <input type=“submit”> Use an external css to change the default style of your webpage. You must use at least one element selector, one id selector, and one class selector Using...
In the previous assessment, you used a static set of named variables to store the data...
In the previous assessment, you used a static set of named variables to store the data that was entered in a form to be output in a message. For this assessment, you will use the invitation.html file that you modified in the previous assessment to create a more effective process for entering information and creating invitations for volunteers. Rather than having to enter each volunteer and create an invitation one at a time, you will create a script that will...
Section 1.1: Creating the First Table Locate the HTML comment that reads "table 1" just below...
Section 1.1: Creating the First Table Locate the HTML comment that reads "table 1" just below the opening body tag. Add a table tag with the following specifications using the style attribute and CSS: width with a value of 650 pixels a margin set to auto – this will center the table within the web page a border set to none - this removes the table border Add a table row (<tr>) Add a table data container tag (<td></td>) Within...
Java Script I need the code to produce these 3 output. I can only get it...
Java Script I need the code to produce these 3 output. I can only get it to produce input #3 I need it to produce the following: // Input #1: // http://www.example.com // Output // http://www.example.com // Input #2: // http://www.example.com?name=r2d2 // Output // http://www.example.com // name: r2d2 // Input #3: // http://www.example.com?name=r2d2&email=r2d2%40me.com&human=no // Output // http://www.example.com // name: r2d2 // email: [email protected] // human: no THIS IS WHAT I HAVE SO FAR: HTML <!DOCTYPE html> <html lang="en"> <head> <meta...
PART B- Javascript Using a text editor (VS Code or Notepad) and a web browser, you...
PART B- Javascript Using a text editor (VS Code or Notepad) and a web browser, you will demonstrate how to create an HTML file, externally link a JavaScript file, and write some source code in the JavaScript file. a..Create two blank files to be an HTML file and a JavaScript file. The file names should be partA.html and partA.js. b.. Create a basic HTML webpage structure. c..Link the JavaScript file to the HTML file using the <script> tag. d.. Prompt...
For this assignment, create an html page that has a login form. The form should have...
For this assignment, create an html page that has a login form. The form should have 3 input elements -- 1. This should have a type text for the user to enter UserName 2. This should have a type password (like text except cannot see the text that is typed in), for the user to enter password. 3. Submit button (type submit, as we did in class on 2/6). The form method should be set to POST and the action...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT