CGI Script - Modify image handler to include hyperlink

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smarsh
    New Member
    • Jul 2007
    • 18

    CGI Script - Modify image handler to include hyperlink

    Hi,

    Someone put together a small perl script for me that uses day and time to determine which of two images to display (open.gif or closed.gif). I need to modify the script to also include a hyperlink to open.html or closed.html so that if the open image is displayed it puts the open.html link on the image and the closed.html link on the closed image. The corresponding web page that loads the image is ultra basic, with a link to the .pl script as the image tag.

    Can anyone help a real perl novice (me!) with this one?

    Thanks guys!


    [CODE=perl]#!/usr/bin/perl
    use CGI::Carp qw/fatalsToBrowser/;

    use strict;
    use warnings;

    # path to folder where images are
    # assumes an image named open.gif and closed.gif
    my $path = '/home/fireplug/public_html/images/';

    # time in military format 0-23
    # first number is opening time
    # second number is closing time
    # 24-24 indicates always closed
    # server is central standard time so time is adjusted minus 1 hour
    my %schedule = (
    Sun => [9,11],
    Mon => [10,11],
    Tue => [10,11],
    Wed => [10,11],
    Thu => [10,11],
    Fri => [10,11],
    Sat => [24,24],
    );

    # default value
    my $image = 'closed.gif';

    # get the day and hour
    my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
    $hour = (split(/:/,$hour))[0];

    # check the day and hour
    if ($schedule{$day }[0] <= $hour && $schedule{$day}[1] > $hour) {
    $image = 'open.gif';
    }

    #print appropriate MIME type header
    print "Content-type: image/gif\n\n";

    open(FH,"$path$ image") or die "$!";
    binmode(FH);
    print while (<FH>);
    close(FH);
    exit; [/CODE]
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    What you want is not possible.

    This script is a handler that serves an image depending on the current time. This script is intended to be referenced inside the src value of an image tag. There is no way to effect the enclosing html that refers to this "image" handler.

    - Miller

    Comment

    • smarsh
      New Member
      • Jul 2007
      • 18

      #3
      Hmmm... does that mean an entirely new script written in a different way can do the job or that a CGI script can't do it at all? And... can a SSI be used in conjunction with the script to achieve what I need; something like redirecting based on the open or closed image? CGI + javascript?

      Thanks!

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        The solution must be found in the enclosing html, not the image handler. I don't know what is generating the html that is using this image handler, or if it's just static html. But regardless of what is currently generating it, if you want dynamic html, then you're going to have to program it.

        - Miller

        Comment

        • smarsh
          New Member
          • Jul 2007
          • 18

          #5
          Thanks Miller.

          Currently it is a static html page(s). All the "intelligen ce" is in the CGI where the time and day are gathered and the appropriate image selected. The html is:
          Code:
          <img src="http://www.site_name.com/cgi-bin/open.pl" width="50" height="20">
          I know I'm missing something conceptually here, making this more of a project than it really is but... Are you suggesting dynamic html from a new script that uses the server "intelligen ce" to both gather the day and time and then write the html code for both image and hyperlink, or some other approach? Would that require SSI to call the CGI or how would it be inserted into an otherwise static page?

          Thanks.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Someone put together a small perl script for me
            I did the script for you on the webmaster forum.

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              It's a world of laughter
              A world of tears
              It's a world of hopes
              And a world of fears
              There's so much that we share
              That it's time we're aware
              It's a small world after all

              There is just one moon
              And one golden sun
              And a smile means
              Friendship to ev'ryone
              Though the mountains divide
              And the oceans are wide
              It's a small world after all

              It's a small world after all
              It's a small world after all
              It's a small world after all
              It's a small, small world

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                hehehe..... brings back memories of Disneyland :)

                what he wants to do could be handled via SSI and a perl script called using the include virtual SSI tag:

                <!--#include virtual="../cgi-bin/open.pl" -->

                which is cool becuase it allows you to send arguments to the script in the query string so you can get pretty fancy with it.

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  he has this question posted on yet a different perl forum from this one and from the original one where I wrote the script for him.

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    Originally posted by miller
                    It's a world of laughter
                    A world of tears
                    It's a world of hopes
                    And a world of fears
                    There's so much that we share
                    That it's time we're aware
                    It's a small world after all

                    There is just one moon
                    And one golden sun
                    And a smile means
                    Friendship to ev'ryone
                    Though the mountains divide
                    And the oceans are wide
                    It's a small world after all

                    It's a small world after all
                    It's a small world after all
                    It's a small world after all
                    It's a small, small world
                    ROTFLMAO!!!!! It simply amazes me that people think that others only read one forum. I post in more than one myself, I know that Kevin does as I have seen his posts. I mean COME ON PEOPLE!!!

                    Jeff

                    Comment

                    • smarsh
                      New Member
                      • Jul 2007
                      • 18

                      #11
                      You all got bent out of shape for nothing... nothing insidious going on...

                      I said from the start that someone else wrote the script. I couldn't find the bookmark I had for the forum where it originally was posted, so I searched Google and found some new perl forums, so I posted there. KevinADC IS THE AUTHOR of the script and should be credited 110% for his work.

                      If this is a big offense to you guys and don't want to help out, say so and I'll ask elsewhere.

                      Thanks.

                      Comment

                      • numberwhun
                        Recognized Expert Moderator Specialist
                        • May 2007
                        • 3467

                        #12
                        Bent out of shape, you wouldn't like it if I were bent out of shape? (Sorry, channeled The Hulk for a second there) No, that was more laughing hillariously at the silliness that some cross posters exert. We know who wrote the script, Kevin said it in his post when he told us he already wrote it for you. ( he he,

                        If it comes down to it that the question you are posting was answered elsewhere, then say that. Say that you posted and got the question answered elsewhere but cannot remember where so you need to repost it. You would have probably been given the link to the other post by Kevin here.

                        Regards,

                        Jeff

                        Comment

                        • smarsh
                          New Member
                          • Jul 2007
                          • 18

                          #13
                          He created the original script, but the new issue (links with the images) is still unanswered.

                          Thanks.

                          Comment

                          • miller
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1086

                            #14
                            Hello smarsh,

                            Please do not misconstrue my amusement at the prolific state of Kevin's contributions as a rebuke of your question and situation.

                            However, I believe that I personally have helped you as much as I can at this time. You're at that stage now where you must decide what technological level that you want to create this new feature at. And that's not something that I generally like to advise people in.

                            Do you want to use SSI?
                            CGI?
                            Javascript?
                            Some mixture of the above?

                            shrug,
                            - Miller

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              Originally posted by smarsh
                              He created the original script, but the new issue (links with the images) is still unanswered.

                              Thanks.

                              See post #8 in this thread. Using SSI and a slightly different perl script you can generate the image and the link wihin your shtml document.

                              Code:
                              #!/usr/bin/perl
                              use CGI::Carp qw/fatalsToBrowser/;
                              use strict;
                              use warnings;
                              # path to folder where images are
                              # assumes an image named open.gif and closed.gif
                              my $path = '/home/fireplug/public_html/images/';
                              # time in military format 0-23
                              # first number is opening time
                              # second number is closing time
                              # 24-24 indicates always closed
                              # server is central standard time so time is adjusted minus 1 hour
                              my %schedule = (
                                 Sun => [9,11],
                                 Mon => [10,11],
                                 Tue => [10,11],
                                 Wed => [10,11],
                                 Thu => [10,11],
                                 Fri => [10,11],
                                 Sat => [24,24],
                              );
                              # default value
                              my $image = 'closed.gif';
                              my $alt = 'sorry, we are closed';
                              my $page = 'closed.html';
                              
                              # get the day and hour
                              my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
                              $hour = (split(/:/,$hour))[0];
                              
                              # check the day and hour
                              if ($schedule{$day}[0] <= $hour && $schedule{$day}[1] > $hour) {
                                 $image = 'open.gif';
                                 $alt = 'we are open';
                                 $page = 'open.html';
                              }
                              
                              #print appropriate MIME type header
                              print "Content-type: text/html\n\n";
                              print qq{<a href="../$page title="$alt"><img src="../images/$image" alt="$alt"></a>};
                              PS: I accept donations.

                              Comment

                              Working...