Perl/CGI Scripting Exercise 4

Cookies

 

1.  To set a cookie, you must set the cookie before the mime content is printed by your cgi sript.  For example, if I want to set a cookie and print some information to the screen, the following syntax must be followed:

 

Set the cookie

&mime;

Text to the screen.

 

2.  To send a cookie type the following in any of your files.

 

 

·        Note: name is the name of the cookie and value is the value that you want the cookie to have. The value can be a constant or a variable.

 

3.  There are other options to sending a cookie. You can set the cookies expiration date by entering the following.

 

 

·        Note that 00:00:00 is in 24-hour time format. GMT is Greenwich Mean Time.

 

4.  You can set other options such as limiting the cookie to a certain domain or even limiting a cookie to a certain path in your domain. For more information on how to do this, please visit

5.  To send more than one cookie, just repeat the process over as many times as needed.

6.  Now we need to know how to read a cookie that we have previously stored on the users computer. Try the following script:

 

·        You now have a hash called crumbs that contains the cookie names as keys and their values. You can use that information however you please.

 

 

On Your Own Exercise 4

 

Create a HTML input file that gets a name from the user.  The source code is provided below:

 

 

Source code for exercise_4.html

 

<html>

<head>

<title>CGI/Perl Exercise 4</title>

</head>

<body>

<form action="/cgi-bin/exercise_4_1.cgi" method="GET">

First Name:&nbsp;&nbsp;<input type="text" name="name"><br>

<input type="submit" Value="Go">

</form>

</body>

</html>

 

Create a .cgi file called exercise_4_1.cgi.  This file will actually parse the data from the form.  Then, this file will set a cookie called first_name with the value that the user enters on exercise_4.html.  Then, the script will provide a link to exercise_4_2.cgi.

 

 

Create a .cgi file called exercise_4_2.cgi.  This file will read the value of the first_name cookie and print it out to the screen.