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.
Related
I have an issue with downloading a page,
my $url='http://www.ncbi.nlm.nih.gov/nuccore?linkname=pcassay_nucleotide&from_aid=504934,1806,1805,1674';
I can browse following with a browser but when I run bash command in perl or linux shell,
GET $url >OUTPUT1; # Even it does not write anything to file "OUPUT1"
When I try wget, It downloads but not correct ,I mean with --> <title>Error - Nucleotide - NCBI</title>. I want the page with items , but it returns me a page without items.
my $html = qx{wget --quiet --output-document=OUTPUT1 $url};
**Note: I noticed a few minutes ago, url is ok with Mozilla firefox, but it can not be browsed via google chrome. it is weird, probably my issue related with this too. Any idea?
Code from link:
my $url='http://www.ncbi.nlm.nih.gov/nuccore?linkname=pcassay_nucleotide&from_aid=504934,1806,1805,1674';
my $html = qx{wget --quiet --output-document=OUTPUT11 $url};
# wget get something, but it does not get items, it gets what I get via google chrome
`GET $url2 >OUTPUT11`; # it does not write anything to file,
OK, given your code - the problem is almost certainly one of interpolation. Because the & in your URL is going to be interpreted by the shell you're spawning as 'background this process'.
That's almost certainly not what you want. Why not just use LWP natively?
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
my $url='http://www.ncbi.nlm.nih.gov/nuccore?linkname=pcassay_nucleotide&from_aid=504934,1806,1805,1674';
my $content = get $url;
print $content;
open ( my $output_fh, '>', 'output.html' ) or die $!;
print {$output_fh} $content;
close ( $output_fh );
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";
}
I am making a .cgi file which prints all values from database table on webpage in a table format.The problem is that when I run the file on putty terminal emulator it works fine but when I try to run the file on my browser I get an error message "file not found" even though T typed the correct location of the file on the server.
I can't understand what am I doing wrong? I set my file's permission to chmod 755 * using putty but it's still not working.Is this a problem of file permissions or table structure is wrong for running on browser something else?
Please help...
people.CGI File
#!/usr/bin/perl
use CGI;
use DBI;
use strict;
#use warnings;
#use diagnostics;
print "Content-type:text/html\r\n\r\n";
#$q = CGI->new;
#print $q->header;
my $dsn = "DBI:mysql:Demo:localhost"; # Data source name
my $username = "mint"; # User name
my $password = "MINT123"; # Password
my $dbh;
my $sth; # Database and statement handles
$dbh = DBI->connect($dsn, $username, $password);
$sth = $dbh->prepare("SELECT * from people");
$sth->execute();
print "<h1>ganesh</h1>";
print "<table >
<tr>
<th>ID</th>
<th>Name of People Involved</th>
<th>Position</th>
<th>Roles(A user can have multiple roles)</th>
<th>Notes</th>
</tr>";
while( my $href = $sth->fetchrow_hashref )
{
print "<tr>";
print "<td>$$href{'id'}</td>";
print "<td>$$href{'name'}</td>";
print "<td>$$href{'pos'}</td>";
print "<td>$$href{'role'}</td>";
print "<td>$$href{'notes'}</td>";
#print "<td><input type='text' value=\"$$href{'display_name'}\" id =\"dis-$$href{'windows_id'}\" readonly> </td>";
#print "<td><input type='text' value=\"$$href{'email_id'}\" readonly> </td>";
print "</tr>";
}
print "</table>";
$sth->finish();
$dbh->disconnect();
Database Table structure...
Table data...
Output when i run the file in putty...
Message when i try running the file on my browser..
The two answers you have received previously are complete nonsense. You don't need to to use a CGI object in order to run a CGI program. Of course, it makes it easier, but it's not necessary.
The only part of the CGI protocol that your program needs to handle is the Content-Type header. And you're doing that with your print line.
No, your problem is somewhere else completely. But, unfortunately, it's somewhere where we can be of very little help without knowing a lot more. You're getting a file not found error because the web server can't find your code. In other words, the address that you're typing into your browser (128.9.45.170/~pankaj.yadav/Test/cgi/people.cgi) doesn't match a filename on your web server.
This all comes down to how your web server is configured. How are web addresses mapped onto file paths? We don't know. Only your web server administrator will know the answer for sure.
You might get a clue if you look at the web server error log. You'll see a file not found error in the log which will (hopefully) contain the actual file path that the web server is trying to find. And that might help you work out where you should put your CGI program.
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 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 …