Nothing is getting displayed on browser while using open statement

Hi,

I have the below script which is working fine on cmd line, but nothing gets displayed on browser. There is no permission issue. If i remove the open statement and print some other data. Its working fine on browser. Kindly help.

#!c:/perl/bin/perl
use Win32::ODBC;
use CGI ':standard';
print "Content-type: text/html\n\n";
print '<HTML>';
print '<HEAD>';
print '<link rel="stylesheet" type="text/css" href="../user_tabs.css" />';
print '<TITLE>Local User Accounts</TITLE>';
print '</HEAD>';
print '<BODY>';
print '<TABLE BORDER="2" FRAME="BOX" RULES="ALL">';
print '<TR><TH><FONT FACE ="ARIAL" SIZE="2">User ID</TH><TH><FONT FACE ="ARIAL" SIZE="2">Hosts</TH></TR>';
 
my $line;
my @line2;
my @line3;
my %hosthash;
my @templine;
my @acc_user;
my @host_name;
my $count=0;
my $count1=0;
my $check;
my $check2;
my @hostname;
my @filedata;
my $ldapuser;

open(FILE,"somefile.txt") or die("Unable to open file");
foreach $line3 (<FILE>) 
{
 chomp($line3);
 push(@filedata,$line3);  
}
close(FILE);

foreach $line (@filedata) 
{
     chomp($line); 
 @templine=split(/,/ ,$line);
 $count=0;
 $hosthash{$templine[0]} = [] unless exists $hosthash{$templine[0]};  
 foreach $check(@acc_user)
 {
  if($check eq $templine[0])
  {
   $count=1;
   last;
  }
  
 }
 if($count==0)
 {
  push(@acc_user,$templine[0]);
 }
 
 foreach $check2(@acc_user)
 {
  if($check2 eq $templine[0])
  {
        
   push @{ $hosthash{$check2} }, $templine[1];
   #print "Plesae wait Loading Data \n";
   
  }
  
 }
 
 
 
}
foreach $ldapuser(@acc_user)
{
 
 
 @hostname = @{$hosthash{$ldapuser}}; 
 print "<TR><TH><FONT FACE =ARIAL SIZE=2>"."$ldapuser"."</TH><TH><FONT FACE =ARIAL SIZE=2>"."@hostname"."</TH></TR>";
 
 
}

print '</TABLE>';
print '</BODY>';
print '</HTML>';

---------- Post updated at 02:06 PM ---------- Previous update was at 02:04 PM ----------

Only

User IDHosts is getting displayed on browser

Use the absolute path for the file. Your current directory is probably different when called by CGI.

open(FILE,"c:/path/to/somefile.txt") or die("Unable to open file");

or maybe c:\\path\\to... depending on how windows paths in Perl work.

Thanks, but it is still not working :frowning:

Can you get a minimal perl script to work?

#!c:/perl/bin/perl

print "Content-type: text/plain\n";
print "\n";

print "This is a message\n";

---------- Post updated at 12:39 PM ---------- Previous update was at 12:36 PM ----------

Also, you can try open(STDERR, ">&STDOUT"); to redirect error messages out of standard error and into stdout where they should appear on your browser.

Yes. If i remove the open statement and pring some thing else. Its working.

For example the below script.

#!c:/perl/bin/perl
use Win32::ODBC;
use CGI ':standard';
print "Content-type: text/html\n\n";
 
print '<HTML>';
print '<HEAD>';
print '<TITLE>Local User Accounts</TITLE>';
print '</HEAD>';
print '<BODY>';
print '<TABLE BORDER="2" FRAME="BOX" RULES="ALL">';
print '<TR><TH><FONT FACE ="ARIAL" SIZE="2">Number</TH><TH><FONT FACE ="ARIAL" SIZE="2">Factorial</TH></TR>';

my $i;
my $j;
my $prod;
for($i=1;$i<=20;$i++)
{
 $prod=1;
 for($j=i;$j>=1;$j++)
 {
  $prod=$prod*$j;
 }
 print '<TR><TH><FONT FACE ="ARIAL" SIZE="2">'."$i".'</TH><TH><FONT FACE ="ARIAL" SIZE="2">'."$prod".'</TH></TR>';
}
print '</TABLE>';
print '</BODY>';
print '</HTML>';

prints

NumberFactorial112131415161718191101111121131141151161171181191201

on the browser

try open(STDERR, ">&STDOUT"); after you import your modules to redirect error messages out of standard error and into stdout where they should appear on your browser.

No luck

It's annoying that even redirecting STDERR doesn't get you the error messages! It's probably trying to tell you what you're doing wrong but the message floats off into hyperspace...

I suspect it's not actually able to open the file due to either some difference in path or some difference in permissions. The perl scripts may be getting run under a different user than you. Try putting your datafile under something like C:\Documents and Settings\All Users\Documents\ where permissions are more relaxed. Check the logs for your webserver, see if it's logging the error output anywhere.

