1 / 31

Cookies & file i/o in perl

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cookies & file i/o in perl

  2. Survey (html form in notes)

  3. 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.

  4. View survey results: another perl program

  5. 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

  6. 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

  7. 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);

  8. Write to file

  9. Form processing and a file

  10. 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

  11. File display (in notes)

  12. 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

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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’);

  18. Cookies • To display all the cookies we could use foreach $name (keys cookie()){ print “$name \t cookie($name) <br/>”;}

  19. 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.

  20. 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();

  21. Setting bob cookie

  22. Get bob cookie

  23. 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();

  24. 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";

  25. Time.pl

  26. Cookies: code in notes

  27. 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');

  28. 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 />";}

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

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

  31. 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) {@new_cart=(@shopping_cart,@cart);} else {@new_cart=@cart;} $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/>";} }

More Related