Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Browser dependent CGI script failure?

by markmoon (Deacon)
on Apr 14, 2004 at 04:59 UTC ( #344925=perlquestion: print w/replies, xml ) Need Help??

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

I'm currently working on a script that replaces about 40 HTML files to generate a portfolio section of an older website. I thought I was nearing completion because it works (or at least does what I was waiving dead chickens for ;-). However, when I starting checking it with different browsers to see how badly the CSS gets mangled, I realized that some of the browsers are doing something strange with the parameters. Instead of passing my values back to the script they seem to be simply trying to pass them to the directory the script is in (cgi-bin).

For example Mozilla 1.5, Safari 1.0.2 and I.E. 5.2 all properly send the parameters back to the calling script. Opera 6.03 and Netscape 4.77 both omit the script name but contain the rest of the path info. These are all on Mac OS X. The parameters are in the form of ?preview=deck or ?section=deck&image=deck1.jpg. The server is Red Hat running Apache 2.0.40.

I've looked through Super Search, categorized Q&A and the CGI Tutorials but can't find a similar problem. I'm not sure if this is a browser problem or have I done something odd?

Here's the script, although I don't think I'm doing anything weird...(I hope the readmore tags work)

#!/usr/bin/perl -wT use strict; use CGI qw/:standard :html3/; use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; my $ROOT = "/path/to/files"; # figure out where to go if (param()){ ### Ho +uston we have input!!! ### if (param('preview')){ ### de +cide which section to preview my $section = param('preview'); create_links($section); }elsif (param('closeup')){ ### sh +ow closeup image ### my $section = param('closeup'); my $image = param('image'); my @closeup_info = ($section,$image); show_closeup(@closeup_info); }else{ die "I'm sorry, but that is not a valid selection.\n"; } }else{ ### script being called for first time -- show portfolio s +ections ### print_header(); portfolio_main(); the_end(); } #-------------------------------SUBROUTINES -------------------------- +-------------------------------------# # portfolio main displays the different portfolio section choices sub portfolio_main{ ### PRINT PORTFOLIO BANNER ### print div("<img src = \"/path/to/banner/portfol.gif\" class =\ +"banner\">"); print "\n"; my $class = "class = \"preview\""; print_nav(); ### PRINT MAIN PORTFOLIO SECTION PREVIEW IMAGES ### print div("<a href = \"?preview=deck\"> <img src = \"/path/to/ +images/deck/prevs/prevdeck7.jpg\" class =\"prev1\" id = $ print div("<a href = \"?preview=fence\"><img src = \"/path/to/ +images/fence/prevs/prevfence1.jpg\" class =\"prev1\" id = $ print div("<a href = \"?preview=bench\"><img src = \"/path/to/ +images/bench/prevs/prevbench1.jpg\" class =\"prev2\" id = $ print div("<a href = \"?preview=arb\"> <img src = \"/path/to/ +images/arb/prevs/prevarb3.jpg\" class =\"prev2\" id = $ } ### SHOW SELECTED SECTION PREVIEWS ### # $section which section (duh) i.e. decks, fences etc. # $path tells where to find preview image i.e. "\"/path/to/images/pre +vs/$section$num.jpg" sub create_links{ ### receives contents of param('preview') ex. +deck, fence, bench or arb ### my $section = shift; my $path = "/path/to/images/$section/prevs/"; my $dir_path = "$ROOT/path/to/images/$section/prevs/"; ### Open and read in the contents of the selected section's preview di +rectory ### opendir(DIR,$dir_path) or die "$!\n"; my @images; while (my $filename = readdir(DIR)) { push(@images,$filename) if ($filename =~ /$section\d+\.jpg/); } closedir(DIR); ### Create the page ### print_header(); print_banner($section); my $class = 1; my $id = 1; foreach my $image (@images){ my $preview = "\"$path$image\" class = \"prev$class\" id = \"p +$id\""; $image =~ s/prev//; print div("<a href = \"?closeup=$section&image=$image\"><img s +rc = $preview></a>"); print "\n"; $id++; if ($id > 4){ $class++; $id = 1; } } the_end(); } sub show_closeup{ ### receives $section and $image ### my $section = shift; my $image = shift; my $link_path = "/path/to/images/$section/"; print_header(); print_banner($section); print div("<img src = \"$link_path$image\" class = \"closeup\" +>"); print "\n"; the_end(); } sub print_nav { my $image_path = "/path/to/images"; print div({-class => 'buta'}, "<img src =\"$image_path/a1.gif +\">"),"\n"; print div({-class => 'buts'}, "<img src =\"$image_path/s1.gif +\">"),"\n"; print div({-class => 'butp'}, "<img src =\"$image_path/p1.gif +\">"),"\n"; print div({-class => 'butmid'},"<img src =\"$image_path/ +f\">"),"\n"; print div({-class => 'butr'}, "<img src =\"$image_path/r1.gif +\">"),"\n"; print div({-class => 'butm'}, "<img src =\"$image_path/m1.gif +\">"),"\n"; print div({-class => 'buty'}, "<img src =\"$image_path/y1.gif +\">"),"\n"; } sub print_header { print header(),"\n", start_html( -title => 'some site', -style => {-src => ' +.css'}),"\n", } sub print_banner { my $section = shift; print div("<img src = \"/path/to/images/$section/ban$section. +gif\" class = \"banner\">"); print "\n"; } sub print_footer{ print div("footer info"),"\n"; } sub the_end{ # print_footer(); print end_html; }


update: removed a line I forgot to comment out - doesn't affect the script though

Replies are listed 'Best First'.
Re: Browser dependent CGI script failure?
by matija (Priest) on Apr 14, 2004 at 05:20 UTC
    Your problem is here: print div("<a href = \"?closeup=$section&image=$image\">.

    You are using a relative URL of a form that is not strictly standard, supplying only the parameters of the script, without the script name (or the directory, etc). Some browsers fill out the script name automatically, some don't.

    I strongly suggest that you use either self_url or url() methods (provided by to determine the URL of your script and explicitly use them in links.

    That way, you will retain the ability to move the script to other machines/directories and have it work automatically, and avoid the browser dependent interpretation of the relative URL RFCs.

      Some browsers fill out the script name automatically, some don't.
      Like what browser? All browsers should support this. It's a standard relative uri, perfectly legal. See
        RFCs are not law - and ultimately, it is up to the browser manufacturers, whether they adhere to them or not.
        (if browsers all followed the standards - webdesigners wouldn't be having as many compatibility discussions.)
        It is open to interpretation if it is a legal relative URI. The relativeURI grammar in RFC 2396 defines that the relative path must have at least a single character before the query string. However, the examples include '?y' as a relative URL. RFC 1808 allows just a query string and specified the resolution rules for it.

      Thank you very much. I wasn't aware of that, but I promise not to forget in the future.


      @a = ("a".."z"," ","-","\n");foreach $b ( 12,0,17,10,24,12,14,14,13,26,8,18,26,0,26, 22,0,13,13,0,27,1,4,26,15,4,17,11,26,7,0, 2,10,4,17) {print $a[$b]};print $a[28];
      you just kept me from doing this mistake, too. wasn't aware of it at all...

      and thanks to markmoon for asking this in here...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://344925]
Approved by kvale
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2021-07-31 15:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found