Quick Search for:  in language:    
short,wich,will,explain,randomness,make,banne
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 74,273. lines
 Jobs: 24. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Perl.
Click here to see a screenshot of this code!CGIScripter
By David Simpson on 11/24

(Screen Shot)

Calender
By Jeff Mills on 11/20


quikpoll
By Jeff Mills on 11/20


Encrypt Password
By Jeff Mills on 11/20


Rock, Paper, Scissors w/ GUI
By Kurt Rudolph on 11/19


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Learn to write an ad / banner script!

Print
Email
 

Submitted on: 10/21/2001 5:25:09 PM
By: Rombauts Steven  
Level: Beginner
User Rating: By 4 Users
Compatibility:5.0 (all versions), Active Perl specific, 4.0 (all versions)

Users have accessed this article 4164 times.
 

(About the author)
 
     This is a short tutorial/article wich will explain you how to use randomness to make a banner script. This explains you how to use randomness, the split function, storing files in arrays and ssi includes. This tutorial has an example included in the zip file wich you can also download here! I hope this will help you to understand these functions!

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

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;

#!/usr/bin/perl

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

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.

Virus note:All files are scanned once-a-day by Planet Source Code for viruses,but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:
1)Re-scan downloaded files using your personal virus checker before using it.
2)NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 2 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
11/4/2001 12:45:46 AM:Enrique Hernandez
Excellent tutorial Steven, very good, I gave you a perfect, keep up the good work man!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/4/2001 10:06:50 AM:Rombauts Steven
Thank you very much :) I appreciate your feedback (and especialy when it's as positive as this ;) )
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/16/2002 11:14:08 AM: ccxvbcxv
cvzxcvzxc
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.