469,325 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,325 developers. It's quick & easy.

CGI Script - Modify image handler to include hyperlink

18
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!


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI::Carp qw/fatalsToBrowser/;
  3.  
  4. use strict;
  5. use warnings;
  6.  
  7. # path to folder where images are
  8. # assumes an image named open.gif and closed.gif
  9. my $path = '/home/fireplug/public_html/images/';
  10.  
  11. # time in military format 0-23
  12. # first number is opening time
  13. # second number is closing time
  14. # 24-24 indicates always closed
  15. # server is central standard time so time is adjusted minus 1 hour
  16. my %schedule = (
  17.     Sun => [9,11],
  18.     Mon => [10,11],
  19.     Tue => [10,11],
  20.     Wed => [10,11],
  21.     Thu => [10,11],
  22.     Fri => [10,11],
  23.     Sat => [24,24],
  24. );
  25.  
  26. # default value
  27. my $image = 'closed.gif';
  28.  
  29. # get the day and hour
  30. my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
  31. $hour = (split(/:/,$hour))[0];
  32.  
  33. # check the day and hour
  34. if ($schedule{$day}[0] <= $hour && $schedule{$day}[1] > $hour) {
  35.     $image = 'open.gif';
  36. }
  37.  
  38. #print appropriate MIME type header
  39. print "Content-type: image/gif\n\n";
  40.  
  41. open(FH,"$path$image") or die "$!";
  42. binmode(FH);
  43. print while (<FH>);
  44. close(FH);
  45. exit; 
Jul 25 '07 #1
23 2149
miller
1,089 Expert 1GB
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
Jul 25 '07 #2
smarsh
18
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!
Jul 25 '07 #3
miller
1,089 Expert 1GB
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
Jul 25 '07 #4
smarsh
18
Thanks Miller.

Currently it is a static html page(s). All the "intelligence" is in the CGI where the time and day are gathered and the appropriate image selected. The html is:
Expand|Select|Wrap|Line Numbers
  1. <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 "intelligence" 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.
Jul 26 '07 #5
KevinADC
4,059 Expert 2GB
Someone put together a small perl script for me
I did the script for you on the webmaster forum.
Jul 26 '07 #6
miller
1,089 Expert 1GB
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
Jul 26 '07 #7
KevinADC
4,059 Expert 2GB
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.
Jul 26 '07 #8
KevinADC
4,059 Expert 2GB
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.
Jul 26 '07 #9
numberwhun
3,503 Expert Mod 2GB
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
Jul 26 '07 #10
smarsh
18
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.
Jul 26 '07 #11
numberwhun
3,503 Expert Mod 2GB
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
Jul 26 '07 #12
smarsh
18
He created the original script, but the new issue (links with the images) is still unanswered.

Thanks.
Jul 26 '07 #13
miller
1,089 Expert 1GB
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
Jul 26 '07 #14
KevinADC
4,059 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI::Carp qw/fatalsToBrowser/;
  3. use strict;
  4. use warnings;
  5. # path to folder where images are
  6. # assumes an image named open.gif and closed.gif
  7. my $path = '/home/fireplug/public_html/images/';
  8. # time in military format 0-23
  9. # first number is opening time
  10. # second number is closing time
  11. # 24-24 indicates always closed
  12. # server is central standard time so time is adjusted minus 1 hour
  13. my %schedule = (
  14.    Sun => [9,11],
  15.    Mon => [10,11],
  16.    Tue => [10,11],
  17.    Wed => [10,11],
  18.    Thu => [10,11],
  19.    Fri => [10,11],
  20.    Sat => [24,24],
  21. );
  22. # default value
  23. my $image = 'closed.gif';
  24. my $alt = 'sorry, we are closed';
  25. my $page = 'closed.html';
  26.  
  27. # get the day and hour
  28. my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
  29. $hour = (split(/:/,$hour))[0];
  30.  
  31. # check the day and hour
  32. if ($schedule{$day}[0] <= $hour && $schedule{$day}[1] > $hour) {
  33.    $image = 'open.gif';
  34.    $alt = 'we are open';
  35.    $page = 'open.html';
  36. }
  37.  
  38. #print appropriate MIME type header
  39. print "Content-type: text/html\n\n";
  40. print qq{<a href="../$page title="$alt"><img src="../images/$image" alt="$alt"></a>};
  41.  
PS: I accept donations.
Jul 26 '07 #15
numberwhun
3,503 Expert Mod 2GB
PS: I accept donations.
Aaaaalms for the poor......... ***ding - ding***..... Aaaaaalms for the poor.....

Please sir, could you spare a pence or two to pay my 'lectric bill? This coding stuff does use power ya know.

;-|)
Jul 26 '07 #16
smarsh
18
Great! This time I got it to run on the 1st try... Thanks!

Donation/compensation... you say tomato I say tomaaato... that's cool.
Give me some details and I'll work on it.
Jul 26 '07 #17
KevinADC
4,059 Expert 2GB
Great! This time I got it to run on the 1st try... Thanks!

