Cookies file i o in perl
This presentation is the property of its rightful owner.
Sponsored Links
1 / 31

Cookies & file i/o in perl PowerPoint PPT Presentation


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

Cookies & file i/o in perl. Survey (html form in notes). Submit (Post) and anchor tag in form. Submit calls a perl program to process the data passed in a query string. Perl program opens data file, writes results to it. In slide notes

Download Presentation

Cookies & file i/o in perl

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cookies file i o in perl

Cookies & file i/o in perl


Survey html form in notes

Survey (html form in notes)


Submit post and anchor tag in form

Submit (Post) and anchor tag in form

  • Submit calls a perl program to process the data passed in a query string. Perl program opens data file, writes results to it.

  • In slide notes

  • There is an anchor at the bottom of the form which points to another perl program which displays survey results.


View survey results another perl program

View survey results: another perl program


Perl program displays results

Perl program displays results

  • In slide notes.

  • You’ll need to create a data file, too, and put it in cgi-bin directory.

  • I saved a blank data file to get started. It is named survdat.dat


Write to file

Write to file

  • Need to open file for output

  • Need to lock file

  • Now write to file using

    print fhandle string

  • Unlock file

  • Close file


Write to file1

Write to file

#!c:\perl\bin\perl.exe

print "Content-Type: text/html\n\n";

$LOCK = 2;

$UNLOCK = 8;

open(SURVDAT, ">somedat.dat") or error();

flock(SURVEY, $LOCK);

# Write out the file data, unlock the file, and close it

for ($j = 0; $j<= 20 ; $j++) {

print "writing to file: $line <br/>\n";

print SURVDAT "Bob+$j $j A\n";

}

flock(SURVDAT, $UNLOCK);

close(SURVDAT);


Write to file2

Write to file


Form processing and a file

Form processing and a file


Form processing and a file1

Form processing and a file

  • Get old file contents into an array or some other structure (open file for read, lock, read, unlock, close)

  • Get user data from query string

  • Add it to the data from the file

  • Open file for writing

  • Write the new data


File display in notes

File display (in notes)


File update in notes

File update (in notes)

I did not do it precisely as per earlier notes, but used text page 408 notes to open a file for updating

Open(FILEHANDLE,”+<filething”) or die “cant open”;

#read from file using chomp

chomp(….)

#rewind to start of file

seek(FILEHANDLE,0,0) or die…

#then print to file


Cookies

Cookies

  • http is stateless, so after a browser interacts with a server, no one has any memory of what happened, unless a resord of some sort is made.

  • Data could be written to a file for instance, as in the previous example.

  • Since servers may want remember client profile or shopping cart, cookies provide a mechanism of storing the information on the browser itself.

  • The server asks for and can look at this information if the browser returns.


Cookies1

Cookies

  • A browser makes a request to a server

  • A server makes a response.

  • The response header may include cookies.

  • A cookie has a name and a text value.

  • Every HTTP communication between a browser and a server has a header.

  • A CGI program can create a cookie.

  • The request-response cycle can include cookies being passed back and forth.


Cookies2

Cookies

  • When it is create a cookie is assigned a lifetime.

  • The browser deletes the cookie when its lifetime expires.

  • Only the server that created the cookie can receive the cookie back.

  • You can change your browser settings to reject or delete cookies.


Cookies3

Cookies

  • CGI.pm includes support for cookies through the cookie function.

  • It can create or retrieve cookies.

  • Form to create a cookie:

    cookie(-name->cookiename,-value->cookievalue,-expires->atimevalue)

  • Name can be any string. Value can be any scalar, including references to hashes and arrays. Expires may be expressed in many different time units. +3d means 3 days. Also s for seconds, m for minutes, h for hours, M for months, y for years and now for right now.

  • Cookies can be placed before print content-type in programs that don’t use CGI package, but that is not covered here.


Cookies4

Cookies

  • A cookie must be placed in the header at the time the header is created. It is passes as a parameter to the header with CGI.pm:

  • Header(-cookie=>$my_cookie);

  • I f cookie is called with no parameters it returns a hash of all the cookies in the HTTP header of the current request.

  • To get one specific cookie, the function is called with just that cookie name.

    $age=cookie(‘age’);


