« Ping ping, ping pong | Index | Alle angsten »

January 30, 2004, by Léon Krijnen

How to randomize   

random.gifI've been asked questions about the random cookies and the random thumbnails. Also the logo on top of the site randomizes from colour to tiles to black and white. How do we do it?
It's really very simple. You put an executable file (chmod 755) in your cgi-local, or anywhere else your server allows you to execute files.
The name of the file can be something like random.cgi, or random.pl, depending on your httpd.config file (I think :-)
Anyway, your randomizer should look like this:

### beginning of the script ###

$random_file = "random.dat";
open (FILE, $random_file);
print "Content-type: text/html; charset=CP1250\n\n";
print "@LINES[int rand(@LINES)]\n";

### end of the script ###

In the second line you see the name of the file containing whatever you want to parse at random. The script reads lines until hard breaks, so if you want to show breaks in the ouput use the html-tag <br>
The random.dat file could look like:

The wombat lives across the seas,<br>Among the far Antipodes.<br>He may exist on nuts and berries,<br>Or then again, on missionaries;<br>His distant habitat precludes<br>Conclusive knowledge of his moods.<br>But I would not engage the wombat<br>In any form of mortal combat.
Excellent day to have a rotten day.
The faster we go, the rounder we get. <br> [ © The Grateful Dead ]

You get it: this dat file contains three cookies.

To call the script in action you use a server side include. Most servers want the html-file in which you use an include to have the .shtml extension.

Put this tag (depending on your path, some servers demand a full path) where you want your cookie to show up:

<!--#exec cgi="/cgi-local/random.cgi" -->

and random.cgi will parse one cookie at random. You can wrap font and color tags around your include call, or you can hack random.cgi and add two lines of code containing the tags. In this case the last two lines would become the last four lines, something like:

print "Content-type: text/html; charset=CP1250\n\n";
print "<font face=verdana size=2>";
print "@LINES[int rand(@LINES)]\n";
print "</font>";

A randomizer for thumbnails works the same. This script could be named randompics.cgi (or .pl), calling randompics.dat
The randompics.dat can contain as much links as you like to any pictures in any directory.
As I'm using ImageFolio as web fotoalbum, my randompics.dat contains links tot the individual url's on the thumbnailpages, and looks like this:

<a href="/cgi-local/imageFolio.cgi?action=view&link=Australia_2000&image=01.jpg"><img src="/gallery/Australia_2000/tn_01.jpg" alt="Australia 2000/01" border="0"></a><br>
<a href="/cgi-local/imageFolio.cgi?action=view&link=Australia_2000&image=02.jpg"><img src="/gallery/Australia_2000/tn_02.jpg" alt="Australia 2000/02" border="0"></a><br>

And so on ...

Have fun!

Link: all cookies

Posted: January 30, 2004 11:57 AM (408 words).   

Comment over here or on my Facebook wall . . .