Multi-Site Banner Managing – Advertising Displayed Based On Domain


Did you recently take my advice on manging banners? Or maybe you’re just putting advertisements on your site for the first time this month? Either way you’ll findĀ as your sites grow, its a lot of work to manage banner advertisements. You could go with something like OpenAds but applications like this are taxing on server efficiency – particularly if you’re on a shared hosting plan (The newest versin of OpenAds utilizes 16mb of ram). Sometimes simple is better and effective! Here’s some PHP coding tips to set yourself up for banner automation.

The first step involves grabbing the current pages domain. Break the url on the slashes like so. This should go in your template page.
$parts = explode(“/”, $HTTP_HOST);

This creates an array which we can now pick and choose an element to access. The first array “0″ is the first part of the array. This contains everything between the http:// and the start of the next slash if there is one. We’re going to store that in variable “$whichsite”. NOTE: You can apply these same rules to directories or pages within a site by incrementing the part variable. This should go your template page.
$whichsite = $parts[0];

Now use an includes in the spot on your page (probably in a template somewhere on your site) where you want the ad to appear. This again, goes in your sites template.

Create an IF statement to check and see what domain is being selected. NOTE: if its not a sub-domain you’ will want to enter its WWW counterpart since a site can usually be accessed by or
if($whichsite == “” || $whichsite == “”)
If there are any additional links you want to have the same condition for continue to add them by separating them with a “||”. This specifies an OR statement in PHP. This code should go in a new document named “ads.php” which we called in the past step. Your ads should go in-between each if statement accordingly.

The code in your template should look like this:

  1. <?php if($whichsite == "" || $whichsite == "") { ?>
  2. //Replace this with banners for this site
  3. <?php } elseif($whichsite == "") { ?>
  4. //Banners code for
  5. <?php } else { ?>
  6. //Catch all banner (if you use this add in a non-applicable spot) it will display this code) You could use this alone as a substitue for if you are only doing this on 2 sites.
  7. <?php } ?>

And your ads.php script for your advertisements should look something like this:

  1. <?php
  2. $parts = explode("/", $HTTP_HOST);
  3. $whichsite = $parts[0];
  4. include "$whichsite";
  5. ?>

If you’re starting to learn PHP/mySQL I would strongly suggest an activity that would involve setting up a database which tracks banner loads. You could also set up a simple system that manages the domains and ads while you’re at it. Break the steps down and use this as a learning tool. The only way I ever learned is when I had an excuse to create something or had a problem that needed fixing. The possibilities are endless as you can apply this concept to things other then banner advertising! Be creative with it!

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.

Leave a Reply

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois