Скачать презентацию Lecture 15 CGI Sessions Perl CPE 401 Скачать презентацию Lecture 15 CGI Sessions Perl CPE 401

5da1adb0d9b44b20e57a69d5fc140f18.ppt

  • Количество слайдов: 73

Lecture 15 CGI Sessions Perl CPE 401 / 601 Computer Network Systems slides are Lecture 15 CGI Sessions Perl CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Shwen Ho

Sessions q Many web sites allow you to establish a session. v v you Sessions q Many web sites allow you to establish a session. v v you identify yourself to the system. now you can visit lots of pages, add stuff to shopping cart, establish preferences, etc. CGI Sessions 2

State Information q Remember that each HTTP request is unrelated to any other v State Information q Remember that each HTTP request is unrelated to any other v as far as the Web server is concerned q Each new request to a CGI program starts up a brand new copy of the CGI program. q Providing sessions requires keeping state information. CGI Sessions 3

Session Conversation Client Hi! I'm Joe. Hi Joe (it's him again) Welcome Back. . Session Conversation Client Hi! I'm Joe. Hi Joe (it's him again) Welcome Back. . . Server CGI 1 I wanna buy a cookie. CGI 2 OK Joe, it will be there tomorrow. CGI Sessions 4

Hidden Field Usage q One way to propagate state information is to use hidden Hidden Field Usage q One way to propagate state information is to use hidden fields. q User identifies themselves to a CGI program v fills out a form q CGI sends back a form that contains hidden fields that identify the user or session. CGI Sessions 5

Revised Conversation Initial form has field for user name. GET /cgi 1? name=joe HTTP/1. Revised Conversation Initial form has field for user name. GET /cgi 1? name=joe HTTP/1. 0 CGI 1 creates order form with hidden field. GET/cgi 2? name=joe&order=cookie HTTP/1. 0 CGI Sessions 6

Session Keys q Many Web based systems use hidden fields that identify a session. Session Keys q Many Web based systems use hidden fields that identify a session. q When the first request arrives, the system generates a unique session key and stores it in a database. q The session key can be included in all forms/links generated by the system v as a hidden field or embedded in a link CGI Sessions 7

Session Key Properties q Must be unique. q Should expire after a while. q Session Key Properties q Must be unique. q Should expire after a while. q Should be difficult to predict. v typically use a pseudo-random number generator seeded carefully. CGI Sessions 8

Pizza Server Session Keys q We define a server to use session keys: <INPUT Pizza Server Session Keys q We define a server to use session keys: q A request to order a pizza might look like this v all on one line GET /pizza. cgi? sessionkey= Hungry. Student 971890237&pizza=cheese &size=large HTTP/1. 0 CGI Sessions 9

HTTP Cookies q A HTTP Cookies q A "cookie' is a name, value pair that a CGI program can ask the client to remember. q The client sends this name, value pair along with every request to the CGI. q We can also use "cookies" to propagate state information. CGI Sessions 10

Cookies are HTTP q Cookies are HTTP headers. q A server (CGI) can give Cookies are HTTP q Cookies are HTTP headers. q A server (CGI) can give the browser a cookie by sending a Set-Cookie header line with the response. q A client can send back a cookie by sending a Cookie header line with the request. CGI Sessions 11

Set-Cookie Header Options The general form of the Set-Cookie header is: Set-Cookie: name=value; options Set-Cookie Header Options The general form of the Set-Cookie header is: Set-Cookie: name=value; options The options include: expires=. . . domain=. . . path=. . . CGI Sessions 12

Setting a cookie HTTP/1. 0 200 OK Content-Type: text/html Set-Cookie: customerid=0192825 Content-Length: 12345. . Setting a cookie HTTP/1. 0 200 OK Content-Type: text/html Set-Cookie: customerid=0192825 Content-Length: 12345. . . CGI Sessions 13

expires Option q This tells the browser how long to hang on to the expires Option q This tells the browser how long to hang on to the cookie. expires=Friday 29 -Feb-2000 00: 00 GMT q The time/date format is very specific! Weekday, Day-Month-Year Hour: Minute: Second GMT CGI Sessions 14

Default expiration q If there is no expires option on the Set- Cookie header Default expiration q If there is no expires option on the Set- Cookie header line, v the browser does not save the cookie to disk. q In this case, when the browser is closed it will forget about the cookie. CGI Sessions 15

domain Option domain=. unr. edu q The domain option tells the browser the domain(s) domain Option domain=. unr. edu q The domain option tells the browser the domain(s) to which it should send the cookie. q Domains as in DNS. q The domain must start with ". " and contain at least one additional ". " CGI Sessions 16

Domain option rules q The server that sends the Set-Cookie header must be in Domain option rules q The server that sends the Set-Cookie header must be in the domain specified. q If no domain option is in the header, the cookie will only be sent to the same server. lt u Defa avior Beh : CGI Sessions 17

path Option path=/ or path=/~mgunes/cpe 401 q The path option tells the browser what path Option path=/ or path=/~mgunes/cpe 401 q The path option tells the browser what URLs the cookie should be sent to. CGI Sessions 18

path default q If no path is specified in the header, v the cookie path default q If no path is specified in the header, v the cookie is sent to only those URLs that have the same path as the URL that set the cookie. q A path is the leading part of the URL v does not include the filename CGI Sessions 19

Default Path Example If the cookie is sent from: /~mgunes/cpe 401/pizza. cgi it would Default Path Example If the cookie is sent from: /~mgunes/cpe 401/pizza. cgi it would also be sent to /~mgunes/cpe 401/pizza/blah. cgi but not to /~mgunes/cpe 401/soda/pizza. cgi CGI Sessions 20

Set-Cookie Fields q Many options can be specified. q Things are separated by Set-Cookie Fields q Many options can be specified. q Things are separated by "; " Set-Cookie: a=blah; path=/; domain=. cse. unrr. edu; expires=Thursday, 21 -Feb-2002 12: 41: 07 2002 ust on e ne o ! ne li b ll m A CGI Sessions 21

CGI cookie creation q A CGI program can send back any number of HTTP CGI cookie creation q A CGI program can send back any number of HTTP headers. v can set multiple cookies q Content-Type is required! q Blank line ends the headers! CGI Sessions 22

