#! /usr/local/bin/perl
# Web Dice
# dice.pl
# Version 1.0
# By Mike Calabrese
# http://www.mikecalabrese.com
# mikecalabrese@mikecalabrese.com
# 03-04-2004
# This makes sure that the data is coming from your site
$refurl = ("yourdomain.com");
if ($ENV{'HTTP_REFERER'} !~ /$refurl/g) {
&bad_referer;
}
$dice_image_path =
"http://www.yourdomain.com/exact/url/path/to/dice/dice_images";
# Receive & clean up the data
$theinput = <>;
chomp $theinput;
my @value_pairs = split (/&/,$theinput);
my %formdata = ();
foreach $pair (@value_pairs) {
($field, $value) = split (/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$formdata{$field} = $value;
}
# Assign variable names to the input
$number_dice = $formdata{number_dice};
print "Content-type: text/html\n\n";
print <<"HTMLDOC";
<HTML><HEAD><TITLE>Web Dice</TITLE></HEAD>
<BODY BGCOLOR="#ffffff" TEXT="#000000" LINK="#008000" VLINK="#400000">
<CENTER>
<H3>Web Dice</H3><P>
<P>
HTMLDOC
if ($number_dice eq "1") {
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
}
if ($number_dice eq "2") {
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
}
if ($number_dice eq "3") {
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
}
if ($number_dice eq "4") {
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TABLE BORDER="0"><TR><TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD></TR>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TR><TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD></TR></TABLE>
HTMLDOC
}
if ($number_dice eq "5") {
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
<BR>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<IMG SRC="$dice_image_path/die$roll.jpg">
HTMLDOC
}
if ($number_dice eq "6") {
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TABLE BORDER="0"><TR><TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD></TR>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TR><TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD>
HTMLDOC
undef $roll;
$roll = int(rand 6) + 1;
print <<"HTMLDOC";
<TD VALIGN="Top">
<IMG SRC="$dice_image_path/die$roll.jpg">
</TD></TR></TABLE>
HTMLDOC
}
print <<"HTMLDOC";
<P><FORM METHOD="POST" ACTION="dice.pl">
<INPUT TYPE="Hidden" NAME="number_dice" VALUE="$number_dice">
<INPUT TYPE="Submit" VALUE="Roll Again">
</FORM>
<P>
<SMALL>This application by
<A HREF="http://www.mikecalabrese.com/index.shtml">Mike Calabrese</A>
</CENTER></BODY></HTML>
HTMLDOC
exit;