Solved

inserting a random html snippet in an SSI tag?

Posted on 2000-04-20
2
315 Views
Last Modified: 2013-12-25
I've got the basic framework down for selecting from a list and displaying a random piece of arbitrary code, but much to my chagrin it always picks the last of them instead of a random one.  I know it's probably nothing more than a misplaced bit of punctuation, but I'm pulling my hair out over it...here's the code:

#!/usr/bin/perl
# Define Variable

@includedhtml = ("<random bit of html #1>",
      "<random bit #2>",
      "<random bit #3>");

srand(time ^ $$);
$num = rand(@includedhtml); # Pick a Random Number
print "Content-type: text/html\n\n";
print $includedhtml[$num];
print "\n";

Thanks everybody!
0
Comment
Question by:dibowkley
2 Comments
 
LVL 1

Accepted Solution

by:
chaduka earned 50 total points
ID: 2737302
I donno what's contained in the <random bit of HTML ##> part, but make sure they are well written and characters that need escaping are taken care of correctly.

See my alteration and another suggested solution to handle this product below:

#!/usr/bin/perl
# Define Variable

@includedhtml = ('<random bit of html #1>',
'<random bit #2>',
'<random bit #3>'); # Note the use of single quotes, double quotes are a bit pecky.

srand(); # The srand() func. uses time as a seed by default.

$num = int(rand(@includedhtml)); # Pick a Random Number
# the rand() function returns a floating point value. You need to get the integer part of this number for your app to know exactly what to pick from @includedhtml

print "Content-type: text/html\n\n";
print $includedhtml[$num];
print "\n";

--------------------------------------
I would suggest you do it like this:
1) Either have the bits of HTML in a file, as in:
 1:<htmltag>My text</htmltag>
 2:<htmltag2>My other text</htmltag2>
or put each bit of HTML in a seperate file e.g. 1.html will contain some HTML stuff, 2.html etc...
2) generate a random number using srand() and rand().
3) read the line or file corresponding to that number.

Note: seperate files will be a bit faster if your randmon bits count grow.

...or better still, try a database!!

Hope this helps, ...and have fun!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 2738642
#The only bit of punctuation that should be giving you trouble is the ^
srand(time + $$); #would be a bit better
#or in 5.004 or later you can add a # before that line
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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