Cookies & file i/o in perl. Survey (html form in notes)

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
CGI Programming.
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
2012 CALENDAR. JANUARY 2012 Sunday 日 Monday 月 Tuesday 火 Wednesday 水 Thursday 木 Friday 金 Saturday 土
Using Perl for CGI Programming
Servlets and a little bit of Web Services Russell Beale.
COS 381 Day 22. Agenda  Assignment #5 Corrected 2 B’s, 1 C, and 3 D’s  Next Capstone progress report due April 21  Capstone projects are DUE May 10.
January 2012 Monday Tuesday Wednesday Thursday Friday Sat/ Sun / /8 14/15 21/22 28/
How the web works: HTTP and CGI explained
COS 381 Day 20. Agenda  Assignment #5 Due  Capstone Progress Reports Overdue Were Due April 7  Capstone projects are DUE May 10 at 1PM  Assignment.
Python and Web Programming
COS 381 Day 23. Agenda  Questions??  Resources Source Code Available for examples in Text Book in Blackboard
COS 381 Day 23. Agenda  Questions??  Resources Source Code Available for examples in Text Book in Blackboard
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Prof. Alfred J Bird, Ph.D., NBCT Office – Wheatly 2nd floor Office Hours – MW 3:00PM to 4:00PM.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
Practical PHP IDIA Spring 2012 Bridget M. Blodgett.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
What day is today? What`s the date?. Sunday Monday Tuesday Wednesday Thursday Friday Saturday What day is today?
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
CP476 Internet Computing CGI1 CGI is a common way to provide for specific computations on server side, interactions with users, or access to databases.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
USING PERL FOR CGI PROGRAMMING
CSU - DCE Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Caching Chapter 12. Caching For high-performance apps Caching: storing frequently-used items in memory –Accessed more quickly Cached Web Form bypasses:
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
Chapter 6 Server-side Programming: Java Servlets
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
Cookies & Session Web Technology
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Chapter 10 © 2003 by Addison Wesley Longman, Inc. 1 Chapter 10 Using Perl for CGI Programming.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
Christopher M. Pascucci Basic Structural Concepts of.NET Managing State & Scope.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
Cookies (continue). Extracting Data From Cookies Data retrieved from a cookie is a simple text string. While there is no specific JavaScript function.
Fall 2000C.Watters1 World Wide Web and E-Commerce Servers & Server Side Processing.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Perl Tutorial - 2 林光龍. Memory Address (1/2) A number that is assigned to each byte in a computer’s memory that the CPU uses to track where data and instructions.
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
8 th Semester, Batch 2008 Department of Computer Science SSUET.
MIT AITI 2004 JSP – Lecture 4 Sessions and Cookies.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Fall 2000C.Watters1 World Wide Web and E-Commerce Servers & Server Side Processing.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
IT441 Network Services Administration
ITM 352 Cookies.
Web Systems Development (CSC-215)
10.1 The Common Gateway Interface
CSc 337 Lecture 27: Cookies.
CSc 337 Lecture 25: Cookies.
Presentation transcript:

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

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 Need to open file for output Need to lock file Now write to file using print fhandle string Unlock file Close file

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 \n"; print SURVDAT "Bob+$j $j A\n"; } flock(SURVDAT, $UNLOCK); close(SURVDAT);

Write to file

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

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.

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.

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.

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

Cookies To display all the cookies we could use foreach $name (keys cookie()){ print “$name \t cookie($name) ”;}

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’) #!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

Get bob cookie

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 #!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

Cookies: code in notes

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 = 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 = = ($day_of_week, $day_of_month, $month); $day_cookie = cookie(-name => 'last_time', -value => -expires => '+5d');

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 == 0) { print "Welcome to you on your first visit to our site ";} # Otherwise, welcome the user back and give the date of # the last visit else { ($day_of_week, $day_of_month, $month) print "Welcome back! ", "Your last visit was on ", "$day_of_week, $month $day_of_month ";}

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

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

Most of the perl = cookie('cart'); # Produce the return document (param("cruise"), param("food"),param("music")); if != 0) else $the_cookie = cookie(-name => 'cart', -value => -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 == 0) { print "Welcome to you on your first visit to our site ";} # Otherwise, welcome the user back and give the date of # the last visit else { foreach $name {print "$name ";} }