C Example printf( C Example printf("Content-Type: text/htmlrn"); printf("Set-Cookie: prefs=nofrmsrn"); printf("Set-Cookie: Java=yesrn"); printf("rn"); … now sends document content CGI Sessions 23

Getting HTTP Cookies q The browser sends each cookie as a header: Cookie: prefs=nofrms Getting HTTP Cookies q The browser sends each cookie as a header: Cookie: prefs=nofrms Cookie: Java=OK q The Web server gives the cookies to the CGI program via an environment variable. CGI Sessions 24

Multiple Cookies q There can be more than one cookie. q The Web Server Multiple Cookies q There can be more than one cookie. q The Web Server puts them all together like this: prefs=nofrms; Java=OK and puts this string in the environment variable: HTTP_COOKIE CGI Sessions 25

Cookie Limits q Each cookie can be up to 4 k bytes. q One Cookie Limits q Each cookie can be up to 4 k bytes. q One "site" can store up to 20 cookies on a user's machine. CGI Sessions 26

Cookie Usage q Create a session. q Track user browsing behavior. q Keep track Cookie Usage q Create a session. q Track user browsing behavior. q Keep track of user preferences. q Avoid logins. CGI Sessions 27

Cookies and Privacy q Cookies can't be used to: v send personal information to Cookies and Privacy q Cookies can't be used to: v send personal information to a web server without the user knowing about it. v be used to send viruses to a browser. v find out what other web sites a user has visited. * v access a user's hard disk * although they can come pretty close to this one! CGI Sessions 28

Some Issues q Persistent cookies take up space on user's hard disk. q Can Some Issues q Persistent cookies take up space on user's hard disk. q Can be used to track your behavior within a web site. v This information can be sold or shared. q Cookies can be shared by cooperating sites v advertising agencies do this. CGI Sessions 29

Perl q Practical Extration and Reporting Language v a high-level programming language • whose Perl q Practical Extration and Reporting Language v a high-level programming language • whose semantics are largely based on C q Designed for text manipulation v Very fast to implement v particularly strong at process, file and text manipulation q Runs on many different platform v Windows, Mac, Unix, Linux, Dos, etc Perl 31

Running Perl q Perl scripts do not need to be compiled v interpreted at Running Perl q Perl scripts do not need to be compiled v interpreted at the point of execution v do not necessarily have a particular file extension • “. pl” is used commonly q Executing it via the command line> perl script. pl arg 1 arg 2. . . q Or add the line "#!/usr/bin/perl" to the start of the script if you are using unix/linux. /perlscript. pl • Remember to set the correct file execution permissions before running it Perl 32

Beginning Perl q Every statement end with a semi colon Beginning Perl q Every statement end with a semi colon "; " q Comments are prefixed at the start of the line with a hash "#" q Variables are assigned a value using the "=" q Variables are not statically typed, v No need to declare what kind of data you want to hold in them. q Variables are declared the first time you initialize them and they can be anywhere in the program. Perl 33

Scalar Variables q Contains single piece of data q '$' character shows that a Scalar Variables q Contains single piece of data q '$' character shows that a variable is scalar q Scalar variables can store v number v string • a chunk of text surrounded by quotes $name = "paul"; $year = 1980; print "$name is born in $year"; output: paul is born in 1980 Perl 34

Arrays Variables (List) q Ordered list of data, separated by commas q '@' character Arrays Variables (List) q Ordered list of data, separated by commas q '@' character shows that a variable is an array Array of numbers @year_of_birth = (1980, 1975, 1999); Array of string @name = ("Paul", "Jake", "Tom"); Array of both string and numbers @paul_address = (14, "Cleveland St", "NSW", 2030); Perl 35

Retrieving data from Arrays q Printing Arrays @name = ( Retrieving data from Arrays q Printing Arrays @name = ("Paul", "Jake", "Tom"); print "@name"; q Accessing individual elements in an array @name = ("Paul", "Jake", "Tom"); print "$name[1]"; q What has changed? v @name to $name To access individual elements use the syntax $array[index] q Why did $name[1] print the second element? v index 0 represents the first element. Perl 36

Arrays … @name = ( Arrays … @name = ("Paul", "Jake", "Tom"); print "@name"; Paul Jake Tom print @name; Paul. Jake. Tom $count=@name; $count = 3 @name. R=reverse(@name); @name. R=("Tom", "Jake", "Paul") @name. S=sort(@name); @name. S=("Jake", "Paul", "Tom") Perl 37

Basic Arithmetic Operators + * / ++ -$a += 2 $b *= 3 Addition Basic Arithmetic Operators + * / ++ -$a += 2 $b *= 3 Addition Subtraction multiplication division adding one to the variable subtracting one from the variable incrementing variable by 2 tripling the value of the variable Perl 38

Relational Operators Comparison Equals Not equal Less than Greater than Less than or equal Relational Operators Comparison Equals Not equal Less than Greater than Less than or equal Greater than or equal Comparison Numeric String == != < > <= >= <=> eq ne lt gt le gt cmp Perl 39

Control Operators - If if ( expression 1) {. . . } elsif (expression Control Operators - If if ( expression 1) {. . . } elsif (expression 2) {. . . } else {. . . } Perl 40

Iteration Structures q while (CONDITION) { BLOCK } q until (CONDITION) {BLOCK} q do Iteration Structures q while (CONDITION) { BLOCK } q until (CONDITION) {BLOCK} q do {BLOCK} while (CONDITION) q for (INITIALIZATION ; CONDITION ; Re-INITIALIZATION) {BLOCK} q foreach VAR (LIST) {BLOCK} v for VAR (LIST) {BLOCK} Perl 41

Iteration Structures $i = 1; while($i <= 5){ print Iteration Structures $i = 1; while($i <= 5){ print "$in"; $i++; } for($x=1; $x <=5; $x++) { print "$xn"; } @array = [1, 2, 3, 4, 5]; foreach $number (@array){ print "$numbern"; } Perl 42

String Operations q Strings can be concatenated with the dot operator $lastname = String Operations q Strings can be concatenated with the dot operator $lastname = "Harrison"; $firstname = "Paul"; $name = $firstname. $lastname; $name = "$firstname$lastname"; q Comparison can be done with the relational operator $string 1 = "hello"; $string 2 = "hello"; if ($string 1 eq $string 2) { print "they are equal"; } else { print "they are different"; } Perl 43

String comparison using patterns q The ‘=~ ’ operator return true if the pattern String comparison using patterns q The ‘=~ ’ operator return true if the pattern within the ‘/’ quotes are found. $string 1 = "HELLO"; $string 2 = "Hi there"; # test if the string contains the pattern EL if ($string 1 =~ /EL/) { print "This string contains the pattern"; } else { print "No pattern found"; } Perl 44

Functions in Perl q No strict variable type restriction during function call q Perl Functions in Perl q No strict variable type restriction during function call q Perl has provided lots of useful functions chop - remove the first character of a string v chomp - remove the carriage return character from the end of a string v push - append one or more element into an array v pop - remove the last element of an array and return it v shift - remove the first element of an array and return it vs - replace a pattern with a string v Perl 45

Functions in Perl q The Functions in Perl q The "split" function breaks a given string into individual segments given a delimiter q split( /pattern/, string) returns a list @output = split (/s/, $string); # breaks the sentence into words @output = split (//, $string); # breaks the sentence into single characters @output = split (/, /, $string); # breaks the sentence into chunks separated by a comma. § join ( /delimiter/, array) returns a string Perl 46

Functions in Perl A simple perl function sub say. Hello { print Functions in Perl A simple perl function sub say. Hello { print "Hello!!n"; } say. Hello(); Perl 47

Executing functions in Perl q Function arguments are stored automatically in a temporary array Executing functions in Perl q Function arguments are stored automatically in a temporary array called @_ sub say. Helloto { @name = @_; $count = @_; foreach $person (@name){ print "Hello $personn"; } return $count; } @array = ("Paul", "Jake", "Tom"); say. Helloto(@array); say. Helloto("Mary", "Jane", "Tylor", 1, 2, 3); Perl 48

Input / Output q Perl allows you to read in any input that is Input / Output q Perl allows you to read in any input that is automatically sent to your program via standard input by using the handle . q Other I/O topics include reading and writing to files, Standard Error (STDERR) and Standard Output (STDOUT). q One way of handling inputs via is to use a loop to process every line of input Perl 49

Input / Output q Count the number of lines from standard input and q Input / Output q Count the number of lines from standard input and q print the line number together with the 1 st word of each line. $count = 1; foreach $line (){ @array = split(/s/, $line); print "$count $array[0]n"; $count++; } Perl 50

Regular Expression q Regular expression is a set of characters that specify a pattern. Regular Expression q Regular expression is a set of characters that specify a pattern. q Used for locating piece of text in a file. q Regular expression syntax allows the user to do a "wildcard" type search without necessarily specifying the character literally q Available across OS platform and programming language. Perl 51

Simple Regular Expression q A simple regular expression contains the exact string to match Simple Regular Expression q A simple regular expression contains the exact string to match $string = "aaaabbbbccc"; if($string =~ /bc/){ print "found patternn"; } output: found pattern Perl 52

Simple Regular Expression q The variable ‘$& ’ is automatically set to the matched Simple Regular Expression q The variable ‘$& ’ is automatically set to the matched pattern $string = "aaaabbbbccc"; if($string =~ /bc/){ print "found pattern : $&n"; } output: found pattern bc Perl 53

Simple Regular Expression q What happen when you want to match a generalised pattern Simple Regular Expression q What happen when you want to match a generalised pattern v like an "a" followed by some "b"s and a single "c" $string = "aaaabbbbccc"; if($string =~ /abbc/){ print "found pattern : $&n"; } else {print "nothing foundn"; } output: nothing found Perl 54

Regular Expression - Quantifiers q We can specify the number of times we want Regular Expression - Quantifiers q We can specify the number of times we want to see a specific character in a regular expression by adding operators behind the character. q ‘ * ’ (asterisk) v matches zero or more copies of a specific character q ‘ + ’ (plus) v matches one or more copies of a specific character Perl 55

Regular Expression - Quantifiers @array = [ Regular Expression - Quantifiers @array = ["ac", "abbc", "abb", "bbc", "bcf", "abbb", "c"]; foreach $string (@array){ if($string =~ /ab*c/){ print "$string "; } } output: ac abbc abbbc Perl 56

Regular Expression - Quantifiers @array = [ Regular Expression - Quantifiers @array = ["ac", "abbc", "abb", "bbc", "bcf", "abbb", "c"]; Regular Exp Matched pattern abc ab*c ac abbc abbbc ab+c abbc abbbc Perl 57

Regular Expression - Anchors q Anchor restrictions preceding and behind the pattern specify where Regular Expression - Anchors q Anchor restrictions preceding and behind the pattern specify where along the string to match to. q ‘^’ indicates a beginning of a line restriction q ‘$’ indicates an end of line restriction Perl 58

Regular Expression - Anchors @array = [ Regular Expression - Anchors @array = ["ac", "abbc", "abb", "bbc", "bcf", "abbb", "c"]; Regular Exp Matched pattern ^bc bc ^b*c bbc bcf c ^b*c$ bbc c b*c$ ac abbc abbbc c Perl 59

Regular Expression - Range q […] is used to identify the exact characters you Regular Expression - Range q […] is used to identify the exact characters you are searching for q [0123456789] will match a single numeric character q [0 -9] will also match a single numeric character q [A-Za-z] will match a single alphabet of any case Perl 60

Regular Expression - Range q Search for a word that v starts with the Regular Expression - Range q Search for a word that v starts with the uppercase T v second letter is a lowercase alphabet v third letter is a lower case vowel v is 3 letters long followed by a space q Regular expression : "^T[a-z][aeiou] " q Note : [z-a] is backwards and does not work q Note : [A-z] does match upper and lowercase but also 6 additional characters between the upper and lower case letters in the ASCII chart: [ ] ^ _ ` Perl 61

Regular Expression - Others q Match a single character (non specific) with Regular Expression - Others q Match a single character (non specific) with ". " (dot) a. c matches any string with "a" follow by one character and followed by "c" q Specifying number of repetition sets with "{" and "}“ [a-z]{4, 6} match four, five or six lower case alphabet q Remembering Patterns with "(, )" and "1" v Regular Exp allows you to remember and recall patterns Perl 62

Reg. Exp problem and strategies q You tend to match more lines than desired. Reg. Exp problem and strategies q You tend to match more lines than desired. A. *B matches AAB as well as AAAAAAACCCAABBBBAABBB q Knowing what you want to match q Knowing what you don’t want to match q Writing a pattern out to describe that you want to match q Testing the pattern Perl 63

Web Servers & CGI q Most web server are capable of running CGI programs. Web Servers & CGI q Most web server are capable of running CGI programs. q The server must be able to determine whether a URI refers to: v Document • just send it back v CGI program • run it and send back the result. CGI … 65

CGI recognition q Some servers insist that CGI programs be in a special place CGI recognition q Some servers insist that CGI programs be in a special place v typically the URL path is one of: /CGI-BIN /cgi-bin /CGI /cgibin q Some servers look at the filename: v filename ends with. cgi q Some servers are given a list of URLS that are CGIs CGI … 66

User files and Web Servers On Unix based web servers, the URL /~username is User files and Web Servers On Unix based web servers, the URL /~username is typically mapped to the directory ~username/public. html -or~username/public_html CGI … 67

www. cse. unr. edu q On the CSE web server you should put your www. cse. unr. edu q On the CSE web server you should put your files in ~/public. html The URI http: //www. cse. unr. edu/~you is your home page where you is your CSE username. CGI … 68

Directories q Most web servers do the following when a URL maps to a Directories q Most web servers do the following when a URL maps to a directory: v if there is a file named index. html in the directory • it is sent back. v if there is no index. html, • an HTML formatted directory listing is sent back. CGI … 69

Debugging q It's hard to debug a CGI program! q Debugging print statements should Debugging q It's hard to debug a CGI program! q Debugging print statements should generate HTML. q You can run the program from the Unix command line v you just need to set the environment variables right (use GET for this). CGI … 70

CGI script example Perl 71 CGI script example Perl 71

HTML for Forms <HTML> <HEAD> <TITLE>cgi-test</TITLE> </HEAD> <BODY> <p> This is a sample page HTML for Forms cgi-test

This is a sample page to read two data items from the web page:

First name= Last name=

Parameters passed as arguments xfirst and xlast Perl 72

Perl - CGI script #!/usr/bin/perl print “Content-Type: text/htmlnn”; print “<html><head>n”; print “<title>Sample PERL script</title>n”; Perl - CGI script #!/usr/bin/perl print “Content-Type: text/htmlnn”; print “n”; print “Sample PERL scriptn”; print “n”; print “

Query_string is $ENV{'QUERY_STRING'}n”; foreach ( split( /&/, $ENV{'QUERY_STRING'}) ) { ( $key, $val ) = split( /=/, $_, 2 ); $tmp{$key} = $val; } print “

First name is $tmp{'xfirst'}n”; print “

Last name is $tmp{'xlast'}n”; print “n” • Perl program first reads parameters as xfirst&zlast from $ENV (environment) into QUERY_STRING • Output of Perl is the syntax of an HTML page that is displayed Perl 73