Solved

it is very bother me !

Posted on 1998-12-30
39
182 Views
Last Modified: 2013-12-25
I have a random display image, written by perl.
It used html img tag to call the script like:
<img src="http://imsp.stcloudstate.edu:8002/cgi-bin/img.pl">

If I used IE , the images will be displayed just fine, each time I hit reload, I saw different images. That is what I want.

   B   U   T ,

if I used Netscape, no matter I hit reload how many time, it just displayed the first image that is picked to display.
Why the images will not be chnaged. ?
How to solve this problem ?
I must use img tag , and I hate to use SSI.
Because I want to make it as inline image.

Here is my script :

#!/usr/bin/perl

      $basedir = "http://imps.stcloudstate.edu:8002/";
      @files = ("bup1.gif","bup2.gif","bup3.gif","bup4.gif","bup5.gif");
      srand(time);
      $num = int(rand(5));
      print "Location: $basedir$files[$num]\n\n";


If I didn't use Location, I will get broken image.
For your imformation, this script is residing at ../cgi-bin/
and the images and img.html is residing at ../../web-docs/
0
Comment
Question by:tanc02
  • 22
  • 13
  • 3
  • +1
39 Comments
 
LVL 8

Expert Comment

by:jhurst
ID: 1830341
your are re-loading the same image from the cache!
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830342
but why IE will display the random images, is that a different between
IE and Netscape ?

How to solve this problem ?
It must be called from img tag
-no SSI, please
0
 
LVL 1

Expert Comment

by:sdjjm
ID: 1830343
Yes, you are reloading the image from the cache in Netscape.  I suggest you change your CGI call to:
<!--#exec cgi="cgi-bin/img.pl" -->

And then change the print statement in your CGI to the following which will get around the problem:

print "<img src=\"$basedir$files[$num] \"> ";
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830344
if I used this <!--#exec cgi="cgi-bin/img.pl" -->  , then I have to change
index.html to index.shtml, right ?
But I really hate to use SSI, What if , I want my friend to call this script
from his account and he doesn't have permission to use SSI.
I think there must be a way to solve this problem without SSI.
For example, I saw some web site provide free counter by placing
image tag in my html file, why they can do and I have the problem ?
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830345
sorry to reject your answer, because I think there is a way to solve this....
0
 
LVL 1

Expert Comment

by:sdjjm
ID: 1830346
I've done exactly what you want with a very simple JavaScript before, maybe you can use it (or modify it and use a combination of JS and your Perl).  Here's the JS and HTML that will work for you.  It can also be added to your friends document as well  (I tested it from MY server and got a different blue button to reload off YOUR server everytime :)  
It works by adding a different search string value after the image href causing the script to reload the image from the server each time...

<SCRIPT LANGUAGE="JavaScript"><!--
function loadImage() {
    filenum = Math.round(Math.random() * 4) +1;
    var now = new Date();
    document.images.myImageName.src = 'http://imps.stcloudstate.edu:8002/bup' + filenum + '.gif?' + now.getTime();
    }
//--></SCRIPT>

<IMG SRC="javascript:loadImage()" NAME="myImageName">

0
 
LVL 1

Author Comment

by:tanc02
ID: 1830347
It is very nice of you, sdjjm ! But I just would like to implement this script by using
perl ONLY. If I used javascript, then my friend will not be able to call this script
by placing <img src="http://xxxxxxxxxx/cgi-bin/xx.pl">, right ?
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830348
Ok problem 1 is that when no cache or expirey http header is received IE and NN behave in different ways depending on the mime type of the data.

As you have seen with Netscape it caches the results.

The answer is to send the no-cache pragma. However problem 2 is that because you are using the location header no other http head can be sent.

So we need to find a way to avoid using the location header.

I assume that you are using it because your images are stored in the directory above your cgi-bin.

The obvious way to address this is to output the image directly from the perl script. This would mean instead of just outputing the location you should do something like as follows:

print("HTTP/1.0 200\n")
print("Content/type image/gif\n");
print("pragma:no-cache\n\n");

now open the gif file and output its contents directly to stdout.

HTH
steve
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830349
mouatts :
 
  this is my script :

#!/usr/bin/perl

@files=("bup1.gif","bup2.gif","bup3.gif","bup4.gif","bup5.gif");
srand(time);
$num=int(rand(5));
print "HTTP/1.0 200\n";
print "Content/type image/gif\n";
print "pragma:no-cache\n\n";
print "\"http://imps.stcloudstate.edu:8002/$files[$num]\"\n\n";

anything wrong ? why I got a broken image, how to fix that ?
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830350
the image still not display
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830351
You need to open the image file read its contents then output it.
so assuming that the files are all on the same box as the CGI then you would do something like
open THISFILE $files[$num]
while <THISFILE>
{
print $_;
}

