Ursula has asked for the wisdom of the Perl Monks concerning the following question:

Using Netscape browser when an invalid number in entered in the textbox, the program is returning the html instructions instead of the webpage with an error message. Using Internet Explorer the program works fine. Please tell me why this is happening and how I can fix it.

Program:

use cgi_utils; use Navigation_bar; use Sspec; use CGI qw(:all); use Far; use cgi_tables; use Fdi_display_file; use Odi_keyed_file; use cgi_site_params; use Farrsh; #use strict; my $total_file_count = 0; my $image_file_count = 0; my @all_files = (); my @image_files = (); my $total_size = 0; my $image_size = 0; my $redir; my $progname = "estimate_edtup_size.cgi"; my $lbxnum; my @lbxnum; my $query = new CGI; my $podid; my @all_dirs; my $all_dirs; my $outfile; my $event; my $asterisk = "*"; my $aspec; my $this_dir; my $env = &get_environment; my $dirname = "/usr/$env/data/editspecs/acct"; my $date; my $sspec = new Sspec; my @datadir; my $siteCode = $sspec->get_site; my $host = get_host; my $domain = get_domain; my $runhost = "ipm0"; my $bpath = "html/custom"; my $nav = new Navigation_bar; $nav->add ( gif => "Main_Menu.gif", href => path_to_script ("main_menu.cgi",$host), desc => "Return to Main Menu"); if(param('redir_to_host')) {&redir_to_host;} if (defined($lbxnum=param('lbxnum'))) { &header1; &getfiles; &footer; } if (!defined($lbxnum=param('lbxnum'))) { &header1; &initask; &footer; } ########################################################## sub header1 { print header, + start_html('-title'=> 'Edtup Size Estimate'), $nav->display, br; print center (h2("Edtup Size Estimate")); br; } ######################################################### sub header2{ br; print center (h3("Size Estimate Results")); } ########################################################### sub footer { print br,hr,"\n", center(a({'-href'=>"http://$runhost.$domain/usr/$env/$bpath/cus +tom_menu.cgi", '-onmouseover'=>"self.status='Return to Custom Menu'; re +turn true;"}, "Return to Custom Menu"), "|", a ({'-href'=>"http://$runhost.$domain/usr/$env/$bpath/$prog +name", '-onmouseover'=>"self.status='Return to Edtup Size Estim +ate'; return true;"}, "Edtup Size Estimate"),"\n"); } ########################################################## sub initask { print center(start_form('-method'=>"POST",'-action'=>"http://$host. +$domain/usr/$env/$bpath/$progname"), "Enter a valid lockbox number:", "\n", textfield('-name'=>"lbxnum",'-size'=>6, '-maxlength'=>6), "\n", br, br, hidden('-name'=>"redir_to_host", '-value'=>"redir_to_host"), "\n", submit('-value'=>"Get Files", '-name'=>"Submit"), "\n", end_form, "\n"); } ########################################################## sub getfiles { $lbxnum = param('lbxnum'); unless($lbxnum=~/\d{6}/) { print center (h2("An error occurred, the lockbox number $lbxnum is +invalid")); return; } @all_dirs = glob("/usr/$env/data/lb/$lbxnum/rd/????????/outputs/w?"); my $no_webs = @all_dirs; if ($no_webs == "0") { print center (h2("Verify Lockbox number $lbxnum, there are no Image T +ransmissions or Lockbox number does not exist")); return; } $outfile = "$lbxnum.csv"; open (OUT, ">/usr/$env/log/$outfile"); # || die "Couldn't open output + file $outfile"; print OUT "Lockbox,Date,Event,Image Files,Total Files,Image Size in M +B, Total Size in MB\n"; foreach my $this_dir (@all_dirs) { + my ($date, $event) = (split(/\//,$this_dir))[7,9]; $total_file_count = 0; $image_file_count = 0; @all_files = (); @image_files = (); $total_size = 0; $image_size = 0; read_dir_recursive($this_dir); # Convert from bytes to MB $image_size /= 1048576; $total_size /= 1048576; print "Lbx=$lbxnum....Date=$date....Event=$event....Image_Files=$image +_file_count....Total_Files=$total_file_count....Image_Size in MB=$image_size....Total_Size in MB=$total_size\n", br,br; #print OUT "Lbx=$lbxnum,Date=$date,Event=$event,Image_Files=$image_fil +e_count,Total_Files=$total_file_count,Image_Size in MB=$im age_size,Total_Size in MB=$total_size\n"; } close(OUT); } ############################################################# sub redir_to_host { $lbxnum = param ('lbxnum'); if ($lbxnum=~/\d{6}/) #change to check for 6 digits { $aspec = new Odi_file ("$dirname", "$siteCode-$lbxnum"); unless ($aspec->{read_successfully}) { print center (h2("the lockbox number $lbxnum does not exist")); return; } $podid = $aspec->{info}{"PodID"}; } else { print center (h2("the lockbox number $lbxnum is invalid")); return; } $redir = "http://oos0$podid.$domain/usr/$env/$bpath/$progname?getfile +s=g&lbxnum=$lbxnum"; print CGI->redirect(-uri=>$redir); } ################################################################### # Sub to read a directory recursively and update variables ################################################################## sub read_dir_recursive { my $dir = shift; opendir( WEBDIR, $dir); my @files = grep !/^\.\.?$/, readdir(WEBDIR); closedir(WEBDIR); foreach (@files) { next if (-l "$dir/$_"); if (-f "$dir/$_") { my $size = (stat("$dir/$_"))[7]; #print "Web Files $_ $size",br; push(@all_files, "$dir/$_"); $total_file_count++; $total_size += $size; if (/jpg$|gif$/) { $image_file_count++; push(@image_files, "$dir/$_"); $image_size += $size; } } if (-d "$dir/$_") { read_dir_recursive("$dir/$_"); } } }

Edit by tye to add CODE and READMORE tags

Replies are listed 'Best First'.
Re: Netscape returns script source on error, IE works
by tadman (Prior) on Nov 22, 2002 at 17:15 UTC
    I hope you realise that commenting out use strict is not the best way to solve problems.

    Generally what causes the Netscape versus IE problem is that you're not returning the correct MIME-Type header. In your code here, it would seem that you're careful to output it, but it's not entirely clear. There is a chance that the MIME type is being set to "text/plain" instead of "text/html".

    If you create a CGI object, you should use it. If you're importing :all of the methods, you should use them. Mixing and matching is not really a good idea, as you've done here. I'd recommend the object way, since it doesn't require importing functions that can collide with your own. For example:
    print $query->header("text/html");
      Specifically, redir_to_host returns content without or before the proper headers.
Re: Netscape returns script source on error, IE works
by fruiture (Curate) on Nov 22, 2002 at 17:22 UTC

    That's probably because IE is not a Browser. Make it work in Netscape and you can be quite sure it is really OK.

    So what's the _relevant_ code? I really don't want to look through a script and find such a bug, when that script has strict disabled, probably because it won't compile with strict. (?) That procedural style with only globals makes it very hard for someone to get what's happening.

    It's all about the HTTP Headers: IE ignores HTTP, it will do what it likes to do. Netscape doesn't ignore them, they probably tell Netscape to show plain text. Check the complete output of your script to make sure what it's doing.

    --
    http://fruiture.de