Free Game Downloads -- Sodaware.net

Free Game Downloads -- Role Playing Games, Puzzle Games and Action Games.

Turn hotlinks into advertising – part II

In Monday’s entry, I explained some simple techniques to turn a drain on resources into a free* method of advertising.

In today’s entry, I’ll be going into more depth about how to use a little PHP to dynamically generate your adverts. Why would you want to do this? For starters, you can modify the image to include a discount code, which will help track where the orders came from. Who knows, you could even strike up a deal with the site owner to display ads on their site should it bring you a lot of customers.

Please note that most of the code will just be fragments, and I’m not tying it down to one implementation. It’s quite likely that I’ll modify this method over time and post updates in the future. There’s also the small question of “is it worth it”. Again, I’ll keep you updated.

First things first

First, we’ll create a database table to store several things:

  • The full referring URL – The unique identifier.
  • The number of hits to this file.
  • The discount code, either for this URL or for the domain.

Nothing particularly complex there, although you may wish to tweak it to allow for different advert styles.

Basic PHP

Our file, which I’ll call “image-script.php” needs to carry out several functions:

  • Check to see if we’ve had a referrer from this site before.
    • If we have, log the hit and get the discount code
    • If we haven’t, create an entry and generate a discount code.
  • Create and output an image

Lets look at some sample code

[php]
// Get the referrer and look for this site in the database
// Please note: HTTP_REFERER is not a typo
$referrer_url = $_SERVER['HTTP_REFERER'];
$siteData = find_site($referrer_url);

if ($siteData) {
// Seen the site before, so grab discount code
add_site_hit($referrer_url);
$discountCode = $siteData['discount_code'];
} else {
// A new site, so create a discount code and add it to the db
$discountCode = create_discount_code($refferer_url);
add_new_site($referrer_url, $discountCode);
}

// Image Creation code
[/php]

Sample code to grab the referring URL and find the site in the database.

create_discount_code would need to be tailored to each particular solution, and I’ve deliberately left out the database code to save space.

Image Creation Code

The final bit of code is creating our image, complete with embedded discount code. You’ll need the GD engine installed to use these functions. More information about that is available in the image section of the PHP manual.

The code itself is relatively simple. We open an advert image, and then add the discount code to it before outputting it to the browser.

[php]// Image Creation code

// Setup where we want the code to appear
$codeX = 5;
$codeY = 5;

// Load advert image.
$advertImage = imagecreatefrompng(’background-image.png’);

// Draw discount code
$textColour = imagecolorallocate($advertImage, 255, 255, 255);
imagestring($advertImage, 3, $codeX, $codeY, $discountCode, $textColour );

// Output our image and cleanup
header(’content-type: image/png’);
imagepng($advertImage);
imagedestroy($advertImage);[/php]

Code to create and output our image to the browser.

In case you’re wondering if creating an image works, here’s one I made earlier (using slightly modified code for the shadow and position).

Image created using dynamic script.

One of the advantages of using dynamically generated images is that you can modify the code to perform a split test if you wish to check ad wordings or appearances. That falls outside the scope of this article, but I may write about it in future.

All done

So there we have it – how to change an unpleasant situation into something that can benefit your business.

The code is still a little rough around the edges, so I’ll be cleaning it up to make it ready for release. My goal is to create something that is simple to install and use, which will take some work.

In case anybody’s wondering, I’m using “iG::Syntax Hiliter” to produce the highlighted code snippets.

* Not including bandwidth costs, but seeing as your bandwidth was already being stolen at least now you’re getting some value out of it.


These icons link to social bookmarking sites where readers can share and discover new web pages.

Article Info

Date: November 3, 2005

Author: Phil Newton


Categories:

Hot Game Downloads

Pirates of the Atlantic

Pirates of the Atlantic

Defend your base from marauding pirates!


Fairy Jewels Fairy Jewels
Enjoy a journey through a fantastic fairy adventure!
Mahjong Escape Mahjong Escape
Relax and unwind with Mahjong Escape.



No comments have been posted yet. Why not be the first?


Leave a comment [TrackBack URI]


Tags Allowed: a, abbr, acronym, b, blockquote, code, em, i, strike, strong

 


Browse By Date

Adventures in Shareware

Welcome to "Adventures in Shareware", the blog of an indie software business known only as "Sodaware". Learn all about indie games, the people that make them and how you can improve your own indie business.

Download Free Computer Games | Site Map | Links | Contact Us

© 2005-2007 Sodaware. All rights reserved. About Us | Privacy Policy