?
Solved

SSI rotating image

Posted on 1998-10-08
19
Medium Priority
?
219 Views
Last Modified: 2013-12-25
Is there a way to have a different random image displayed on a page every time it is reloaded solely using server side includes? I would prefer to avoid the use of javascript or cgi with this function.
0
Comment
Question by:ficken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
19 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 1855558
I thought about it, it's a challenging question. But I don't think it's possible, because you need to do some programming to achieve time-dependent differences. And SSI has no programming options as far as I know, it's "include". The trouble is that the including is the first thing a server does when a request is made. After that it may look for scripting.

It depends maybe also on the SSI capacities of the server. Not every server knows every SSI command. What server are you on ?
0
 

Expert Comment

by:singy
ID: 1855559
as far as I know 'sybe' is correct. scripting is necessary.
0
 

Author Comment

by:ficken
ID: 1855560
this is for an IIS server on NT...if it can not be done without scripting, how can it be done so that the broadest array of users and browsers will be able to view the images?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 28

Accepted Solution

by:
sybe earned 400 total points
ID: 1855561
This is ASP which runs on IIS3 if you have ASP installed. IIS4 has it installed by default.

The example will choose between 5 images
It will work for every browser that can display images (the code it browser independent).

==============
<%
Randomize Second(time())
intRandomNumber = Int((5) * Rnd + 1)
SELECT CASE intRandomNumber
  CASE 1
    strBanner = "first.gif"
  CASE 2
    strBanner = "second.gif"            
  CASE 3
    strBanner = "third.gif"
  CASE 4
  strBanner = "fifth.gif"
  CASE 5
    strBanner = "sixth.gif"
END SELECT
%>

<img src="/images/<%=strBanner%>" border="0">


0
 

Author Comment

by:ficken
ID: 1855562
can I combine the script you gave me with an SSI file so that I can store the list of images in a separate file from the HTML file? It would be easier to update and maintain this way...
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855563
Yes, that is possible. I will post the code.
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855564
In this code the file with the image names is called "images.txt.
Each image in that file should be on a new line.
The maximum amount od images in this code is 20.


<%
strFileName = "image.txt"
Set FileObject      = Server.CreateObject("Scripting.FileSystemObject")
Set ReadStream = FileObject.OpenTextFile( strFileName )


Dim Images(20)

i = 1
Do While Not ReadStream.AtEndOfStream
  Images(i) = ReadStream.ReadLine
  i = i + 1
Loop

Randomize Second(time())
intRandomNumber = Int((i) * Rnd + 1)
strBanner = Images(intRandomNumber)

%>

<img src="/images/<%=strBanner%>" border="0">
0
 

Author Comment

by:ficken
ID: 1855565
I now get the following error when I try to load the page....

Server object error 'ASP 0177 : 800a0035'

Server.CreateObject Failed

/temp/test.asp, line 29

The operation completed successfully.



BTW, why the limit on 20 images? If I change the number in the script can I use more, or is this the limit for some reason?
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855566
Hmmm, if you are on IIS3, then the FileSystemObject is not installed automatically, so the script won't work. You'll have to find it somewhere (on the microsoft site).

Another possibility is to store the information in a database. That will work for sure with IIS3, but it seems a bit of an overkill. You could also try to open the txt file using ODBC, again, this seems a bit of an overkill, but you won't have to install the FileSystemObject.

Secondly I have limited it to 20, by dimensioning the array which hold the images with 20.

Dim Images(20)

It is possible to make that larger. If you want the dimensioning to be automatically, then it will take some more scripting. It is possible however.


0
 

Author Comment

by:ficken
ID: 1855567
we are running IIS 4....
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855568
Ok, I see.

I have tested the following code on IIS4, and it worked