How's the output look running from the command prompt?

Maybe try added a newline at the end. Also, I know \n\n works but I believe \r\n\r\n to be more standard and was required for me on one webserver (though my CGI was in bash).

First Print

print "Content-type: text/html\r\n\r\n";

Last print

print "</HTML>\n"

Any difference?

Considering this is Windows it's running on, I doubt carriage returns are particularly lacking.

CGI Error

The specified CGI application misbehaved by not returning a complete set of HTTP headers.

Its givimg error now

On command prompt the out put is as follows

jamoi</TH><TH
 SIZE=2>zope<
FACE =ARIAL S
></BODY></HTM

What webserver is this? Maybe

print "Status: 200 OK\nContent-type: text/html\n\n";

or is it "HTTP 200 OK" ? :slight_smile: Haven't needed to use this for awhile

Edit: or is it Microsoft Support

Now again blank page is displayed while on cmd line

<HTML>
<HEAD>
<TITLE>Local User Accounts</TITLE>
</HEAD>
<BODY>
<TABLE BORDER=2>
<TR><TH>User ID</TH><TH>Hosts</TH></TR>
<TR><TH>aabano</TH><TH>nis1m3</TH></TR>
<TR><TH>aabukhat</TH><TH>adm7m3 nis1m3</TH></TR>
<TR><TH>aaditi</TH><TH>nis1m3</TH></TR>
<TR><TH>aalekova</TH><TH>dwdev1w88m7</TH></TR>
<TR><TH>aambani</TH><TH>nis1m3</TH></TR>
</TABLE>
</BODY>
</HTML>

is displayed

After doing ...? and please use code tags.

I was not closing the tags correctly but its displaying the data onCMD but still not on browser

What if you 'view source' in the browser?

Did you try moving the file to where I suggested?

Okay let me list the problem once again. Below is my code.

#!c:/perl/bin/perl
use Win32::ODBC;
use CGI ':standard';
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>Local User Accounts</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
print "<TABLE BORDER=2>\n";
print "<TR><TH>User ID</TH><TH>Hosts</TH></TR>\n";
 
my $line;
my @line2;
my @line3;
my %hosthash;
my @templine;
my @acc_user;
my @host_name;
my $count=0;
my $count1=0;
my $check;
my $check2;
my @hostname;
my @filedata;
my $ldapuser;

open(FILE,"Somefile.txt") or die("Unable to open file");
foreach $line3 (<FILE>) 
{
 chomp($line3);
 push(@filedata,$line3);  
}
close(FILE);
open(STDERR, ">&STDOUT");
foreach $line (@filedata) 
{
     chomp($line); 
 @templine=split(/,/ ,$line);
 $count=0;
 $hosthash{$templine[0]} = [] unless exists $hosthash{$templine[0]};  
 foreach $check(@acc_user)
 {
  if($check eq $templine[0])
  {
   $count=1;
   last;
  }
  
 }
 if($count==0)
 {
  push(@acc_user,$templine[0]);
 }
 
 foreach $check2(@acc_user)
 {
  if($check2 eq $templine[0])
  {
        
   push @{ $hosthash{$check2} }, $templine[1];
   #print "Plesae wait Loading Data \n";
   
  }
  
 }
 
 
 
}
foreach $ldapuser(@acc_user)
{
 
 
 @hostname = @{$hosthash{$ldapuser}}; 
 print "<TR><TH>"."$ldapuser"."</TH><TH>"."@hostname"."</TH></TR>\n";
 
 
}

print "</TABLE>\n";
print "</BODY>\n";
print "</HTML>\n";

It gives me NO OUTPUT on broser other than the headings
User ID Hosts

But on the cmd prompt it gives the follwing output

<HTML>
<HEAD>
<TITLE>Local User Accounts</TITLE>
</HEAD>
<BODY>
<TABLE BORDER=2>
<TR><TH>User ID</TH><TH>Hosts</TH></TR>
<TR><TH>aabano</TH><TH>nis1m3</TH></TR>
<TR><TH>aabukhat</TH><TH>adm7m3 nis1m3</TH></TR>
<TR><TH>aaditi</TH><TH>nis1m3</TH></TR>
<TR><TH>aalekova</TH><TH>dwdev1w88m7</TH></TR>
<TR><TH>aambani</TH><TH>nis1m3</TH></TR>
</TABLE>
</BODY>
</HTML>

Kindly help.

The files have correct permission. One more thing if i remove the pen statement and pront some other things its displayed correctly on the browser

What makes you think the file has correct permissions when it never works from anything but your own user? Did you actually try moving it to where I suggested?

open(STDERR, ">&STDOUT");

This should be BEFORE the other open, so you can see its error message.

1 Like

I have accomplished the task by using a seprate html uploader and then parsing the file using cgi.

Thanks for the responses

Let me have a try!

---------- Post updated at 10:42 PM ---------- Previous update was at 09:28 PM ----------

Too complex!!