Steve
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830352
to mouatts :
how to put this in my script

open THISFILE $files[$num]
    while <THISFILE>
    {
    print $_;
    }

I don't have clues.
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830353
can any one tell me how to put no-cache
in html. Should be in <head> ? how ? or in cgi ?
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830354

mouatts:
 
    sorry to reject your asnwer, since I have been waiting too long to get the asnwer from you !   So, I think it will be fair and let others to answer this question.
0
 
LVL 11

Accepted Solution

by:
mouatts earned 30 total points
ID: 1830355
Try the following.

#!/usr/bin/perl

@files=("bup1.gif","bup2.gif","bup3.gif","bup4.gif","bup5.gif");
srand(time);
$num=int(rand(5));
print "HTTP/1.0 200\n";
print "Content/type image/gif\n";
print "pragma:no-cache\n\n";
open THISFILE $files[$num]
while <THISFILE>
{
print $_;
}


0
 
LVL 1

Author Comment

by:tanc02
ID: 1830356
I got a compiled error :

#!/usr/bin/perl

@files=("bup1.gif","bup2.gif","bup3.gif","bup4.gif","bup5.gif");
srand(time);
$num=int(rand(5));

print "HTTP/1.0 200\n";
print "Content-type: image/gif\n";
print "pragma:no-cache\n\n";

open (THISFILE,$files[$num]);  -- I think you forgot () .
while <THISFILE>        
{
  print $_;
}
close(THISFILE);  -- you gorgot to close ?

Here is the error message :

syntax error at banner.cgi line 12, near "while <THISFILE>"
Execution of banner.cgi aborted due to compilation errors.
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830357
and how to change the code because my images are in the dir :

   /home/tanc02/web-docs/mcs/home1.gif


0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830358
Sorry right idea, missing brackets but wrong place.

Below is the corrected code I've also added the path that you wanted. As an alternative you could place this within the list of files at the start of the routine or just append it at the point of opening

HTH
Steve

@files=("bup1.gif","bup2.gif","bup3.gif","bup4.gif","bup5.gif");
srand(time);
$num=int(rand(5));

print "HTTP/1.0 200\n";
print "Content-type: image/gif\n";
print "pragma:no-cache\n\n";

open THISFILE,"/home/tanc02/web-docs/mcs/".$files[$num];
while (<THISFILE>)
{
  print $_;
}
close THISFILE;  
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830359
why I got the broken page.

the images is in the /home/tanc02/web-docs/mcs/
and the cgi script is in /home/tanc02/web-server/cgi-bin/

what's wrong ?

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 11

Expert Comment

by:mouatts
ID: 1830360
Try appending

exit 0;

at the end of the script;

Steve
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830361
I don't know why, but if I took out these lines :

   print "HTTP-1.0 200\n\n";
   print "pragma:no-cache\n\n";

everything is fine.

So here is my code :

#!/usr/bin/perl
$|=1;
@files=("home1.gif","mess1.gif","contact2.gif","faculty1.gif");
srand(time);
$num=int(rand(5));
print "Content-type: image/gif\n\n";
open THISFILE,"/home/tanc02/web-docs/mcs/".$files[$num];  
while (<THISFILE>)                                                              
{
   print $_;
}
close THISFILE



can you explain to me line by line for the code below and why I need those lines :

open THISFILE,"/home/tanc02/web-docs/mcs/".$files[$num];  
while (<THISFILE>)                                                              
{
   print $_;
}
close THISFILE


Thank a lot for trying to help me !
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830362
oh yeah ! what does this mean ?

$|=1;
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830363
tanc02:
$|=1;

This means output autoflush. I'm not certain that it has any effect here as it is normally used with pipes. It basically turns buffering off. However according to my manual it doesn't work with STDOUT.

That you took out those two lines and it worked is a bit weird. The first one should always be present but I know that you can often get away with it. So far as the pragma one is concerned it is there to prevent the browsers cacheing the image so that on a second call the get a new image and not the one from the cache.

Did you try the script with the Exit in it before you removed these two lines. If so try putting them back in and see if it still works.

So far as the code is concerned.

open THISFILE,"/home/tanc02/web-docs/mcs/".$files[$num];    

This opens the file that you have chosen randomly and assigns it a handle called THISFILE

while(<THISFILE>)

This is a condition that says do the next bit (within {})until you reach the end of the file. It also reads the file as well to establish whether EOF has been reached.

print $_;

This outputs the data that has just been read to STDOUT.

close THISFILE
This closes the file.

If this is now working can we close this question now please.

Steve          


0
 
LVL 1

Expert Comment

by:sdjjm
ID: 1830364
Hallelujah!
Either close it, or drop back 10 and punt....
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830365
I have tried everyting I can, I think this is the best :

