PHP Forums Archive

Show Random Advertisements

Tags: adsense, rotate, random, banner

Olaf posted on 2009-09-21 14:16:52 #

Use this code to rotate multiple banners on some web page. It's possible to use multiple graphical banners and also some code from Google Adsense.

The following php is used to create the banner array: (place the code above your html code)

<?php
$random[] = '<a href="http://www.all4yourwebsite.com/" title="Website templates, dreamweaver templates, flash intro templates"><img src="images/banners/all4webBanner468_60.jpg" alt="all4yourWebsite.com - web design templates" /></a>';

$random[] = '<a href="http://www.designers-website.eu/" title="Premium Web Design Templates"><img src="/images/banners/designers_website_468_60.jpg" alt="Designers Website" /></a>';

// ad more banners if you like

$random[] = '<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_page_url = document.location;
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';

$i = rand(0, count($random)-1);
?>

Don't forget to change the publisher ID

google_ad_client = \"pub-xxxxxxxxxxx\";

the following code is not standard and should be user if you use the banner snippet within an Iframe:

google_page_url = document.location;

Use this short code inside the websites body:

<?php echo $random[$i]; ?>

Comments / discussions

fl00r posted on 2009-09-21 16:36:40 #

Great, just what I was looking for!

One question though: I copy/paste the whole above code into the page. It works but the page (footer.php) becomes very messy and cluttered. Is there a way to prevent this?

Olaf posted on 2009-09-21 18:18:48 #

The code from this snippet has to go into a new file. I use a similar script for the 468*60 banner on the homepage:

I use that code to include the iframe:

<iframe src="/for_adsense.php" width="468" height="60" scrolling="no" marginwidth="0" marginheight="0" frameborder="0">
</iframe>

The file "for_adsense.php" has the code for the banner.

fl00r posted on 2009-09-21 19:14:38 #

I placed this code into random.php

<?php
$random[] = '<a rel="nofollow" href="http://www.spanjemarktplaats.nl/rubriek.asp?rub_id=24" title="SpanjeMarktplaats"><img src="http://204.12.19.206/bb-templates/inove/images/spm_sub_overnames.jpg" alt="SpanjeMarktplaats" /></a>';

$random[] = '<a rel="nofollow" href="http://www.spanjemarktplaats.nl/rubriek.asp?rub_id=1" title="SpanjeMarktplaats"><img src="http://204.12.19.206/bb-templates/inove/images/spm_sub_vacatures.jpg" alt="SpanjeMarktplaats" /></a>';

// ad more banners if you like
/*
$random[] = '<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_page_url = document.location;
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
*/

$i = rand(0, count($random)-1);
?>

And placed this code in the page:

<!-- random banner START -->
		<div class="widget">
        <iframe src="http://204.12.19.206/bb-templates/inove/random.php" width="270" height="99" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
		</div>
        <!-- random banner STOP -->

But it doesn't show the banners.
There must be someting stupids I forgot?

Olaf posted on 2009-09-21 19:19:04 #

you need to add at the end of random.php

echo $random[$i];

fl00r posted on 2009-09-21 19:41:42 #

Last thing: why is there a blue border on the top and left side?

Olaf posted on 2009-09-21 19:53:29 #

I guess you need to set the border to "0" for the banner images (default html for link elements)

fl00r posted on 2009-09-21 20:33:45 #

Yes, you are right.... as always!

Thank you so much for your time,

Ben