Build an Ad Redirector





Build an Ad Redirector

Add the ability for your site to serve up ads on a random basis between link clicks.

Content sites, like the IGN gaming site (http://www.ign.com/) have revenues based on serving ads. If you click on an article link, you might get the article, or you might get an ad page. The ad page has both the ad and a link to the requested article (so that you can manually go to the requested page). The ad page will also automatically forward you to your article if you let it sit for a few seconds.

I have to admit that I thought twice about writing and including this hack, because I don't like this behavior all that much. But I figured I would let you decide for yourself. It's sort of like the Anarchist's Cookbook; just because there is a book on how to make a bomb doesn't mean you have to make one for yourself.


The illustration in Figure shows the relationship among the pages in the ad redirector system. All of the links on the index.php page go to the redir.php redirector page. Based on a random value, the redir.php page decides whether you will stay there and watch an ad or be sent to the originally requested article.

The Code

Save the code in Figure as index.php.

The ad redirector page flow


The home page with article links
<?php
function redir_link( $url, $text )
{
?>
<a href="redir.php?url=<?php echo( $url ); ?>"><?php echo( $text ); ?></a>
<?php
}
?>
<html>
<body>
Here are my articles:<br/><br/>
<?php redir_link( 'article1.html', 'Article one' ); ?><br/>
<?php redir_link( 'article2.html', 'Article two' ); ?><br/>
<?php redir_link( 'article3.html', 'Article three' ); ?><br/>
</body>
</html>

Save the code in Figure as redir.php.

The ad redirector
<?php
srand(time());
if ( mt_rand(0,10) < 7 )
{
	header( "Location: ".$_GET['url'] );
	exit; 
} 
?> 
<html> 
<head> 
<script language="Javascript">
function redir() 
{
  window.location='<?php echo( $_GET['url'] ); ?>'; 
} 
function startTimer() 
{
  window.setTimeout( "redir();", 2000 ); 
} 
</script> 
</head> 
<body onload="startTimer()"> 
Here is my groovy ad. You can continue onto the article 
<a href="<?php echo( $_GET['url'] ); ?>">here</a>. Or just 
wait for a couple of seconds. 
</body> 
</html>

Save the code in Figure as article1.html.

The first article
<html>
<head><title>Article one</title></head>
<body>
Article one
</body>
</html>

Save the code in Figure as article2.html.

The second article
<html>
<head><title>Article two</title></head>
<body>
Article two
</body>
</html>

Save the code in Figure as article3.html.

The third article
<html>
<head><title>Article three</title></head>
<body>
Article three
</body>
</html>

Running the Hack

Upload the files to the PHP server and navigate your browser to index.php. You should see something like Figure.

The home page with the article links


Click on the link for "Article two," which is actually a link to redir.php. Sometimes redir.php will send you to an ad page, as shown in Figure.

The ad page with the timer and the link to the article


There is a direct link to the article if people want to skip through the ad. Additionally, there's a timer on the page that, once expired, triggers a redirection to the user's target page.

In cases where the random generator doesn't cause an ad to appear, users go directly to the article, as shown in Figure.

There are several ways to implement this ad redirector mechanism. With the implementation shown here, the user can still link directly to articles by referencing the redirection page and supplying the exact URL of the target page. If you want the ads to appear even on direct links, you can invert this mechanism by putting a check at the top of every page in your application, randomly redirecting users to an ad instead of displaying requested articles or pages.

The article page




 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows