Hack61.Build an Ad Redirector


Hack 61. 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 6-27 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.

6.12.1. The Code

Save the code in Example 6-32 as index.php.

Figure 6-27. The ad redirector page flow


Example 6-32. 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 Example 6-33 as redir.php.

Example 6-33. 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 Example 6-34 as article1.html.

Example 6-34. The first article
 <html> <head><title>Article one</title></head> <body> Article one </body> </html> 

Save the code in Example 6-35 as article2.html.

Example 6-35. The second article
 <html> <head><title>Article two</title></head> <body> Article two </body> </html> 

Save the code in Example 6-36 as article3.html.

Example 6-36. The third article
 <html> <head><title>Article three</title></head> <body> Article three </body> </html> 

6.12.2. Running the Hack

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

Figure 6-28. 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 6-29.

Figure 6-29. 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 6-30.

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.

Figure 6-30. The article page




PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net