Donation/compensation... you say tomato I say tomaaato... that's cool.
Give me some details and I'll work on it.

Glad you go it working. I did spot an error in the last line of the code:

Expand|Select|Wrap|Line Numbers
  1. print qq{<a href="../$page title="$alt"><img src="../images/$image" alt="$alt"></a>};
there should be a double-quote after $page:

Expand|Select|Wrap|Line Numbers
  1. print qq{<a href="../$page" title="$alt"><img src="../images/$image" alt="$alt"></a>};
check your PM's.
Jul 28 '07 #18
smarsh
18
All of a sudden (I think) I'm getting wacky results. I thought I tested the script during open and closed times, yet now nothing makes sense. The server is GMT-6 . I want the open time for Monday to be between 10 and 11 AM, so I set it that way. The only way I can get it to work and show the "open" graphic is by setting it to 6. The close time doesn't seem to work right at all - like setting it to 7 for 11AM (because of the 6 being 10???) it still stays open after 11.

Any thoughts?
Aug 6 '07 #19
KevinADC
4,059 Expert 2GB
post your current code.
Aug 7 '07 #20
smarsh
18
This is the current script that as far as I know, I have only "modified" by setting the times for each day of the week. Monday should be [10,11] as well, but I changed that to [6,11] to get the open time to work at the actual 11:00AM. The server checks out to be running the correct time in the central time zone.

I'm lost...

Thanks.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use CGI::Carp qw/fatalsToBrowser/;
  4.  
  5. use strict;
  6. use warnings;
  7.  
  8. # path to folder where images are
  9. # assumes an image named open.gif and closed.gif
  10. my $path = '/home/fireplug/public_html/images/';
  11.  
  12. # time in military format 0-23
  13. # first number is opening time
  14. # second number is closing time
  15. # 24-24 indicates always closed
  16. # server is central time
  17. my %schedule = (
  18.     Sun => [9,11],
  19.     Mon => [6,11],
  20.     Tue => [10,11],
  21.     Wed => [10,11],
  22.     Thu => [10,11],
  23.     Fri => [10,11],
  24.     Sat => [24,24],
  25. );
  26.  
  27. # default value
  28. my $image = 'closed.gif';
  29. my $alt = 'sorry, we are closed';
  30. my $page = 'closed.html';
  31.  
  32. # get the day and hour
  33. my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
  34. $hour = (split(/:/,$hour))[0];
  35.  
  36. # check the day and hour
  37. if ($schedule{$day}[0] <= $hour && $schedule{$day}[1] > $hour) {
  38.     $image = 'open.gif';
  39.     $alt = 'we are open now';
  40.     $page = 'open.html';
  41. }
  42.  
  43. #print appropriate MIME type header
  44. print "Content-type: text/html\n\n";
  45.  
  46. print qq{<a href="../$page" title="$alt"><img src="../images/$image" alt="$alt"border="0"></a>};
Aug 7 '07 #21
KevinADC
4,059 Expert 2GB
You should offset the time here:

Expand|Select|Wrap|Line Numbers
  1. my ($day, $hour) = (split(/ /,scalar localtime))[0,3];
add or subtract the number of seconds from 'time' to off set the server time to your desired time-zone, an example adding 6 hours:

Expand|Select|Wrap|Line Numbers
  1. my ($day, $hour) = (split(/ /,scalar localtime(time+21600)))[0,3];
and use the actual hours of open/close in the hash of arrays:


Expand|Select|Wrap|Line Numbers
  1. my %schedule = (
  2.    Sun => [9,11],
  3.    Mon => [10,11],
  4.    Tue => [10,11],
  5.    Wed => [10,11],
  6.    Thu => [10,11],
  7.    Fri => [10,11],
  8.    Sat => [24,24],
  9. );
to offset the server time by plus one hour: 60 * 60 * 1 = 3600

localtime(time+3600)

to offset the server time by minus 2 hours: 60 * 60 * 2 = 7200

localtime(time-7200)

I think you get the picture.
Aug 8 '07 #22
smarsh
18
I understand the time adjustment: I'm GMT-5 and the server is GMT-6, so if I use time+3600 I effectively adjust the server time to my time zone and the hash arrays schedule is entered based on my local time. But that's not the problem. It's just acting wacky.

I put in the time+3600 adjustment and to test changed the Wed schedule to
Expand|Select|Wrap|Line Numbers
  1. Wed => [18,20]
because I'm working on it at 6:15PM on Wed. It didn't work though. I had to back down to
Expand|Select|Wrap|Line Numbers
  1. Wed => [9,20]
to get the "open" image to appear. That's a time difference of 9 hours.

What can be going on?
Aug 8 '07 #23
KevinADC
4,059 Expert 2GB
I asked you to post your current code, in that current code I see this line:

my ($day, $hour) = (split(/ /,scalar localtime))[0,3];

there is no offset. Offset the time how I showed you and use the real hours of business in the hash. If that does not work I am not sure what the problem is.
Aug 8 '07 #24

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Wentink | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.