How to write an add script by using randomly chosen
banners?
I will try to explain this to you as good as possible. Lets start thinking
about what our script should do:
The idea of the script is rather easy. All it needs to do is choose a banner randomly
out of a list of banners and print it the html code out to a webpage. We will
include the banner in the page by using SSI INCLUDES (make sure your server
supports this!).
That is all, but how should we do this?
Let us start by creating a file, called rotation_info.txt . This file will
contain all the banners and their associated information. It would be easy to
read from this file if each piece of information of one banner is on the same
line, seperated by :: (two double points ':' ). If every banner is
one another line, it will be easier to read it and to use the randomness. Now,
what information do we need? We need to know the url to the banner itself, the
url to the page it represents and a short text to desribe the page. We'll make each line look like this:
website_url::page_description::banner_url |
Ok add the banners you need, and more later on when needed. Lets
start writing our script!
Create a new file, called show_banner.cgi and open it.
The first line of the script should be your path to perl, eg;
This could differ on every server, so if you are not sure, ask
your system administrator!
Now, we need to upen the file "rotation_info" and store its contents
in the array "@rotation_info" so we can pick out a banner later. We do
this by doing the following;
open(INF,"rotation_info.txt")
|| errhandler("Couldn't open rotation_info for reading!");
@rotation_info = <INF>;
close(INF); |
I will try and explain this piece of code; The first line will open up the
"rotation_info" file and store the file in the placeholder INF. In
case something is wrong (the file is missing, the permissions have been set up
wrongfully or anything else what could go wrong..), the script will execute the
"errhandler" function. Dont mind this yet, i will explain this later
on.
Then, on the second line, we store the contents of the file in the "@rotation_info"
array, so we can use it later in the script. The last line is simply to close
(and erase) the placeholder INF (wich is actualy the file.) This is very
important because it will save memory while the script is running.
Now we must set the random number seed. You can do this on the following
three ways:
1. srand;
2. srand(time|$$);
3. srand(time() ^ ($$+ ($$ << 15)) ); |
Way 2. is less predictable than way 1. and three is less predictable than 2.
Get the point? NOTE: the last one will only work on
UNIX. Don't try it on Windows system because it won't work. Instead, use
srand(time); !
Ok, with the random seed number set, we can move on to the next step; picking a
banner out of the list. You need to know one thing about arrays and files, in
case you don't already; When you store a file into an array, each line of the
file will be stored in another element of the array. An array always starts with
element 0, then 1, 2, etc. So what does this mean? The first line of the file
will be stored in the first element, and in the
@rotation_info array the first element equals to $rotation_info[0] . The
second line will equal $rotation_info[1] etc. When you know this, all you got to
do is pick a number and use that number in the array, and store that element of
the array in another variable. We use the following code to do this:
$banner_info = $rotation_info[int(rand(@rotation_info))];
chomp $banner_info; |
The variable where we are storing it all in, is called $banner_info. So we
got the set it equal to the element of the array.
First, we use the rand(@rotation_info) to return a value, based on the actual
length of the array. We then use the int function to convert this returned value
into an integer. The second line of the code will simply remove the break from
the end of the stored line in $banner_info .
Now we got the line wich holds the information about the banner, we can't use
this line like this. We need to store each part of the line into a seperate
variable so we can use them. Add this line of code:
($url,$alt,$image) = split(/::/,$banner_info); |
Again, what happens here is fairly simple. All we do is extract the
information out of the line. As we mentioned above, each line will look like
this: website_url::page_description::banner_url
. We simply split the information into each variable by using the split
function. The first argument of this function equals the seperator (in this
case: :: ), and the second argument is the variable ($banner_info) where the
information is stored.
With all the variables and their appropriate information ready for use, we
simply need to print out the html code! Here is how:
print "Content-type: text/html\n\n";
print "<a href=\"$url\"><img height=60 width=468 src=\"$image\" border=\"1\" alt=\"$alt\"></a><br>";
print "<font size=\"1\"><a href=\"http://www.planetkoffie.com/addyourbanner.shtml\">Add your banner.</a>";
|
The first line is VERY important, because its the header for the script. If
you do not include this line, your script won't run and there will be no html
printed out to the page. The next two lines are rather easy to understand i
think, it is just basic html. The only thing you might need to know is why is
every " preceded by a \ ? Well, this is fairly simple; the print function
uses " .. " to indicate where the text is it should print out. If you
insert a " inside these two, the function will stop too early with printing
out, and the script will try to execute the rest of the line, but since it isn't
code, it won't recognize it and the script will stop running. So we put a \
before them, so the script knows it isn't the end of the text. Get it?
The script is finished! Save it, and open up your ftp program. Upload the two
files to your server and chmod rotation_info.txt to 444 and chmod
show_banner.cgi to 755. If you dont know how to CHMOD files with your ftp
program, read its help files to know how.
Now, insert the following in the html page where you want the banner to show up;
<!--#exec cgi="/path.to.the.file/show_banner.cgi" --> |
Change the /path.to.the.file/ part to the correct path ofcourse. Incase this way of executing isn't working out, contact
your server administrator and ask them how you should do this. This may differ on every server!
That's it! If you'd like to see an example of this script (it has some extra
features in it though), check out http://www.planetkoffie.com
.
I hope this small tutorial has been useful to you, and if you have any
questions, suggestions, comment or critism, please let me know about them! My
e-mail is dekoffie@planetkoffie.com |