I have a guestbook.htm file in the directory named as Chrome (/home/chankey/Desktop/Chrome/guestbook.htm) whose content is given below
<html>
<head>
<title>Guestbook</title>
</head>
<body>
<form action="/home/chankey/Desktop/Chrome/guestbook.pl" method="get">
<table>
<tr><td>Name</td><td><input name="name" type="text" value=""></td></tr>
<tr><td>E-Mail</td><td><input name="email" type="text" value=""></td></tr>
<tr><td>Location</td><td><input name="loc" type="text" value=""></td></tr>
<tr><td>Comments</td><td>
<TEXTAREA name="comments" rows="10" cols="32"></TEXTAREA></td></tr>
</table><br><br>
<input type="submit" value="Add Entry">
</form>
</body>
</html>
In the same Chrome directory I have one file "guestbook.pl" whose content is
#!/usr/bin/perl
my $query_string = "";
#Get the input
if ($ENV{REQUEST_METHOD} eq 'POST') {
read(STDIN, $query_string, $ENV{CONTENT_LENGTH});
} else {
$query_string = $ENV{QUERY_STRING};
}
##### We will remove this
print "Content-Type: text/html\n\n";
print "Query String is \n<br> $query_string";
##### We will remove this
When I am executing the guestbook.htm file there appears a form, when I fill the data and click on "Add Entry" button a new page opens where the complete script appears.
i.e. the script "guestbook.pl" is not executing. May I know the reason behind this? Why the script is not executing? (I have already given executing permission to this file).
In the httpd.conf file I have added
AddHandler cgi-script cgi pl
<Directory /home/chankey/Desktop/Chrome/>
Options +ExecCGI
</Directory>
Still it is not executing. Let me know the reason.
When you access a file locally (over the file:// type URL in your browser), it's not running from a web server, so:
There is no CGI environment
The httpd.conf/.htaccess files have no effect
A few ways to deal with this:
Create a folder named public_html in your home. Your web server likely has a setting to map http://localhost/~chankey/ to /home/chankey/public_html. (On MacOSX, the preferred name is Sites, instead, I believe.) On an SELinux system, you'll have to specifically grant permission for Apache to use this method.
For the specific case of Perl scripts using the standard CGI package, you can also run them manually from a terminal shell, and redirect their output to a temporary file (e.g. >/tmp/output.html), which you can then access
Migrate your development workspace into your web server's own directory structure, typically /var/www/html/…
PS / unrelated: I strongly recommend that, if you plan to put this on the Internet, you should probably use CGI; use strict; and have the tainting mode enabled #!/usr/bin/perl -WT …
Related
I have an HTML page in which I run a Perl script to get the value from HTML and writing it onto a file.
The problem I'm facing is that whenever I run the Perl script from the terminal as a root, the HTML page gets updated and runs fine, however if I try the same thing from HTML (using the Firefox browser) it fails. Perhaps because of a permission issue.
The following is an extract from my HTML page:
<div>
<form id="QA1_Insert" action="http://localhost/cgi-bin/person.pl" METHOD="POST" >
<table>
<tr>
<td class="columnLabel"> Name</td>
<td><input type="text" name="Name"></td>
<td class="columnLabel">Address:</td>
<td><input type="text" name="Description"></td>
</tr>
</table>
<input type="submit" value="submit"></form>
</div>
And here is my person.pl:
#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use warnings;
use IPC::System::Simple qw(system capture);
use DBI;
use Cwd qw(chdir);
use FileHandle;
use Fcntl;
#sysopen (FILE ,"/root/info/person.txt",O_RDWR|O_CREAT,0755);
open (my $file,"<","/root/info/person.txt");
chmod 755,$file;
print FILE "<html><head></head><body><p>sakshi</p></body></html>";
close (FILE);
print
header(),
start_html(
-title => 'Command',
-text => '#520063'
);
print "Hello ";
print end_html();
I have tried creating it with sysopen, which doesn't work due to permission errors. I've also tried chmod. However, neither are working. Any suggestions on how to solve it would be appreciated.
Your web server does not run as the root user. That is correct. It should not do that. Since your file is in root's home directory, the web server user cannot access it. The chmod command will fail as well as the open.
Put your output file in a directory that the web server can access (and should access, unlike root's stuff) and it will work.
Note that you have a mix of lexical and global filehandles there. Decide which one you want. (You want $fh!) Also you are opening the file for reading with < and then you try printing to it. That will also not work.
You should check the return value of the open call. For example, you can say:
open my $fh, '<', $filename or die $!;
Check your web server's log file to see the errors. You are using strict and warnings, which is good, but you do not profit from the error messages if you do not look at them. It's probably saying 500 Internal Server Error. Your web server log has more information.
As an alternative, use CGI::Carp qw(fatalsToBrowser); will redirect them to the browser. Do not use this in production though.
Finally, note that CGI has been removed from the Perl core recently and that in recent releases of the CGI module on CPAN the HTML creation functions have been deprecated. They should not be used in new code.
I am new to CGI. I wrote a very complex module around perl, cgi, html and javascript. And it runs perfectly on command line. But I am not able to run it via browser. I went on debugging line by line from bottom of my script, only to find that the issue was around the HTML::TableExtract module itself. So to make it simple -
# perl -c test.cgi
test.cgi syntax OK
test.cgi
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TableExtract;
print "Content-type: text/html\n\n";
print <<htmlcode;
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
htmlcode
This works perfectly on command line. But if I run it via Browser it just doesn't work. However, if I remove "use HTML::TableExtract" it works perfectly fine again - even in browser. The permissions are correctly set to 755.
Can someone please help me understand, what I am missing? And how can I fire it up from browser. How can I go about debugging this - My browser redirects me to page not found if I mention use HTML::TableExtract.
Note: Would like to point out one thing, this may be related to setting up some environment variable around HTML::TableExtract. When I first installed the module, there was an error which my hosting administrator helped resolve.
# ./test.cgi
Content-type: text/html
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
Debugging perl CGI scripts
Add use CGI::Carp; to you script to make it report bugs to browser
[It produces http reply over STDOUT instead of default text over STDERR]
Most likely scripts executed from command line and from web server search for modules in different places e.g. due to different settings of PERL5LIB or PERLLIB environment variables.
WARNING
CGI::Carp may be a security threat in "production" version of cgi scripts.
It may provide crucial information to (potential) hackers.
I have a file in.txt with some text.
I am using IIS 7 local server on Windows. and isapiModule is working fine for perl.
When I run this perl code via browser it is printing to browser just print line (not data) but not writing to file out.txt and just opening the empty file out.txt (i.e. unable to write via browser).
My wwwroot folder has full control access for all users.
html code:
<html>
<body>
<form name="form1" action="perl.pl" method="post"
enctype="multipart/form-data">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
perl.pl:
open(file,"<in.txt");
#x=<file>;
open(OUT,">out.txt");
print"print to browser...";
print OUT"#x\n";#for printing to file
close file;
close OUT;
What may be the reason and solution.
UPDATE: program is writing when manually run via command drive.
Have you tried something like this?
#!/usr/bin/perl -w
use strict;
my $in = 'in.txt';
open my $file, '<', $in or die "Can't open $in\n";
my $outfile = 'out.txt';
open my $outprint, '>', $outfile or die "Can't write to $outfile\n";
print "print to browser…\n";
while (<$file>) {
chomp;
print $outprint "$_\n";
}
Can someone please explain to me the very basics of getting perl to work on a server. Do I need a module on the server? If so where does it go? What do I name my files and where do they go?
From my understanding you need a module and it goes in the cgi-bin. I can't get a clear answer whether I name the file .pl or .cgi and when I put it in the cgi-bin I am getting a server error. I also have my permissions set to 777, so that shouldn't be the problem.
Please help! I just want to understand the how to get the very basic program working such as the one below. Thanks in advance!
#!/usr/bin/perl
require("cgi-lib.pl");
print &PrintHeader;
print "<html>";
print "<head><title>Hello world!</title></head>";
print "<body>";
print "<p>Hello world!</p>";
print "</body>";
print "</html>";
The latest version of cgi-lib.pl is dated 1999 and is very out of date. I suggest you use the CGI library instead, which is almost bound to be installed already on your server and is kept up to date (most recently on August 16 2012)
Your program should look like this:
#!/usr/bin/perl --
use strict;
use warnings;
use CGI ':standard';
print header;
print <<END;
<html>
<head><title>Hello world!</title></head>
<body>
<p>Hello world!</p>
</body>
</html>
END
Also note that you can run the program from the command line to see if it compiles and what output it generates. Once you have it working there you can move it to the server
The problem you are facing is you probably edited the source file on a Windows machine, which inserts a CR character before each linefeed. Make sure your code does not include any CRs or change the first line to:
#!/usr/bin/perl --
(that's two dashes and a space at the end of the line)
I concur with Dave Cross' comment, you need to tell your school that they are teaching you incorrectly. We in the Perl land are trying to get people to stop using the CGI module, and you are using its predecessor!
Here is a hello world app in a modern Perl framework, Mojolicious:
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => 'hello';
app->start;
__DATA__
## hello.html.ep
<html>
<head><title>Hello world!</title></head>
<body>
<p>Hello world!</p>
</body>
</html>
which you place into a file, (lets say test.pl). Install Mojolicious by running:
curl get.mojolicio.us | sh
and then start your app by running
perl test.pl daemon
Now you can visit http://localhost:3000 in your browser to see the result, no Apache, no cgi-bin needed!
A more fun example takes an "argument" with a default:
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/:name' => { name => 'world' } => 'hello';
app->start;
__DATA__
## hello.html.ep
<html>
<head><title>Hello <%= $name %>!</title></head>
<body>
<p>Hello <%= $name %>!</p>
</body>
</html>
Run this and try visiting http://localhost:3000/SilverNightaFall and see what it does!
This process of interpolating dynamic values into your HTML is known as templating, and it is much preferred these days (vs generating your entire HTML on each request).
I am new to both perl and apache servers. I'm trying to get a basic Hello World going for a CGI script. Here is the code for my hello world CGI file:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<H1>Hello World</H1>\n";
When I execute the CGI script on the command line, ./hello.cgi, it works, but when I open hello.cgi in my browser, it just displays the text of the cgi file.
When I look at my error_log in /var/log/apache2/error_log I can see that mod_perl is running:
Apache/2.2.21 (Unix) DAV/2 mod_perl/2.0.5 Perl/v5.12.3 configured -- resuming normal operations
but when I run the following perl program, it appears that I don't have the "MOD_PERL" env variable:
if (exists $ENV{"MOD_PERL"}) {
print "YAY!\n";
}
else{
print"mod_perl is not working\n";
}
By the way, my hello.cgi file and the perl script above are located in /Users/myusername/Sites/
Could someone help me configure mod_perl properly so that I can properly view hello.cgi in my browser? I've been reading the mod_perl documentation and searching forums for many, many hours with no avail. Thanks in advance
#SebastianStumpf I got your first example to work, but I still can't seem to get a mod_perl script going. I've been reading through the documentation, but I haven't been able to figure it out. Thanks for your help, by the way. I'm very grateful
UPDATE: i believe I got it working! Thanks for the help!
If you are using the stock Apache/Perl of Mac OS X Lion and don't need mod_perl then you don't have to configure anything. Just create your file with the .cgi extension in /Library/WebServer/CGI-Executables and adjust the permissions via sudo chmod 755 file.cgi. The script will be executed via CGI (not mod_perl). I tried this and it worked just fine:
$ sudo -s
# cat - > /Library/WebServer/CGI-Executables/test.cgi
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw/:standard/;
use Data::Dumper;
print header, start_html, h1('works'), end_html;
^D
# sudo chmod 755 /Library/WebServer/CGI-Executables/test.cgi
# exit
Testing it:
$ curl -i http://localhost/cgi-bin/test.cgi
HTTP/1.1 200 OK
Date: Mon, 11 Jun 2012 22:29:24 GMT
Server: Apache/2.2.21 (Unix) DAV/2
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>works</h1>
</body>
</html>
If you need mod_perl, then have a look at the documentation. The configuration part of the introduction should be all you need to get mod_perl running.
EDIT:
I've added the following lines to /etc/apache2/httpd.conf, restarted the web server and mod_perl works:
LoadModule perl_module libexec/apache2/mod_perl.so
Alias /perl /Library/WebServer/perl
<Location /perl>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
Options ExecCGI
PerlSendHeader On
Order allow,deny
Allow from all
</Location>
If the script is saved in /Library/WebServer/perl/ you can see that all mod_perl headers are available now, but I'd rather setup a private installation of Apache/mod_perl. MacPorts makes this a lot easier...