<%
ThisFile = Request.ServerVariables("PATH_TRANSLATED")
LastSlash = InstrRev(ThisFile, "\")
ThisDir = Left(ThisFile,LastSlash)

strFileName = ThisDir + "images.txt"
Set FileObject = CreateObject("Scripting.FileSystemObject")
Set ReadStream = FileObject.OpenTextFile(strFileName)


Dim Images(20)

i = 1
Do While Not ReadStream.AtEndOfStream
  Images(i) = ReadStream.ReadLine
  i = i + 1
Loop

Randomize Second(time())
intRandomNumber = Int((i) * Rnd + 1)
strBanner = Images(intRandomNumber)

%>

<img src="/images/<%=strBanner%>" border="0">
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855569
One addition to make it better:

in stead of:
==
Randomize Second(time())
intRandomNumber = Int((i) * Rnd + 1)
strBanner = Images(intRandomNumber)
==

use:

====
MaxImages = i - 1
Randomize Second(time())
intRandomNumber = Int((MaxImages) * Rnd + 1)
====
0
 

Author Comment

by:ficken
ID: 1855570
I am stumped now, the previous problem appears to be fixed, but now I get a broken image when i try to load the page. I have the ".asp" page and "images.txt" page at the root level and the images stored in a "rotating" directory at this level. When I view the source of the page with the broken image, here is what shows for the image source   src="/rotating/">

any ideas why it is not working properly?
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855571
Hmmm,

You could try and debug the script.

Try this:

replace
=====
i = 1
Do While Not ReadStream.AtEndOfStream
  Images(i) = ReadStream.ReadLine
  i = i + 1
Loop
=====

with
=====
i = 1
Do While Not ReadStream.AtEndOfStream
  Images(i) = ReadStream.ReadLine
  Response.write "image nr " & i & " = " & Images(i) & "<br>"
  i = i + 1
Loop
=====

In the browser you'll see the names of the images from the images.txt file.


0
 

Author Comment

by:ficken
ID: 1855572
I am completely stumped, I did your bug test, and each of the image filenames was printed on the page. I still can not find a way to make the images work. is there a problem with naming the directory where the images are stored "rotating" rather than "images"? Does the script need to be located in a certain part of the document? I have the main script in the head and the img tag in the body. should I have anything other than a list of filenames in the images.txt file? Should directory information go here instead of in the image tag perhaps? How is it that the  %> and <% are not interpreted as html tags when they are inside of the image tag, it would seem that  the %> tag would close off the img tag prematurely....is this something the server intercepts and replaces before that becomes a problem?

I am really lost as to what the problem could be.
0
 

Author Comment

by:ficken
ID: 1855573
even if I put all the files in one directory (images, .asp file, images.txt file) i still get the fractured image. This problem started happening since you redesigned the script to include a reference to a list of images, it displayed the images properly when I used the first version which contained the list of images in the .asp file....
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855574
So if the images strings are right, then the error is made after that.

What is the source code of the HTML result (do "view source" in your browser and post the code for the image here)

Secondly you could try to rightclick on the broken image and do "view image", then notice the url that appears in the browsers location, and tell what you see in your browser.

Third, you could try and adapt the script:
replace
==============
<img src="/images/<%=strBanner%>" border="0">
=============
with
=========
<img src="/images/<%=Images(intRandomNumber)%>" border="0">
=========


Another thing that might go wrong is that the images are placed in an "execute only" directory. This means that files can not be read from that. Images have to be read, not executed.


To answer your question:
The <% and %> are not interpreted as HTML, because they never reach the browser. They are only meaningful to the server, and the server leaves them out when output is send to the browser. Look in the "view source", and you will see that that is no sign of the <% and %>.



0
 

Author Comment

by:ficken
ID: 1855575
after changing that img tag code it now works. is there any downfall to using that change? I assume that there was something about the strBanner part that was not working properly?
0
 
LVL 28

Expert Comment

by:sybe
ID: 1855576
I am glad you got it working now.

I don't know exactly why it works now, the code I posted worked on my PC. The fact that it works now with you, sure has something to do with strBanner. There no downfall to using that change. In fact the code should be faster (but that won't be measurable).


0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

770 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