Sunday, February 6, 2011

PERL : CGI - common gateway interface

cd /etc/httpd/conf
grep cgi httpd.conf # Here ScriptAlias /cgi-bin/ "/var/www/cgi-bin"
http:..192.168.1.10/cgi-bi/helloworld.pl
cd /var/www/cgi-bin
nano helloworld.pl
#!/usr/bin/perl -w
use strict;
print "Content-type: text/html\n\n";
print "<h1>Hello World! - our first perl CGI script</h1>";
#end
chmod +x helloworld.pl
open a browser - http://192.168.1.10/cgi-bi/helloworld.pl


nano enviroment1.pl
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
print "<table>";
foreach (sort keys %ENV)
{
print "<tr><td>$_</td><td>$ENV{$_}</td></tr>"
}
print "</table>";#All the values are printed in 2 columns
#end


nano form1.pl
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
print "<html><title>Our first HTML Perl Form</title></head><body>";
print "<form method='post' action='action1.pl'>";#form methods: GET, POST
print "Name: <input='text' name='name' size='25'><br>";
print "E-Mail: <input='text' name='e-mail' size='25'><br>";
print "<input type='submit' value='Submit'>"
print "</form>";
print "</body></html>";
#end


nono action1.pl
#!/usr/bin/perl -w
use strict;
use cgi;
my $cgi = new CGI;
print $cgi->header();
print cgi->start_html("Action Page");
print $cgi->param('name'), "<br>";
print $cgi->param('email');
print $cgi->end_html();
#end
chmod u+x action1.pl
http://192.168.1.10/cgi-bi/helloworld.pl. After you enter the name and email the follwoing page appear http://192.168.1.10/cgi-bi/action1.pl. Here the name and email are displayed

No comments:

Post a Comment