#!/usr/bin/perl
$|=1;

@files=("home1.gif","mess1.gif","contact2.gif","faculty1.gif");
srand(time);
$num=int(rand(4));
print "Content-type: image/gif\n\n";
open THISFILE,"/home/tanc02/web-docs/mcs/".$files[$num];
while (<THISFILE>)
{
   print $_;
}
close THISFILE;
exit 0;


but if I called the script like this imps.stcloudstate.edu:8018/cgi-bin/try.pl
I can see the images changed when I hit reload.

but If I tried to use <img> tag in the imps.stcloud........./try.html
and the image tag is <img src="http://imps.stcloudstate.edu:8018/cgi-bin/try.pl">
the image is changed if I hit reload just one time. And If I hit again, then the second
image will be there forever !

Any Idea, how can I improve my script.

if I included this print "HTTP/1.0 200\n";
then I will get 500 error
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830366
OK two things
1 leave the pragma in but take the HTTP out. It should work OK.
2 Close this question.

Steve

0
 
LVL 1

Author Comment

by:tanc02
ID: 1830367
leave the pragma in will cause the broken image

 leave the pragma out, everything is fine in IE but not netscape.

 please take a look at imps.stcloudstate.edu:8018/try.html
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830368
OK can you post your script as it is back again.

Steve
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830369
here is my script :

#!/usr/bin/perl
$|=1;

@files=("home1.gif","mess1.gif","contact2.gif","faculty1.gif");
srand(time);
$num=int(rand(4));
print "Content-type: image/gif\n\n";
#print "pragma:no-cache\n\n";
open THISFILE,"/home/tanc02/web-docs/mcs/".$files[$num];
while (<THISFILE>)
{
 print $_;
}
close THISFILE;
exit 0;

the reason I would like it to work on both browser is because someonemay want to use
my img tag on his/her web-site

0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830370
Try this

#!/usr/bin/perl
$|=1;

@files=("home1.gif","mess1.gif","contact2.gif","faculty1.gif");
srand(time);
$num=int(rand(4));
print "Content-type: image/gif\n";
print "pragma:no-cache\n\n";
open THISFILE,"/home/tanc02/web-docs/mcs/".$files[$num];
while (<THISFILE>)
{
 print $_;
}
close THISFILE;
exit 0;

0
 
LVL 1

Author Comment

by:tanc02
ID: 1830371
still the same thing, it just changed the first image, the rest images will still be the same !
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830372
I just cut and paste this code altering only the image names and the file location and it worked in IE4,Netscape 3 and Netscape 4.

With Netscape 4 I went a tep further and cleared my cache and then hit the reload button many many times whilst monitoring the cache directory. Finally I closed Netscape down again thus purging the memory cache to disk. Except nothing was written to disk! The cache remained empty throughout.

You are absolutely right that sometimes the image doesn't change but that is becuase the number being generated by random is the same in two or more consequative occassions.

This code is working as you required so there is nothing further that I can add.

Steve
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830373
sorry to bother, I will appreciate if you can go to these site

 imps/stcloudstate.edu:8018/cgi-bin/try.pl
 -  everything is fine here, can see all the images changed.

 imps.stcloudstate.edu:8018/try.html
 - only the first imgae changed. I also tried to hit reload so many many many many many    time.

If you tried the site above, you will now what I mean .

once again, sorry !


0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830374
Ok the problem appears to be with Netscape 4 (I don't know about 4.5)

With Netscape 3 it works correctly.

I have tried a number of things but N4 seems to lose track, internally, of the status of the image. If you check the page info you will see that it reckons that the image is not cached.

This is not a new bug however, although I didn't think of this until now, but with N4 if you do a reload the images don't refresh. To force that you normal do a view image and reload that then when you go back it utilises the image previously loaded.

I am afraid that I don't believe there is a way around this. Nevertheless the script is now working on other browsers.

Steve
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830375
I am using Netscape 4.5.
but why I can see other peolpe web's images changed, they also used img call.
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830376
Can you give me a URL that demonstrates this working.
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830377
it is long time ago, I saw those web-site, but give me a day I will try to find it.
Mean while, I think the best example are the links that provided by LinkExchange, they use img call, right ? and the images changed everytime.
0
 
LVL 1

Author Comment

by:tanc02
ID: 1830378
it is long time ago, I saw those web-site, but give me a day I will try to find it.
Mean while, I think the best example are the links that provided by LinkExchange, they use img call, right ? and the images changed everytime.
0
 
LVL 11

Expert Comment

by:mouatts
ID: 1830379
I'm afraid that LinkExchange does the same thing. Try my site at www.bedrockcomputers.demon.co.uk and you wil see that the images refresh for a short time only.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now