Cookies5

Cookies

  • To display all the cookies we could use

    foreach $name (keys cookie()){

    print “$name \t cookie($name) <br/>”;}


A really simple cookie example

A really simple cookie example

  • The next example (2 perl programs) shows how to set and get a cookie with CGI.

  • It is about as simple as it could be.


Perl to set cookie username

Perl to set cookie (‘username’)

#!c:\perl\bin\perl.exe

use CGI ":standard";

$name = "bob";

$to_set = cookie(-name => "username",

-value => $name,

-expires => "+3d");

print header(-cookie => $to_set);

print start_html("Thanks bob!");

print "other content";

print end_html();


Setting bob cookie

Setting bob cookie


Get bob cookie

Get bob cookie


Get a cookie named username

Get a cookie (named “username”)

#!c:\perl\bin\perl.exe

use CGI ":standard";

$name = cookie("username");

print

header(),

start_html("Hello $name"),

h1("Hello " . $name || "Stranger");

if ($name) {

print p("See, I remembered your name!");

} else {

print p("The cookie must have expired.");

}

print end_html();


Getting time information for a cookie that checks when you last visited

Getting time information for a cookie that checks when you last visited

#!C:\perl\bin\perl.exe

print "Content-Type: text/html\n\n";

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday,

$isdst) = localtime;

print "\$sec = $sec\n";

print "\$min = $min\n";

print "\$hour = $hour\n";

print "\$mday = $mday\n";

print "\$mon = $mon\n";

print "\$year = $year\n";

print "\$wday = $wday\n";

print "\$yday = $yday\n";

print "\$isdst = $isdst\n";


Time pl

Time.pl


Cookies code in notes

Cookies: code in notes


Day cookie pl

Day_cookie.pl

#!c:\perl\bin\perl.exe

# day_cookie.pl

# - A CGI-Perl program to use a cookie to remember the

# day of the last login from a user and display it when run

use CGI ":standard";

# Get the existing day cookie, if there was one

@last_day = cookie('last_time');

# Get the current date and make the new cookie

$day_of_week = (qw(Sunday Monday Tuesday Wednesday Thursday

Friday Saturday)) [(localtime)[6]];

$month = (qw(January February March April May June July

August September October November December))

[(localtime)[4]];

$day_of_month = (localtime)[3];

@day_stuff = ($day_of_week, $day_of_month, $month);

$day_cookie = cookie(-name => 'last_time',

-value => \@day_stuff,

-expires => '+5d');


Day cookie pl1

Day_cookie.pl

# Produce the return document

# First, put the cookie in the new header

print header(-cookie => $day_cookie);

print start_html('This is day_cookie.pl');

# If there was no day cookie, this is the first visit

if (scalar(@last_day) == 0) {

print "Welcome to you on your first visit to our site <br />";}

# Otherwise, welcome the user back and give the date of

# the last visit

else {

($day_of_week, $day_of_month, $month) = @last_day;

print "Welcome back! <br /> ",

"Your last visit was on ",

"$day_of_week, $month $day_of_month <br />";}


Keeping track of the shopping cart html in slide notes

Keeping track of the shopping cart (html in slide notes)


Perl displays old shopping cart cookie and adds new shopping cart to it

Perl displays old (shopping cart) cookie and adds new shopping cart to it


Most of the perl program

Most of the perl program

@shopping_cart = cookie('cart');

# Produce the return document

my @cart= (param("cruise"), param("food"),param("music"));

if (scalar(@shopping_cart) != 0)

[email protected]_cart=(@shopping_cart,@cart);}

else

[email protected][email protected];}

$the_cookie = cookie(-name => 'cart',

-value => \@new_cart,

-expires => '+5d');

# First, put the cookie in the new header

print header(-cookie => $the_cookie);

print start_html('This is cookies.pl');

# If there was no cookie, this is the first visit

if (scalar(@shopping_cart) == 0) {

print "Welcome to you on your first visit to our site <br />";}

# Otherwise, welcome the user back and give the date of

# the last visit

else {

foreach $name (@new_cart)

{print "$name <br/>";}

}


  • Login