Download presentation
Presentation is loading. Please wait.
1
CGI II: Cookies & Stuff Web Programming
2
HTTP Cookie Mechanism to store/retrieve information on the HTTP client
Web server sends a “cookie” in HTTP response header cookie is stored as text file (not executable) on the client’s local drive cookie is included in subsequent HTTP request headers until expiration Syntax HTTP Response Cookie is created & sent from Web Server to Web Client (e.g., Internet Explorer) Set-Cookie: NAME=VALUE; expires=DATE; domain=DOMAIN_NAME; path=PATH e.g. Set-Cookie: user=kiyang; expires=Thursday, 09-Dec-10 11:11:22 GMT; domain= kiyang.kmu.ac.kr; path=/~kiyang/teaching HTTP Request Cookie is returned from Web Client to Web Server Cookie: NAME1=VALUE1; NAME2=VALUE2; .. e.g. Cookie: user=kiyang; Web Programming
3
Cookies with CGI.pm Setting Cookies Getting Cookies
$cookie = cookie ( -name => “WP-cookie”, -value => “$cookieID”, -path=> “/teaching/WP/f10’, -expires=>’+24h’); print header (-cookie=>$cookie); Getting Cookies $cookie_value = cookie (‘cookie-name’); Example: Form Form CGI CGI (sending cookie) CGI (checking cookie) Web Programming
4
Fetching Web Pages Using LWP::Simple #!/usr/bin/perl use LWP::Simple;
$url= $body= get($url); print $body; Example script Web Programming
5
Fetching Web Pages Using WWW-Mechanize
use WWW::Mechanize; $url= " # Create a mechanize object my $mech = WWW::Mechanize->new( autocheck => 1 ); # Fetch a page $mech->get($url); print $mech->content; # Fetch a page into a file $mech->get($url,":content_file"=>"hello.htm"); # Get links @links= $mech->links(); array of link object foreach { my($url,$text,$absurl)= ($link->url(), $link->text, $link->url_abs()); print “$url, $text, $absurl\n”; } # Find links $link= $mech->find_link(url_abs_regex=>qr|^ $page= $mech->follow_link(url_abs_regex=>qr|^ @pages= $mech->follow_all_links(url_abs_regex=>qr|^ Mechanized Spider Example: HTML, CGI S517 Session 13, SLIS-IU
6
CGI Examples Email Webpage Fetch File Upload Simple Search
HTML form CGI Webpage Fetch File Upload Simple Search Advanced Search S517 Session 10, SLIS-IU
7
Reference/Pointer A reference Syntax
a pointer to something (e.g. array, hash) a variable that refer to other variables contain a memory address of data good for working with large data structures creating complex data structures Syntax to create a reference $ptr1= \$scalar; $ptr2= $ptr3= \%hash; $ptr4= [1, 2, 3]; $ptr5= { k1=>v1, k2=>v2} to get the value Variable (i.e. scalar, array, hash) $$ptr1; @$ptr2; %$ptr3; Element of array/hash $$ptr2[0]; $$ptr3{0}; $ptr2->[0]; $ptr3->{0}; S517 Session 12, IU-SLIS
8
Multi-dimensional Arrays
Using anonymous arrays $array = [ [id1,pw1], [id2,pw2], [id3,pw3] ]; for ($i=0; $i<=2; $i++) { for ($j=0; $j<=1; $j++) { print “$array -> [i][j]\n”; } Using array references @a1= (id3,pw3); @array = # array of array pointers for ($i=0; $i<=2; $i++) { $ptr= $array[$i]; print Examples: #1, #2, #3, #4, #5 S517 Session 12, IU-SLIS
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.