O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Tip

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.

The Code

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

The ad redirector page flow

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( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required