Friday, September 10th, 2010
CGI

Determining the User Agent in CGI

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; my $useragent = $ENV{'HTTP_USER_AGENT'}; print header, start_html('User Agent Example'); if ($useragent =~ /Firefox/) { print p("You are visiting with a Firefox browser"); } elsif ($useragent =~ /MSIE/) ...

More CGI

Using carp module to log errors to file

BEGIN { use CGI::Carp qw(carpout); open LOG, '>>', '/var/log/apache/carperror.log' or die "Cannot open file: $!\n"; carpout(LOG); }

Using Carp Module for Debugging CGI

#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; print "Content-type: text/html\n\n"; die "I killed it here";

Viewing Environment Variables in a CGI Script

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; print header, start_html('Environment Variables'); foreach my $variable (keys %ENV) { print p("$variable is $ENV{$variable}"); } print end_html; exit;

Retrieving Multiple Cookies in CGI

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; my $retrievedcookie1 = cookie('testcookie'); my $retrievedcookie2 = cookie('secondcookie'); print header, start_html, p("You sent a couple cookies and their values were $retrievedcookie1 ...

Sending Multiple Cookies Using CGI.pm

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; my $cookie1 = cookie(-name=>'testcookie',value=>'testcookievalue',expires=>'+7d'); my $cookie2 = cookie(-name=>'secondcookie',value=>'secondcookievalue',➥ expires=>'+1d'); print header (-cookie=>[$cookie1,$cookie2]), start_html('CGI Cookie Test'), p("You've received a cookie\n"), end_html; exit;

Setting Cookie Expiration Using the CGI Module

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; my $cookie = cookie(-name=>'testcookie',value=>'testcookievalue',-expires=>'+7d'); print header (-cookie=>$cookie), start_html('CGI Cookie Test'), p("You've received a cookie\n"), end_html; exit;

Retrieving cookies in CGI

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; my $retrievedcookie = cookie('testcookie'); print header, start_html, p("You sent a cookie and its value was $retrievedcookie\n"), end_html; exit;

Simple Cookie Example Using the CGI Module

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; my $cookie = cookie(-name=>'testcookie',-value=>'testvalue'); print header (-cookie=>$cookie); print "You've received a cookie\n"; exit;

Simple cookie example in PERL

#!/usr/bin/perl -T use strict; print "Content-type: text/html\n"; print "Set-Cookie: testcookie=testvalue;"; print "\n\n"; print "You've received a cookie\n"; exit;

Printing the String Input Using the CGI Module

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; print header, start_html('Hello'), start_form, "Enter your name: ",textfield('name'), submit, end_form, hr; if (param()) { print "Hello ", param('name'), p; } print end_html; exit;

Code to Accept Input with the CGI Module

#!/usr/bin/perl -T use strict; use CGI qw/:standard/; print header, start_html('Hello'), start_form, "Enter your name: ",textfield('name'), submit, end_form, hr, end_html; exit;

PERL/CGI Hello World in Object-Oriented Fashion

#!/usr/bin/perl -T use strict; use CGI; my $cgi = new CGI; print $cgi->header; print $cgi->start_html('Hello World'); print $cgi->h1('Hello World'); print $cgi->end_html(); exit;

eXTReMe Tracker