Onto yesterdayโs Perl CGI Spreadsheet Module Sorting Tutorial, today, we introduce a new Perl module โฆ
โฆ which can help us reach the content of a user nominated URL.
From LWP::UserAgent doing itโs stuff, the rest of the changed Perl code below โฆ
- turns relative references into absolute ones โฆ within an โฆ
- iframe โฆ with โฆ
- reworked content placed into the srcdoc attribute โฆ nested in โฆ
- details/summary (with an โaโ link) โrevealโ scenario
โฆ in โฆ
#!/usr/bin/perl
use DateTime;
use DateTime::TimeZone;
use DBI;
use DBD::mysql;
use Spreadsheet::WriteExcel;
use Data::Dumper qw(Dumper);
use LWP::UserAgent ();
#use Unicode::Escape;
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
my $rest = "";
$first_name = $FORM{first_name};
$last_name = $FORM{last_name};
$inurl = $FORM{inurl};
my $before_page = "";
my $allbutlastbit = "";
my $lastbit = (split '/', $ENV{REQUEST_URI})[-1];
my $page_url = 'http';
$page_url.='s' if $ENV{HTTPS};
$page_url.='://';
if ($ENV{SERVER_PORT} != 80) {
$before_page="$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/";
$page_url.="$ENV{SERVER_NAME}:$ENV{SERVER_PORT}$ENV{REQUEST_URI}";
} else {
$before_page="$ENV{SERVER_NAME}/";
$page_url.="$ENV{SERVER_NAME}.$ENV{REQUEST_URI}";
}
if (length($lastbit) > 0) {
$allbutlastbit = substr($page_url, 0, (length($page_url) - length($lastbit)));
} else {
$allbutlastbit = $page_url;
}
if (length($inurl) == 0) {
$inurl = $inurl;
} else {
$inurl =~ s/%3A/:/ig;
$inurl =~ s/%2F/\//ig;
$inurl =~ s/%3F/?/ig;
$inurl =~ s/%26/&/ig;
my $ipage_url = $inurl;
my $ibefore_page = "";
my $iallbutlastbit = "";
my $ilastbit = (split '/', $inurl)[-1];
my $lenseven = (7 + length($ilastbit));
my $leneight = (8 + length($ilastbit));
my $leni = length($inurl);
if (length($ilastbit) > 0) {
if ($lenseven == $leni) {
$iallbutlastbit = "${inurl}/";
} elsif ($leneight == $leni) {
$iallbutlastbit = "${inurl}/";
} else {
$iallbutlastbit = substr($ipage_url, 0, (length($ipage_url) - length($ilastbit)));
}
} else {
$iallbutlastbit = $ipage_url;
}
my $ua = LWP::UserAgent->new(timeout => 10);
$ua->env_proxy;
my $response = $ua->get($inurl);
if ($response->is_success) {
my $dcont = $response->decoded_content;
#$dcont =~ s/html\>/div\>/ig;
#$dcont =~ s/body\>/div\>/ig;
$dcont =~ s/"\/\//"\/\//ig;
$dcont =~ s/"http/"http/ig;
$dcont =~ s/"file/"file/ig;
$dotdot = "..\/";
if (index($dcont, "rjmprogramming.com.au") >= 0) {
$allbutlastbit =~ s/:443//ig;
$allbutlastbit =~ s/\/cgi-bin\//\//ig;
if (substr($allbutlastbit, -1, 1) == "/") {
$allbutlastbit = substr($allbutlastbit, 0, (-1 + length($allbutlastbit)));
}
$dcont =~ s/"\//"${allbutlastbit}\//ig;
#$dcont =~ s/ src="/ src="..\//ig;
#$dcont =~ s/ href="/ href="..\//ig;
$dcont =~ s/ src="/ src="${dotdot}/ig;
$dcont =~ s/ action="/ action="${dotdot}/ig;
$dcont =~ s/ srcset="/ srcset="${dotdot}/ig;
$dcont =~ s/ href="/ href="${dotdot}/ig;
} else {
if (index($dcont, "wikipedia.org") >= 0) {
$dotdot =~ s/\/wiki\//\//ig;
${iallbutlastbit} =~ s/\/wiki\//\//ig;
} else {
$dotdot = $iallbutlastbit;
}
if (substr($iallbutlastbit, -1, 1) == "/") {
$iallbutlastbit = substr($iallbutlastbit, 0, (-1 + length($iallbutlastbit)));
}
$dcont =~ s/"\//"${iallbutlastbit}\//ig;
$dcont =~ s/ src="/ src="${dotdot}/ig;
$dcont =~ s/ srcset="/ srcset="${dotdot}/ig;
$dcont =~ s/ action="/ action="${dotdot}/ig;
$dcont =~ s/ href="/ href="${dotdot}/ig;
$dcont =~ s/url\("/url\("${dotdot}/ig;
$dcont =~ s/url\(portal\//url\(${dotdot}portal\//ig;
$dcont =~ s/url\(static\//url\(${dotdot}static\//ig;
$dcont =~ s/url\(wiki\//url\(${dotdot}wiki\//ig;
$dcont =~ s/url\(er\//url\(${dotdot}er\//ig;
}
$dcont =~ s/"/"/ig;
$dcont =~ s/\r\n//ig;
$dcont =~ s/\n//ig;
$dcont =~ s/\r//ig;
# print "Content-type: text/html\n\n";
$rest .= "<br><details id=drest><summary><h3><a target=_blank href=$inurl>$inurl</a></h3></summary><br><iframe style=width:100%;height:900px; srcdoc=\"" . $dcont . "\"></iframe></details><br>";
}
#else {
# die $response->status_line;
#}
}
$blogp = $FORM{blogp};
$blogp =~ s/%2C/,/ig;
$blogi = $blogp;
my $blogt = $FORM{blogt};
$blogt =~ s/%2C/,/ig;
$oneletter = substr( $first_name, 0, 1 );
#my $uemoji = Unicode::Escape->new($FORM{emoji});
my $emoji = chr(0x0001F517); # "\xf0\x9f\x98\x80"; #$uemoji->unescape;
#my $ucutei = Unicode::Escape->new($FORM{cutei});
my $cutei = chr(0x0001F3C3); # "\xf0\x9f\x98\x80"; #$ucutei->unescape;
#my $uimgei = Unicode::Escape->new($FORM{imgei});
my $imgei = chr(0x0001F5BC); # "\xf0\x9f\x98\x80"; #$uimgei->unescape;
$dt = DateTime->now; # same as ( epoch => time )
$dttz = DateTime::TimeZone->new( name => 'local' )->name();
$dt->set_time_zone( $dttz );
$year = $dt->year;
$month = $dt->month; # 1-12
$day = $dt->day; # 1-31
$dow = $dt->day_of_week; # 1-7 (Monday is 1)
$hour = $dt->hour; # 0-23
$minute = $dt->minute; # 0-59
$second = $dt->second; # 0-61 (leap seconds!)
$doy = $dt->day_of_year; # 1-366 (leap years)
$doq = $dt->day_of_quarter; # 1..
$qtr = $dt->quarter; # 1-4
$dmy = $dt->dmy('/'); # 06/12/2002
$hms = $dt->hms; # 14:02:29
$tt = "";
$cont = "";
$oneletter = " ";
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "<script type=text/javascript>";
print " var ls='', ns='', qwr=''; ";
print " function zeroize(iinsf) { var insf=iinsf; var sep='/', i=0; while (insf.substring(i).substring(0,1) >= '0' && insf.substring(i).substring(0,1) <= '9') { i++; } sep=insf.substring(i).substring(0,1); ";
print " var arr=insf.split(sep); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(sep + arr[1] + sep, sep + '0' + arr[1] + sep); } ";
print " if (eval('' + arr[0].length) == 1) { insf='0' + insf; } ";
print " arr=insf.split(' ')[1].split(':'); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (insf.indexOf(' pm') != -1) { insf=insf.replace(' ' + arr[0].trim() + ':', ' ' + eval(12 + eval('' + arr[0].trim())) + ':').replace(' pm', ' '); arr=insf.split(' ')[1].split(':'); } ";
print " if (eval(-1 + eval('' + arr[1])) == eval('' + document.getElementById('mystime').innerHTML.split(':')[1])) { insf=insf.replace(':' + arr[1] + ':', ':' + document.getElementById('mystime').innerHTML.split(':')[1] + ':'); arr[1]='zz'; } ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(':' + arr[1] + ':', ':0' + arr[1] + ':'); } ";
print " if (eval('' + arr[0].trim().length) == 1) { insf=insf.replace(arr[0].trim() + ':', '0' + arr[0].trim() + ':'); } ";
print " } ";
print " } ";
print " return insf; ";
print " } ";
print " function loctime() { if (ls == '') { ";
print " qwr='' + Intl.DateTimeFormat().resolvedOptions().timeZone; ";
print " var d = new Date(); ";
print " d.toLocaleString('en-US', { timeZone: qwr }); ";
print " ns=document.getElementById('mystime').innerHTML.split(':')[2].split(' ')[0]; ";
print " ls=zeroize(('' + d.toLocaleString()).replace(',','')) + ' <a onclick=alert(qwr); style=cursor:pointer;text-decoration:underline;>GMT' + d.toString().split(' GMT')[1] + '</a>'; } if (!document.getElementById('mytime')) { setTimeout(loctime,1000); } else { ";
print " document.getElementById('mytime').innerHTML=ls.replace(':' + ls.split(':')[2].split(' ')[0] + ' ', ':' + ns + ' '); } ";
print " } ";
print "</script>";
print "</head>";
print "<body onload=setTimeout(loctime,1000);>";
print "<h2 style='font-family:Courier New;'>Hello $first_name $last_name <br> Local: <span id=mytime></span><br>Server: <span id=mystime>$dmy $hms</span> $dttz </h2><h4>";
if ($blogp == "") {
$blogp = $first_name;
$oneletter = substr( $blogp, 0, 1 );
}
if ($blogp != "") {
$oneletter = substr( $blogp, 0, 1 );
if ((ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")) || ord($oneletter) == 40 ){
my ($oneurl) = split /,/, $blogp;
if ($oneurl == $blogp) {
$oneletter = substr( $blogp, 0, 1 );
} else {
if (ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")) {
$oneletter = "1";
$blogp = "( $blogp )";
} else {
$oneletter = "2";
}
}
}
}
if (ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")){
# /*** mysql hostname ***/
$hostname = 'dbhost';
# /*** mysql username ***/
$username = 'username';
# /*** mysql ***/
$password = 'password';
$dbname = 'dbname';
$tname = 'table_name';
$hostname = "DBI:mysql:$dbname:$hostname";
$dbh = DBI->connect($hostname, $username, $password);
my ($oneaurl) = split /,/, $blogp;
if (index($blogp, ',') <= 0) {
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID=?");
} else {
if (ord($oneletter) != 40) {
$blogp = "($blogp)";
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID in $blogp");
$blogp = "";
} else {
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID in $blogp");
$blogp = "";
}
}
if ($blogp == "") {
$sth->execute();
} else {
$sth->execute( $blogp );
}
$one = 0;
$two = 10;
$exc = 0;
$coln = "A";
$dcol = 1;
$rown = 6;
$trown = 6;
$alinkis = "";
my $workbook = 0; # Spreadsheet::WriteExcel->new('../perl.xls');
my $date_format = 0;
my $date = 0;
# Set the default format for dates.
while (($tt, $cont, $iid) = $sth->fetchrow_array()) {
my ($idis) = split /,/, $blogi, 1;
my ($yyyymmdd) = substr( $blogt, $one, $two ); # split /,/, $blogt, 1;
my ($junk, $imageu) = split / src="/, $cont;
my ($imageurl) = split /"/, $imageu;
my ($junk, $cutto) = split / href="/, $cont;
my ($cuttothechase) = split /"/, $cutto;
$blogt = substr( $blogt, 11 );
if ($exc == 0) {
$exc = 1;
# Create a new Excel workbook
$workbook = Spreadsheet::WriteExcel->new('../perl.xlsm');
# Add a worksheet
$worksheet = $workbook->add_worksheet();
$worksheet->set_column(0, 0, 90);
$worksheet->set_column(1, 0, 30);
# Add and define a format
$format = $workbook->add_format(); # Add a format
$format->set_bold();
$format->set_color('red');
$format->set_align('center');
# my $date_format = $workbook->add_format({num_format => 'yyyy-mm-ddThh:mm:ss.sss'});
$date_format = $workbook->add_format(
bold => 1,
align => 'center',
num_format => 'yyyy-mm-dd hh:mm'
);
# Write a formatted and unformatted string, row and column notation.
$col = $row = 0;
$worksheet->write($row, $col, "$first_name $last_name", $format);
$worksheet->write(1, $col, 'My Study Guide');
# Write a number and a formula using A1 notation
$worksheet->write("${coln}3", "$tt");
$date = sprintf "%sT03:00:00.000Z", $yyyymmdd;
$worksheet->write_date_time(2, $dcol, $date, $date_format);
$worksheet->write("${coln}4", "https://www.rjmprogramming.com.au/ITblog/?p=$iid" . "#$emoji");
$date = sprintf "%sT03:01:00.000Z", $yyyymmdd;
$worksheet->write_date_time(3, $dcol, $date, $date_format);
$worksheet->write("${coln}5", "$cuttothechase" . "#$cutei");
$date = sprintf "%sT03:02:00.000Z", $yyyymmdd;
$worksheet->write_date_time(4, $dcol, $date, $date_format);
$worksheet->write("${coln}6", "$imageurl" . "#$imgei");
$date = sprintf "%sT03:03:00.000Z", $yyyymmdd;
$worksheet->write_date_time(5, $dcol, $date, $date_format);
$alinkis = "<a download=perl.xlsm href='//www.rjmprogramming.com.au/perl.xlsm' title='Download Excel spreadsheet'>📄</a> <a target=_blank href=$cuttothechase title='Cut to the chase'>🏃</a> <a target=_blank href=$imageurl title='Image URL'>🖼</a>";
print "<details id=myd><summary style=background-color:#f0f0f0; id=mys><h1><a target=_blank href='//www.rjmprogramming.com.au/ITblog/?p=$iid'>$tt</a> $alinkis</h1></summary>$cont</details>\n";
} else {
$trown += 4;
$date = sprintf "%sT03:00:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "$tt");
$date = sprintf "%sT03:01:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "https://www.rjmprogramming.com.au/ITblog/?p=$iid" . "#$emoji");
$date = sprintf "%sT03:02:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "$cuttothechase" . "#$cutei");
$date = sprintf "%sT03:03:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "$imageurl" . "#$imgei");
$alinkis = "<a download=perl.xlsm href='//www.rjmprogramming.com.au/perl.xlsm' title='Download Excel spreadsheet'>📄</a> <a target=_blank href=$cuttothechase title='Cut to the chase'>🏃</a> <a target=_blank href=$imageurl title='Image URL'>🖼</a>";
print "<details class=myd><summary style=background-color:#f0f0f0; class=mys><h1><a target=_blank href='//www.rjmprogramming.com.au/ITblog/?p=$iid'>$tt</a> $alinkis</h1></summary>$cont</details>\n";
}
# print "@row\n";
}
# }
}
print "</h4>" . $rest . "<br><br><iframe id=myif src='/hello_get.html' style='width:100%; height:1500px;'></iframe></body>";
print "</html>";
1;
โฆ in thechanged Perl hello_getcgi code supervised by (and working with) thechanged HTML hello_get
html web
application we hope you (re-)try for yourself.
Previous relevant Perl CGI Spreadsheet Module Sorting Tutorial is shown below.
Spreadsheets can be a great aid where online activities might coincide, via downloading, to desktop activities. So, we want to delve further into the spreadsheet functionality we started with yesterdayโs Perl CGI Spreadsheet Module Tutorial.
We decided โฆ
- to add a second column with timestamps โฆ to facilitate โฆ
- spreadsheet sorting possibilities via Excel (or LibreOffice) โฆ
Data -> Sort
โฆ as well as โฆ - column width setting โฆ via โฆ
$worksheet->set_column(0, 0, 90);
$worksheet->set_column(1, 0, 30);
โฆ and โฆ - add an emoji hashtag component to the three links associated with each blog posting reference โฆ so that โฆ
- the link cells become more self explanatory without the need for any new columns to explain โฆ
- we add graphical interest to the spreadsheet data
โฆ in the changed โฆ
#!/usr/bin/perl
use DateTime;
use DateTime::TimeZone;
use DBI;
use DBD::mysql;
use Spreadsheet::WriteExcel;
use Data::Dumper qw(Dumper);
#use Unicode::Escape;
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$first_name = $FORM{first_name};
$last_name = $FORM{last_name};
$blogp = $FORM{blogp};
$blogp =~ s/%2C/,/ig;
$blogi = $blogp;
my $blogt = $FORM{blogt};
$blogt =~ s/%2C/,/ig;
$oneletter = substr( $first_name, 0, 1 );
#my $uemoji = Unicode::Escape->new($FORM{emoji});
my $emoji = chr(0x0001F517); # "\xf0\x9f\x98\x80"; #$uemoji->unescape;
#my $ucutei = Unicode::Escape->new($FORM{cutei});
my $cutei = chr(0x0001F3C3); # "\xf0\x9f\x98\x80"; #$ucutei->unescape;
#my $uimgei = Unicode::Escape->new($FORM{imgei});
my $imgei = chr(0x0001F5BC); # "\xf0\x9f\x98\x80"; #$uimgei->unescape;
$dt = DateTime->now; # same as ( epoch => time )
$dttz = DateTime::TimeZone->new( name => 'local' )->name();
$dt->set_time_zone( $dttz );
$year = $dt->year;
$month = $dt->month; # 1-12
$day = $dt->day; # 1-31
$dow = $dt->day_of_week; # 1-7 (Monday is 1)
$hour = $dt->hour; # 0-23
$minute = $dt->minute; # 0-59
$second = $dt->second; # 0-61 (leap seconds!)
$doy = $dt->day_of_year; # 1-366 (leap years)
$doq = $dt->day_of_quarter; # 1..
$qtr = $dt->quarter; # 1-4
$dmy = $dt->dmy('/'); # 06/12/2002
$hms = $dt->hms; # 14:02:29
$tt = "";
$cont = "";
$oneletter = " ";
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "<script type=text/javascript>";
print " var ls='', ns='', qwr=''; ";
print " function zeroize(iinsf) { var insf=iinsf; var sep='/', i=0; while (insf.substring(i).substring(0,1) >= '0' && insf.substring(i).substring(0,1) <= '9') { i++; } sep=insf.substring(i).substring(0,1); ";
print " var arr=insf.split(sep); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(sep + arr[1] + sep, sep + '0' + arr[1] + sep); } ";
print " if (eval('' + arr[0].length) == 1) { insf='0' + insf; } ";
print " arr=insf.split(' ')[1].split(':'); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (insf.indexOf(' pm') != -1) { insf=insf.replace(' ' + arr[0].trim() + ':', ' ' + eval(12 + eval('' + arr[0].trim())) + ':').replace(' pm', ' '); arr=insf.split(' ')[1].split(':'); } ";
print " if (eval(-1 + eval('' + arr[1])) == eval('' + document.getElementById('mystime').innerHTML.split(':')[1])) { insf=insf.replace(':' + arr[1] + ':', ':' + document.getElementById('mystime').innerHTML.split(':')[1] + ':'); arr[1]='zz'; } ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(':' + arr[1] + ':', ':0' + arr[1] + ':'); } ";
print " if (eval('' + arr[0].trim().length) == 1) { insf=insf.replace(arr[0].trim() + ':', '0' + arr[0].trim() + ':'); } ";
print " } ";
print " } ";
print " return insf; ";
print " } ";
print " function loctime() { if (ls == '') { ";
print " qwr='' + Intl.DateTimeFormat().resolvedOptions().timeZone; ";
print " var d = new Date(); ";
print " d.toLocaleString('en-US', { timeZone: qwr }); ";
print " ns=document.getElementById('mystime').innerHTML.split(':')[2].split(' ')[0]; ";
print " ls=zeroize(('' + d.toLocaleString()).replace(',','')) + ' <a onclick=alert(qwr); style=cursor:pointer;text-decoration:underline;>GMT' + d.toString().split(' GMT')[1] + '</a>'; } if (!document.getElementById('mytime')) { setTimeout(loctime,1000); } else { ";
print " document.getElementById('mytime').innerHTML=ls.replace(':' + ls.split(':')[2].split(' ')[0] + ' ', ':' + ns + ' '); } ";
print " } ";
print "</script>";
print "</head>";
print "<body onload=setTimeout(loctime,1000);>";
print "<h2 style='font-family:Courier New;'>Hello $first_name $last_name <br> Local: <span id=mytime></span><br>Server: <span id=mystime>$dmy $hms</span> $dttz </h2><h4>";
if ($blogp == "") {
$blogp = $first_name;
$oneletter = substr( $blogp, 0, 1 );
}
if ($blogp != "") {
$oneletter = substr( $blogp, 0, 1 );
if ((ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")) || ord($oneletter) == 40 ){
my ($oneurl) = split /,/, $blogp;
if ($oneurl == $blogp) {
$oneletter = substr( $blogp, 0, 1 );
} else {
if (ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")) {
$oneletter = "1";
$blogp = "( $blogp )";
} else {
$oneletter = "2";
}
}
}
}
if (ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")){
# /*** mysql hostname ***/
$hostname = 'dbhost';
# /*** mysql username ***/
$username = 'username';
# /*** mysql ***/
$password = 'password';
$dbname = 'dbname';
$tname = 'table_name';
$hostname = "DBI:mysql:$dbname:$hostname";
$dbh = DBI->connect($hostname, $username, $password);
my ($oneaurl) = split /,/, $blogp;
if (index($blogp, ',') <= 0) {
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID=?");
} else {
if (ord($oneletter) != 40) {
$blogp = "($blogp)";
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID in $blogp");
$blogp = "";
} else {
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID in $blogp");
$blogp = "";
}
}
if ($blogp == "") {
$sth->execute();
} else {
$sth->execute( $blogp );
}
$one = 0;
$two = 10;
$exc = 0;
$coln = "A";
$dcol = 1;
$rown = 6;
$trown = 6;
$alinkis = "";
my $workbook = 0; # Spreadsheet::WriteExcel->new('../perl.xls');
my $date_format = 0;
my $date = 0;
# Set the default format for dates.
while (($tt, $cont, $iid) = $sth->fetchrow_array()) {
my ($idis) = split /,/, $blogi, 1;
my ($yyyymmdd) = substr( $blogt, $one, $two ); # split /,/, $blogt, 1;
my ($junk, $imageu) = split / src="/, $cont;
my ($imageurl) = split /"/, $imageu;
my ($junk, $cutto) = split / href="/, $cont;
my ($cuttothechase) = split /"/, $cutto;
$blogt = substr( $blogt, 11 );
if ($exc == 0) {
$exc = 1;
# Create a new Excel workbook
$workbook = Spreadsheet::WriteExcel->new('../perl.xls');
# Add a worksheet
$worksheet = $workbook->add_worksheet();
$worksheet->set_column(0, 0, 90);
$worksheet->set_column(1, 0, 30);
# Add and define a format
$format = $workbook->add_format(); # Add a format
$format->set_bold();
$format->set_color('red');
$format->set_align('center');
# my $date_format = $workbook->add_format({num_format => 'yyyy-mm-ddThh:mm:ss.sss'});
$date_format = $workbook->add_format(
bold => 1,
align => 'center',
num_format => 'yyyy-mm-dd hh:mm'
);
# Write a formatted and unformatted string, row and column notation.
$col = $row = 0;
$worksheet->write($row, $col, "$first_name $last_name", $format);
# $worksheet->write(1, $col, 'Hi Excel!');
# Write a number and a formula using A1 notation
$worksheet->write("${coln}3", "$tt");
$date = sprintf "%sT03:00:00.000Z", $yyyymmdd;
$worksheet->write_date_time(2, $dcol, $date, $date_format);
$worksheet->write("${coln}4", "https://www.rjmprogramming.com.au/ITblog/?p=$iid" . "#$emoji");
$date = sprintf "%sT03:01:00.000Z", $yyyymmdd;
$worksheet->write_date_time(3, $dcol, $date, $date_format);
$worksheet->write("${coln}5", "$cuttothechase" . "#$cutei");
$date = sprintf "%sT03:02:00.000Z", $yyyymmdd;
$worksheet->write_date_time(4, $dcol, $date, $date_format);
$worksheet->write("${coln}6", "$imageurl" . "#$imgei");
$date = sprintf "%sT03:03:00.000Z", $yyyymmdd;
$worksheet->write_date_time(5, $dcol, $date, $date_format);
$alinkis = "<a download=perl.xls href='//www.rjmprogramming.com.au/perl.xls' title='Download Excel spreadsheet'>📄</a> <a target=_blank href=$cuttothechase title='Cut to the chase'>🏃</a> <a target=_blank href=$imageurl title='Image URL'>🖼</a>";
print "<details id=myd><summary style=background-color:#f0f0f0; id=mys><h1><a target=_blank href='//www.rjmprogramming.com.au/ITblog/?p=$iid'>$tt</a> $alinkis</h1></summary>$cont</details>\n";
} else {
$trown += 4;
$date = sprintf "%sT03:00:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "$tt");
$date = sprintf "%sT03:01:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "https://www.rjmprogramming.com.au/ITblog/?p=$iid" . "#$emoji");
$date = sprintf "%sT03:02:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "$cuttothechase" . "#$cutei");
$date = sprintf "%sT03:03:00.000Z", $yyyymmdd;
$worksheet->write_date_time($rown, $dcol, $date, $date_format);
$rown += 1;
$worksheet->write("${coln}${rown}", "$imageurl" . "#$imgei");
$alinkis = "<a download=perl.xls href='//www.rjmprogramming.com.au/perl.xls' title='Download Excel spreadsheet'>📄</a> <a target=_blank href=$cuttothechase title='Cut to the chase'>🏃</a> <a target=_blank href=$imageurl title='Image URL'>🖼</a>";
print "<details class=myd><summary style=background-color:#f0f0f0; class=mys><h1><a target=_blank href='//www.rjmprogramming.com.au/ITblog/?p=$iid'>$tt</a> $alinkis</h1></summary>$cont</details>\n";
}
# print "@row\n";
}
# }
}
print "</h4><br><br><iframe id=myif src='/hello_get.html' style='width:100%; height:1500px;'></iframe></body>";
print "</html>";
1;
โฆ in thechanged Perl hello_getcgi code supervised by (and working with) thechanged HTML hello_get
html web
application we hope you try for yourself.
Previous relevant Perl CGI Spreadsheet Module Tutorial is shown below.
Adding onto yesterdayโs Perl CGI DBI Module Tutorial โฆ
- use of Perl module DateTime โฆ
- use of Perl module DBI โฆ Perl module exploration, and today, we dip our toes into โฆ
- use of Perl module Spreadsheet::WriteExcel โฆ and to help with simulating PHPโs explode function โฆ
- use of Perl module Data::Dumper and its split function
โฆ with modified Perl code as per โฆ
#!/usr/bin/perl
use DateTime;
use DateTime::TimeZone;
use DBI;
use DBD::mysql;
use Spreadsheet::WriteExcel;
use Data::Dumper qw(Dumper);
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$first_name = $FORM{first_name};
$last_name = $FORM{last_name};
$blogp = $FORM{blogp};
$blogp =~ s/%2C/,/ig;
$oneletter = substr( $first_name, 0, 1 );
$dt = DateTime->now; # same as ( epoch => time )
$dttz = DateTime::TimeZone->new( name => 'local' )->name();
$dt->set_time_zone( $dttz );
$year = $dt->year;
$month = $dt->month; # 1-12
$day = $dt->day; # 1-31
$dow = $dt->day_of_week; # 1-7 (Monday is 1)
$hour = $dt->hour; # 0-23
$minute = $dt->minute; # 0-59
$second = $dt->second; # 0-61 (leap seconds!)
$doy = $dt->day_of_year; # 1-366 (leap years)
$doq = $dt->day_of_quarter; # 1..
$qtr = $dt->quarter; # 1-4
$dmy = $dt->dmy('/'); # 06/12/2002
$hms = $dt->hms; # 14:02:29
$tt = "";
$cont = "";
$oneletter = " ";
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "<script type=text/javascript>";
print " var ls='', ns='', qwr=''; ";
print " function zeroize(iinsf) { var insf=iinsf; var sep='/', i=0; while (insf.substring(i).substring(0,1) >= '0' && insf.substring(i).substring(0,1) <= '9') { i++; } sep=insf.substring(i).substring(0,1); ";
print " var arr=insf.split(sep); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(sep + arr[1] + sep, sep + '0' + arr[1] + sep); } ";
print " if (eval('' + arr[0].length) == 1) { insf='0' + insf; } ";
print " arr=insf.split(' ')[1].split(':'); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (insf.indexOf(' pm') != -1) { insf=insf.replace(' ' + arr[0].trim() + ':', ' ' + eval(12 + eval('' + arr[0].trim())) + ':').replace(' pm', ' '); arr=insf.split(' ')[1].split(':'); } ";
print " if (eval(-1 + eval('' + arr[1])) == eval('' + document.getElementById('mystime').innerHTML.split(':')[1])) { insf=insf.replace(':' + arr[1] + ':', ':' + document.getElementById('mystime').innerHTML.split(':')[1] + ':'); arr[1]='zz'; } ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(':' + arr[1] + ':', ':0' + arr[1] + ':'); } ";
print " if (eval('' + arr[0].trim().length) == 1) { insf=insf.replace(arr[0].trim() + ':', '0' + arr[0].trim() + ':'); } ";
print " } ";
print " } ";
print " return insf; ";
print " } ";
print " function loctime() { if (ls == '') { ";
print " qwr='' + Intl.DateTimeFormat().resolvedOptions().timeZone; ";
print " var d = new Date(); ";
print " d.toLocaleString('en-US', { timeZone: qwr }); ";
print " ns=document.getElementById('mystime').innerHTML.split(':')[2].split(' ')[0]; ";
print " ls=zeroize(('' + d.toLocaleString()).replace(',','')) + ' <a onclick=alert(qwr); style=cursor:pointer;text-decoration:underline;>GMT' + d.toString().split(' GMT')[1] + '</a>'; } if (!document.getElementById('mytime')) { setTimeout(loctime,1000); } else { ";
print " document.getElementById('mytime').innerHTML=ls.replace(':' + ls.split(':')[2].split(' ')[0] + ' ', ':' + ns + ' '); } ";
print " } ";
print "</script>";
print "</head>";
print "<body onload=setTimeout(loctime,1000);>";
print "<h2 style='font-family:Courier New;'>Hello $first_name $last_name <br> Local: <span id=mytime></span><br>Server: <span id=mystime>$dmy $hms</span> $dttz </h2><h4>";
if ($blogp == "") {
$blogp = $first_name;
$oneletter = substr( $blogp, 0, 1 );
}
if ($blogp != "") {
$oneletter = substr( $blogp, 0, 1 );
if ((ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")) || ord($oneletter) == 40 ){
my ($oneurl) = split /,/, $blogp;
if ($oneurl == $blogp) {
$oneletter = substr( $blogp, 0, 1 );
} else {
if (ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")) {
$oneletter = "1";
$blogp = "( $blogp )";
} else {
$oneletter = "2";
}
}
}
}
if (ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")){
# /*** mysql hostname ***/
$hostname = 'dbhost';
# /*** mysql username ***/
$username = 'username';
# /*** mysql ***/
$password = 'password';
$dbname = 'dbname';
$tname = 'table_name';
$hostname = "DBI:mysql:$dbname:$hostname";
$dbh = DBI->connect($hostname, $username, $password);
my ($oneaurl) = split /,/, $blogp;
if (index($blogp, ',') <= 0) {
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID=?");
} else {
if (ord($oneletter) != 40) {
$blogp = "($blogp)";
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID in $blogp");
$blogp = "";
} else {
$sth = $dbh->prepare("SELECT post_title, post_content, ID FROM $tname WHERE ID in $blogp");
$blogp = "";
}
}
if ($blogp == "") {
$sth->execute();
} else {
$sth->execute( $blogp );
}
$exc = 0;
$coln = "A";
$rown = 6;
$alinkis = "";
my $workbook = 0; # Spreadsheet::WriteExcel->new('../perl.xls');
while (($tt, $cont, $iid) = $sth->fetchrow_array()) {
my ($junk, $imageu) = split / src="/, $cont;
my ($imageurl) = split /"/, $imageu;
my ($junk, $cutto) = split / href="/, $cont;
my ($cuttothechase) = split /"/, $cutto;
if ($exc == 0) {
$exc = 1;
# Create a new Excel workbook
$workbook = Spreadsheet::WriteExcel->new('../perl.xls');
# Add a worksheet
$worksheet = $workbook->add_worksheet();
# Add and define a format
$format = $workbook->add_format(); # Add a format
$format->set_bold();
$format->set_color('red');
$format->set_align('center');
# Write a formatted and unformatted string, row and column notation.
$col = $row = 0;
$worksheet->write($row, $col, "$first_name $last_name", $format);
# $worksheet->write(1, $col, 'Hi Excel!');
# Write a number and a formula using A1 notation
$worksheet->write("${coln}3", "$tt");
$worksheet->write("${coln}4", "https://www.rjmprogramming.com.au/ITblog/?p=$iid");
$worksheet->write("${coln}5", "$cuttothechase");
$worksheet->write("${coln}6", "$imageurl");
$alinkis = "<a download=perl.xls href='//www.rjmprogramming.com.au/perl.xls' title='Download Excel spreadsheet'>📄</a> <a target=_blank href=$cuttothechase title='Cut to the chase'>🏃</a> <a target=_blank href=$imageurl title='Image URL'>🖼</a>";
print "<details id=myd><summary id=mys><h1><a target=_blank href='//www.rjmprogramming.com.au/ITblog/?p=$iid'>$tt</a> $alinkis</h1></summary>$cont</details>\n";
} else {
$rown += 1;
$worksheet->write("${coln}${rown}", "$tt");
$rown += 1;
$worksheet->write("${coln}${rown}", "https://www.rjmprogramming.com.au/ITblog/?p=$iid");
$rown += 1;
$worksheet->write("${coln}${rown}", "$cuttothechase");
$rown += 1;
$worksheet->write("${coln}${rown}", "$imageurl");
$alinkis = "<a download=perl.xls href='//www.rjmprogramming.com.au/perl.xls' title='Download Excel spreadsheet'>📄</a> <a target=_blank href=$cuttothechase title='Cut to the chase'>🏃</a> <a target=_blank href=$imageurl title='Image URL'>🖼</a>";
print "<details class=myd><summary class=mys><h1><a target=_blank href='//www.rjmprogramming.com.au/ITblog/?p=$iid'>$tt</a> $alinkis</h1></summary>$cont</details>\n";
}
# print "@row\n";
}
# }
}
print "</h4><br><br><iframe id=myif src='/hello_get.html' style='width:100%; height:1500px;'></iframe></body>";
print "</html>";
1;
โฆ creating optionally downloadable Excel spreadsheet documents reflecting user selections, in thechanged Perl hello_getcgi code supervised by (and working with) thechanged HTML hello_get
html web
application, where, as of today, the dropdown for WordPress Blog Posting selection accepts multiple selections โฆ
โฆ with โฆ
- First and Last Name โฆ in webpage and spreadsheet
- Local and Server timestamps โฆ in webpage
- Blog Posting link(s) โฆ in webpage and spreadsheet
- Download Spreadsheet (total synopsis) download link(s) โฆ in webpage
- Cut to the Chase (ie. the action item of a blog posting) link(s) โฆ in webpage and spreadsheet
- Image URL (ie. the topmost image of a blog posting) link(s) โฆ in webpage and spreadsheet
โฆ data items featuring.
Previous relevant Perl CGI DBI Module Tutorial is shown below.
Onto yesterdayโs Perl CGI DateTime Module Primer Tutorialโs โฆ
- use of Perl module DateTime โฆ today, we try out โฆ
- use of Perl module DBI
โฆ to facilitate the connection and use of database functionalities, in our case MySql, as per our changed Perl code to read off our WordPress blog MySql database table fields โฆ
- post_title
- post_content
โฆ fields describing WordPress blog โPosting Titleโ and โPosting HTML Contentโ respectively, as per โฆ
#!/usr/bin/perl
use DateTime;
use DateTime::TimeZone;
use DBI;
use DBD::mysql;
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$first_name = $FORM{first_name};
$last_name = $FORM{last_name};
$blogp = $FORM{blogp};
$oneletter = substr( $first_name, 0, 1 );
$dt = DateTime->now; # same as ( epoch => time )
$dttz = DateTime::TimeZone->new( name => 'local' )->name();
$dt->set_time_zone( $dttz );
$year = $dt->year;
$month = $dt->month; # 1-12
$day = $dt->day; # 1-31
$dow = $dt->day_of_week; # 1-7 (Monday is 1)
$hour = $dt->hour; # 0-23
$minute = $dt->minute; # 0-59
$second = $dt->second; # 0-61 (leap seconds!)
$doy = $dt->day_of_year; # 1-366 (leap years)
$doq = $dt->day_of_quarter; # 1..
$qtr = $dt->quarter; # 1-4
$dmy = $dt->dmy('/'); # 06/12/2002
$hms = $dt->hms; # 14:02:29
$tt = "";
$cont = "";
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "<script type=text/javascript>";
print " var ls='', ns='', qwr=''; ";
print " function zeroize(iinsf) { var insf=iinsf; var sep='/', i=0; while (insf.substring(i).substring(0,1) >= '0' && insf.substring(i).substring(0,1) <= '9') { i++; } sep=insf.substring(i).substring(0,1); ";
print " var arr=insf.split(sep); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(sep + arr[1] + sep, sep + '0' + arr[1] + sep); } ";
print " if (eval('' + arr[0].length) == 1) { insf='0' + insf; } ";
print " arr=insf.split(' ')[1].split(':'); ";
print " if (eval('' + arr.length) >= 3) { ";
print " if (insf.indexOf(' pm') != -1) { insf=insf.replace(' ' + arr[0].trim() + ':', ' ' + eval(12 + eval('' + arr[0].trim())) + ':').replace(' pm', ' '); arr=insf.split(' ')[1].split(':'); } ";
print " if (eval(-1 + eval('' + arr[1])) == eval('' + document.getElementById('mystime').innerHTML.split(':')[1])) { insf=insf.replace(':' + arr[1] + ':', ':' + document.getElementById('mystime').innerHTML.split(':')[1] + ':'); arr[1]='zz'; } ";
print " if (eval('' + arr[1].length) == 1) { insf=insf.replace(':' + arr[1] + ':', ':0' + arr[1] + ':'); } ";
print " if (eval('' + arr[0].trim().length) == 1) { insf=insf.replace(arr[0].trim() + ':', '0' + arr[0].trim() + ':'); } ";
print " } ";
print " } ";
print " return insf; ";
print " } ";
print " function loctime() { if (ls == '') { ";
print " qwr='' + Intl.DateTimeFormat().resolvedOptions().timeZone; ";
print " var d = new Date(); ";
print " d.toLocaleString('en-US', { timeZone: qwr }); ";
print " ns=document.getElementById('mystime').innerHTML.split(':')[2].split(' ')[0]; ";
print " ls=zeroize(('' + d.toLocaleString()).replace(',','')) + ' <a onclick=alert(qwr); style=cursor:pointer;text-decoration:underline;>GMT' + d.toString().split(' GMT')[1] + '</a>'; } if (!document.getElementById('mytime')) { setTimeout(loctime,1000); } else { ";
print " document.getElementById('mytime').innerHTML=ls.replace(':' + ls.split(':')[2].split(' ')[0] + ' ', ':' + ns + ' '); } ";
print " } ";
print "</script>";
print "</head>";
print "<body onload=setTimeout(loctime,1000);>";
print "<h2 style='font-family:Courier New;'>Hello $first_name $last_name <br> Local: <span id=mytime></span><br>Server: <span id=mystime>$dmy $hms</span> $dttz </h2><h4>";
if ($blogp == "") { $blogp = $first_name; } else { $oneletter = substr( $blogp, 0, 1 ); }
if (ord($oneletter) >= ord("0") && ord($oneletter) <= ord("9")){
# /*** mysql hostname ***/
$hostname = 'dbhost';
# /*** mysql username ***/
$username = 'username';
# /*** mysql ***/
$password = 'password';
$dbname = 'dbname';
$tname = 'table_name';
$hostname = "DBI:mysql:$dbname:$hostname";
$dbh = DBI->connect($hostname, $username, $password);
$sth = $dbh->prepare("SELECT post_title, post_content FROM $tname WHERE ID=?");
$sth->execute( $blogp );
#while ( @row = $sth->fetchrow_array ) {
while (($tt, $cont) = $sth->fetchrow_array()) {
print "<details id=myd><summary id=mys><h1><a target=_blank href='//www.rjmprogramming.com.au/ITblog/?p=$blogp'>$tt</a></h1></summary>$cont</details>\n";
# print "@row\n";
}
# }
}
print "</h4><br><br><iframe id=myif src='/hello_get.html' style='width:100%; height:1500px;'></iframe></body>";
print "</html>";
1;
โฆ in thechanged Perl hello_getcgi code supervised by (and working with) thechanged HTML hello_get
html web
application.
Previous relevant Perl CGI DateTime Module Primer Tutorial is shown below.
Do you remember with Perl CGI Modes of Use Primer Tutorial how we used the Perl programming language as an alternative server language player up at the RJM Programming website?
Well, in any case, today, we explore some Perl modules which can make that code be more useful to us. As for how we interface to it, weโve written a very simple HTML hello_gethtml interfacer weโd like to thank https://www.tutorialspoint.com/perl/perl_cgi.htm for โฆ
<html>
<head>
<title>hello_get ... Thanks to https://www.tutorialspoint.com/perl/perl_cgi.htm and https://perlmaven.com/most-popular-perl-modules and https://www.freecodecamp.org/news/how-to-format-dates-in-javascript/</title>
</head>
<body>
<h1>hello_get.html HTML calls on hello_get.cgi Perl</h1>
<h3>RJM Programming - February, 2023</h3>
<h3>Thanks to https://www.tutorialspoint.com/perl/perl_cgi.htm and https://perlmaven.com/most-popular-perl-modules and https://www.freecodecamp.org/news/how-to-format-dates-in-javascript/</h3>
<br><br>
<FORM action = "/cgi-bin/hello_get.cgi" method = "GET">
First Name: <input type = "text" name = "first_name"> <br>
Last Name: <input type = "text" name = "last_name">
<input type = "submit" value = "Submit">
</FORM>
</body>
</html>
โฆ where this webpageโs HTML formโs action points to some Perl hello_getcgi (up at the Apache web server, in a cgi-bin folder and with the executable permission bit set) code using the DateTime module, and showing the user client local timezone current time compared to this RJM Programmingโs server timezone current time โฆ
#!/usr/bin/perl
use DateTime;
use DateTime::TimeZone;
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$first_name = $FORM{first_name};
$last_name = $FORM{last_name};
$dt = DateTime->now; # same as ( epoch => time )
$dttz = DateTime::TimeZone->new( name => 'local' )->name();
$dt->set_time_zone( $dttz );
$year = $dt->year;
$month = $dt->month; # 1-12
$day = $dt->day; # 1-31
$dow = $dt->day_of_week; # 1-7 (Monday is 1)
$hour = $dt->hour; # 0-23
$minute = $dt->minute; # 0-59
$second = $dt->second; # 0-61 (leap seconds!)
$doy = $dt->day_of_year; # 1-366 (leap years)
$doq = $dt->day_of_quarter; # 1..
$qtr = $dt->quarter; # 1-4
$dmy = $dt->dmy('/'); # 06/12/2002
$hms = $dt->hms; # 14:02:29
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "<script type=text/javascript>";
print " function loctime() { ";
print " var qwr='' + Intl.DateTimeFormat().resolvedOptions().timeZone; ";
print " var d = new Date(); ";
print " d.toLocaleString('en-US', { timeZone: qwr }); ";
print " var ns=document.getElementById('mystime').innerHTML.split(':')[2].split(' ')[0]; ";
print " var ls=('' + d.toLocaleString()).replace(',','') + ' GMT' + d.toString().split(' GMT')[1]; ";
print " document.getElementById('mytime').innerHTML=ls.replace(':' + ls.split(':')[2].split(' ')[0] + ' ', ':' + ns + ' '); ";
print " } ";
print "</script>";
print "</head>";
print "<body onload=loctime();>";
print "<h2 style='font-family:Courier New;'>Hello $first_name $last_name <br> Local: <span id=mytime></span><br>Server: <span id=mystime>$dmy $hms</span> $dttz </h2>";
print "<br><br><iframe src='/hello_get.html' style='width:100%; height:1500px;'></iframe></body>";
print "</html>";
1;
The use of the HTML iframe in the Perl (writing HTML) code means that either โฆ
โฆ can be used to get the โPerl ballโ rolling.
Previous relevant Perl CGI Modes of Use Primer Tutorial is shown below.
A little while back we revisited the โPHP Modes of Useโ discussion last talked about with PHP Modes of Use File Traverse Tutorial as shown below. Weโre revisiting today to show you that it doesnโt have to be only your principal server language of choice involved in this kind of thinking, but you can also talk in terms of your chosen CGI (Common Gateway Interface) language of choice, which happens to be Perl here at the www.rjmprogramming.com.au domain. The other common partner of PHP as its CGI partner is Python.
Do you recall the three modes of use we talk about? Here they are below โฆ
- web browsing โฆ via use of a GET arguments URL
- curl (from the command line โฆ in our case Linux, but could be Windows too)
- command line (Perl) โฆ use the $ARGV[] array variables
โฆ all good? โฆ okay, we keep those three, but with the curl option would also like to show that it can be subdivided into two submodes, namely โฆ
- GET mode (via URL)
- POST mode (via simulated FORM (results))
โฆ which is probably true of non-CGI scenarios as well, but is particularly pertinent to discussions of CGI because we generally think of the CGI language as the one POSTed to from an HTML form, but, here, hopefully, we extend your mode of thinking to the thought that curl can handle you sending in the GET mode (though that is awkward with more than one GET argument and using the & syntax) or via the use of various non-default switches such as -d โname1=value1 name2=value2 blah3=blahval3โ to simulate a (form) POST โฆ pretty powerful, huh โฆ enabling what you may have only initially thought of as a piece of code to process an HTML form, into the additional possibility of a standalone piece of code that curl can control independent of a web browser, as required. Please note, though, for those of us who want to โdo their heads inโ for โOnion of the 4th Dimensionโ possibilities, the PHP exec command, or something like it could be used with options 3 and/or 2a and/or 2b to create other possibilities.
Anyway, have a look (closely) at our tutorial picture and our web browser mode liverun โฆ
โฆ and have a look at the associated Perl CGI programming source code to read an *.htm* or *.txt file in readfilecgi
Of great help for this tutorial was Perl and CGI for the World Wide Web by Elizabeth Castro page 195 (in particular) and What is the cURL command-line syntax to do a POST request? (for help regarding curl command line switches for POSTed data) โฆ thanks.
Will leave you with a recall of the discussions regarding PHP (our primary non-CGI server language of choice at www.rjmprogramming.com.au) below โฆ
Previous relevant PHP Modes of Use File Traverse Tutorial is shown below.
Itโs been a while since we last visited the โPHP Modes of Useโ discussion last talked about with C Supervised PHP Modes of Use Primer Tutorial as shown below. Weโre revisiting today to show you a practical application of writing PHP for those three modes of use โฆ namely โฆ
- web browsing โฆ use a form, then post, and then access the $_POST[] variables
- curl โฆ note how only the one $_GET[] variable is available because of trouble using & on Linux command line (use of @ is a local idea โฆ there would be many other ideas that will work)
- command line PHP โฆ use the $argv array variables
โฆ so have a look at the PHP programming source code to traverse a directoryโs files in doviadirphp with the live โweb browsingโ live
run.
Will leave you with a recall of the discussion below โฆ
Recently, with our tutorial, PHP Modes of Use Primer Tutorial as shown below, we talked about three modes of use or access for the use of PHP and today we encase all that thinking in a Mac OS X command line C program supervision framework. Youโd expect that this is possible in a command line or Intranet environment where Linux or unix (or, for that matter, Windows command line โฆ though the code here wonโt work for this one) where C can have such a close relationship to the Linux or unix kernel, that it would be able the โrun the whole showโ as well as the shell commands themselves can. Todayโs C has been compiled using the Xcode IDE for use with a Mac OS X operating system.
So thatโs the supervisor above. Read, again, about โthe supervisedโ, below โฆ
PHP is a very flexible programming language. It is probably best known as a server-side language called by a web browser using the http transport layer (ie. web browsing, or โsurfing the netโ). Letโs list three modes of use of PHP below:
- Via http transport layer (ie. web browsing, or โsurfing the netโ) in an Internet mode of use (eg: //localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser
- Via curl in an Internet mode of use (eg. curl //localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) at Linux or unix or Windows command line to default output (and you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)
- Via command line PHP in Linux or unix or Windows command line in a command or Intranet mode of use (eg. php in_tra_ter_net.php SERVER_NAME=localhost) at a Linux or unix or Windows command line (where the PHP code has been placed, in our example (ie. it is not a URL, but is a file specification of your (Intranet) server)) to default output (or you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)
Does it matter how the PHP is accessed? Well, yes, it does โฆ sometimes, but not if the code is restricted to a particular mode of usage (but part of what this tutorial is about is to get across how flexible and powerful PHP can be). With the last option above, lots of $_SERVER[] variables are undefined, for example.
- $_SERVER[โHTTP_COOKIEโ] is defined, unlike with curl usage (this seems to be a possibility as a way to detect curl usage). Most $_SERVER[] variables are defined, but $argv[] array is not.
- $_SERVER[โHTTP_COOKIEโ] is undefined, but a lot of the rest of $_SERVER[] variables are as above.
- Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[โSCRIPT_FILENAMEโ] which may help you with Intranet application usage. You cannot enter $_GET[] arguments (on the command line (ie. you will get a syntax error if you try)) so these are undefined, but $argv[] array can be used in that awkward way shown, so that $argv[1] can be arranged to be a lot like $_SERVER[โQUERY_STRINGโ] (ie. the bits after ? in a URL).
So, maybe you have the one mode of use in mind, but you need to think a bit to support all the modes of use, if they are all to have a role with the PHP you write, because, despite these differences it is good to use $_SERVER[] and $_GET[] and $_POST[] and $_SESSION[] and $argv[] and whatever else in terms of variable information PHP offers in terms of attempting to write generic code.
Hope you get something out of todayโs discussion of our Mac OS X MAMP Apache local Web server environment example (hence the //localhost:8888/ you see in the URLs above), but please bear in mind that there would be other modes of use once you think of other client HTML element ways of accessing PHP (all a lot like the โweb browsingโ of above) or other possibilities within PHP code itself via PHP commands exec and file_get_contents the latter of which can be very useful during Intranet usage where there is no curl available, perhaps. Instead of curl you could use wget as another approach.
You may want to see a liverun (web browsing) here at the rjmprogramming.com.au domain of the supervised PHP (alone), or you may want to download the supervised PHP programming source code you could call in_tra_ter_net
php or you may want to download the supervisory main
c as you require.
A โstop pressโ to do with code above is that since the โqueueing upโ of this blog post a new posting is relevant, the recent PHP Modes of Use Follow Up Tutorial, and that to incorporate this postingโs improvements over PHP Modes of Use Primer Tutorial, youโd want to substitute all mentions of in_tra_ter_net with more_in_tra_ter_net in the code of main.c as above. The relevant changed PHP source code is more_in_tra_ter_netphp
Previous relevant C Supervised PHP Modes of Use Primer Tutorial is shown below.
Recently, with our tutorial, PHP Modes of Use Primer Tutorial as shown below, we talked about three modes of use or access for the use of PHP and today we encase all that thinking in a Mac OS X command line C program supervision framework. Youโd expect that this is possible in a command line or Intranet environment where Linux or unix (or, for that matter, Windows command line โฆ though the code here wonโt work for this one) where C can have such a close relationship to the Linux or unix kernel, that it would be able the โrun the whole showโ as well as the shell commands themselves can. Todayโs C has been compiled using the Xcode IDE for use with a Mac OS X operating system.
So thatโs the supervisor above. Read, again, about โthe supervisedโ, below โฆ
PHP is a very flexible programming language. It is probably best known as a server-side language called by a web browser using the http transport layer (ie. web browsing, or โsurfing the netโ). Letโs list three modes of use of PHP below:
- Via http transport layer (ie. web browsing, or โsurfing the netโ) in an Internet mode of use (eg: //localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser
- Via curl in an Internet mode of use (eg. curl //localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) at Linux or unix or Windows command line to default output (and you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)
- Via command line PHP in Linux or unix or Windows command line in a command or Intranet mode of use (eg. php in_tra_ter_net.php SERVER_NAME=localhost) at a Linux or unix or Windows command line (where the PHP code has been placed, in our example (ie. it is not a URL, but is a file specification of your (Intranet) server)) to default output (or you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)
Does it matter how the PHP is accessed? Well, yes, it does โฆ sometimes, but not if the code is restricted to a particular mode of usage (but part of what this tutorial is about is to get across how flexible and powerful PHP can be). With the last option above, lots of $_SERVER[] variables are undefined, for example.
- $_SERVER[โHTTP_COOKIEโ] is defined, unlike with curl usage (this seems to be a possibility as a way to detect curl usage). Most $_SERVER[] variables are defined, but $argv[] array is not.
- $_SERVER[โHTTP_COOKIEโ] is undefined, but a lot of the rest of $_SERVER[] variables are as above.
- Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[โSCRIPT_FILENAMEโ] which may help you with Intranet application usage. You cannot enter $_GET[] arguments (on the command line (ie. you will get a syntax error if you try)) so these are undefined, but $argv[] array can be used in that awkward way shown, so that $argv[1] can be arranged to be a lot like $_SERVER[โQUERY_STRINGโ] (ie. the bits after ? in a URL).
So, maybe you have the one mode of use in mind, but you need to think a bit to support all the modes of use, if they are all to have a role with the PHP you write, because, despite these differences it is good to use $_SERVER[] and $_GET[] and $_POST[] and $_SESSION[] and $argv[] and whatever else in terms of variable information PHP offers in terms of attempting to write generic code.
Hope you get something out of todayโs discussion of our Mac OS X MAMP Apache local Web server environment example (hence the //localhost:8888/ you see in the URLs above), but please bear in mind that there would be other modes of use once you think of other client HTML element ways of accessing PHP (all a lot like the โweb browsingโ of above) or other possibilities within PHP code itself via PHP commands exec and file_get_contents the latter of which can be very useful during Intranet usage where there is no curl available, perhaps. Instead of curl you could use wget as another approach.
You may want to see a liverun (web browsing) here at the rjmprogramming.com.au domain of the supervised PHP (alone), or you may want to download the supervised PHP programming source code you could call in_tra_ter_net
php or you may want to download the supervisory main
c as you require.
A โstop pressโ to do with code above is that since the โqueueing upโ of this blog post a new posting is relevant, the recent PHP Modes of Use Follow Up Tutorial, and that to incorporate this postingโs improvements over PHP Modes of Use Primer Tutorial, youโd want to substitute all mentions of in_tra_ter_net with more_in_tra_ter_net in the code of main.c as above. The relevant changed PHP source code is more_in_tra_ter_netphp
Previous relevant PHP Modes of Use Primer Tutorial is shown below.
PHP is a very flexible programming language. It is probably best known as a server-side language called by a web browser using the http transport layer (ie. web browsing, or โsurfing the netโ). Letโs list three modes of use of PHP below:
- Via http transport layer (ie. web browsing, or โsurfing the netโ) in an Internet mode of use (eg: //localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) in address bar of a web browser
- Via curl in an Internet mode of use (eg. curl //localhost:8888/in_tra_ter_net.php?SERVER_NAME=localhost) at Linux or unix or Windows command line to default output (and you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)
- Via command line PHP in Linux or unix or Windows command line in a command or Intranet mode of use (eg. php in_tra_ter_net.php SERVER_NAME=localhost) at a Linux or unix or Windows command line (where the PHP code has been placed, in our example (ie. it is not a URL, but is a file specification of your (Intranet) server)) to default output (or you can redirect as required via > outfile or >> outfile or 2> errfile or 2>> errfile)
Does it matter how the PHP is accessed? Well, yes, it does โฆ sometimes, but not if the code is restricted to a particular mode of usage (but part of what this tutorial is about is to get across how flexible and powerful PHP can be). With the last option above, lots of $_SERVER[] variables are undefined, for example.
- $_SERVER[โHTTP_COOKIEโ] is defined, unlike with curl usage (this seems to be a possibility as a way to detect curl usage). Most $_SERVER[] variables are defined, but $argv[] array is not.
- $_SERVER[โHTTP_COOKIEโ] is undefined, but a lot of the rest of $_SERVER[] variables are as above.
- Lots of $_SERVER[] variables are undefined, with the exception of $_SERVER[โSCRIPT_FILENAMEโ] which may help you with Intranet application usage. You cannot enter $_GET[] arguments (on the command line (ie. you will get a syntax error if you try)) so these are undefined, but $argv[] array can be used in that awkward way shown, so that $argv[1] can be arranged to be a lot like $_SERVER[โQUERY_STRINGโ] (ie. the bits after ? in a URL).
So, maybe you have the one mode of use in mind, but you need to think a bit to support all the modes of use, if they are all to have a role with the PHP you write, because, despite these differences it is good to use $_SERVER[] and $_GET[] and $_POST[] and $_SESSION[] and $argv[] and whatever else in terms of variable information PHP offers in terms of attempting to write generic code.
Hope you get something out of todayโs discussion of our Mac OS X MAMP Apache local Web server environment example (hence the //localhost:8888/ you see in the URLs above), but please bear in mind that there would be other modes of use once you think of other client HTML element ways of accessing PHP (all a lot like the โweb browsingโ of above) or other possibilities within PHP code itself via PHP commands exec and file_get_contents the latter of which can be very useful during Intranet usage where there is no curl available, perhaps. Instead of curl you could use wget as another approach.
You may want to see a liverun (web browsing) here at the rjmprogramming.com.au domain or you may want to download the PHP programming source code you could call in_tra_ter_net
php as you require.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.