473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 2476
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,509 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,509 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,509 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

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

Similar topics

4
by: Dynamo | last post by:
Not sure if this is a php or an html question but I'll post it anyway. I have multiple thumbnail images on my catalogue page catalogue.php and they are all hyperlinked to a single page called...
11
by: Wentink | last post by:
Does anybody have a simple script that let's me popup a picture from a thumbnail? The ones i found are all very very complicated and messy in the source... Thanks, Tintin
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
8
by: Nathan Sokalski | last post by:
I have several System.Web.UI.WebControls.HyperLink Controls which I want to display as rollover images. I know how to make these manually using the <a> and <img> tags or the <a> tag and a...
2
by: jon | last post by:
Hi This might be an easy one to answer but I have a web form in an ASP.NET 1.1 application with a hand-rolled HTML image button as follows which opens a help guide in a new window: <input...
2
by: Bundy | last post by:
Hi On my webpages I have replaced the submit button with a rolling submit button using the script below (Script 1). This script is used by many of my webpages and is included in a external...
3
by: Nathan Sokalski | last post by:
I am using the ImageUrl property of the HyperLink control. My image is large, so I am setting the width/height attributes, but when it renders the width/height attributes are in the <atag rather...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.