Sep 12 2005
Jeremy Schoemaker

Code to rotate easily between adsense and ypn

By Jeremy Schoemaker 20 comments

A few webmasters have recently been talking about rotating google and yahoo ads on the same page. I wrote a VERY simple php script to do this. You just need 2 text files. One of them called adsense.txt which contains your adsense code and the other called YPN.txt which contains your YPN code. The reason you need to include the txt files is because if you put the code directly into the script you would have to excape the “’s and therefore violate TOS by altering the code. Just to be safe the example below shows you how to do it with includes.


< ?php
$number=mt_rand(1, 2);

if ($number==”1″) {

include “adsense.txt”; #include the text file with adsense code

}
else

{
include “YPN.txt”; #include the text file with ypn code

}
?>

thats it!

  1. Tony said on September 20th, 2005 at 8:13 am

    This is a great idea and a great piece of code, thanks!

    [Reply]

  2. Steve said on September 28th, 2005 at 2:57 am

    This is one way to rotate ads but it is also random, therefore you do not know how many Y ads vs G ads are being shown. You could also rotate using any “drop”. Set a cookie for that user and serve ads based on that users cookie, you could drop the current var into a DB and use it globally, each client getting whichever is next Y or G… I prefer the cookie as it should be more measureable as the split is a true 50/50

    Hope that makes sense… it’s late.

    [Reply]

  3. Kathryn said on November 2nd, 2005 at 7:23 am

    Nice simple way to rotate the Adsense and Yahoo YPN code on the same page of your site! Thanks, much.

    [Reply]

  4. Ovisopa said on November 2nd, 2005 at 3:12 pm

    Nice code :)
    For all those who are using diferent ads sizes (probably all of ya) you can use this version :

    …..
    ….

    hope this helps …

    see ya

    [Reply]

  5. jimmy said on November 2nd, 2005 at 10:35 pm

    please give similar code for ASP pages

    [Reply]

  6. Ovisopa said on November 3rd, 2005 at 2:22 am

    Admin -> please delete the previovous post .. It didn’t work using

    here is the link with an txt file :

    http://www.supercarnews.net/test/rotate.txt

    [Reply]

  7. Richard said on November 3rd, 2005 at 9:09 pm

    Great idea, I have tried the code it works, excellent
    but how do you get it to work in the HTML page at the exact position?
    Can you help
    Regards
    Richard

    [Reply]

  8. Marocain said on November 6th, 2005 at 12:00 pm

    thanks a lot !

    [Reply]

  9. Ravi Dronamraju said on November 24th, 2005 at 12:47 pm

    This does not seem to work for me. Browser caching always seems to show the wrong ad!

    [Reply]

  10. bob said on November 26th, 2005 at 8:54 pm

    -meta http-equiv=”Pragma” content=”no-cache”-
    – Pragma content set to no-cache tells the browser not to cache the page
    This may or may not work in IE –
    or
    -meta http-equiv=”expires” content=”0″-
    – Setting the page to expire at 0 means the page is immediately expired
    Any vales less then one will set the page to expire some time in past and
    not be cached. This may not work with Navigator –

    I could not post the actual code here because of posting limitations. You can find no cache code via a google search.

    [Reply]

  11. Angsuman Chakraborty said on December 20th, 2005 at 12:18 am

    I have an AdRotator plugin for WordPress for quite sometime which allows you to randomly choose between several ads/provider. You you could use it to rotate between say YPN,AdSense, Chitika and other. All it requires from you is adding the ad codes to a text file.

    [Reply]

  12. Tony said on December 20th, 2005 at 6:49 am

    Looks interesting. Thanks for sharing.

    [Reply]

  13. Steve said on April 10th, 2006 at 11:05 pm

    Hi All. Help me please with my MSIE!
    In my title bar instead of “Microsoft Internet Explorer” a title appears as an advertisement about a site i visited
    “visit http://www.site353535.com and register for free”. What can I do to restore my browser? And why on this site:
    http://singles-sex-clubs.swingersdate.net (meet single woman)
    I constantly see “cannot resolve host name”? Waiting for your reply.

    [Reply]

  14. Michael Z. said on May 6th, 2006 at 3:21 am

    I’m very, very impressed that this sort of work is being done; Web Design is getting stagnant with people using just styled
    block-level elements to produce artwork. The incorporation of SVG into sites excites me a lot.
    How long do you expect it will take for this sort of technology to be widespread?
    Obviously you can only speak about WebKit realistically, but if it’s going to take ten years for IE Win to gain (full) support,
    we can’t design with it.
    I’m amused by the “Becoming more important” line in the first paragraph. This has been a HUGE problem for years -
    ever since HTML-2.0 was introduced to be more of a layout language and less of a markup language. For an example,
    you just have to look at this site. adults-dot.com Why is all the text
    crammed over on the left side of the page with a big blank space on the right side?
    Why is the default font tiny and unreadable? Fortunately most browsers now let you override the latter problem.

    [Reply]

  15. eh said on January 21st, 2007 at 1:30 pm

    That code is not good:
    if ($number==”1″) {
    should be:
    if ($number==1) {

    It ain’t no string…

    [Reply]

  16. matiasn said on August 5th, 2007 at 2:12 pm

    shoe shoe, would it be too much trouble asking on how can we make it with 3 or more included txts ?.

    thanks in advance :)

    [Reply]

  17. Harish said on October 9th, 2008 at 4:45 am

    Thanx for the code Jeremy.Good post

    [Reply]

  18. JenSense - Making Sense of Contextual Advertising said on October 4th, 2005 at 12:17 pm

    Rotating AdSense & YPN

    Since I began testing AdSense versus Yahoo Publisher Network, many people have asked how they can test themselves by rotating the two on their own sites. Here are three ways you can do this. SeRoundtable has step-by-step instructions on using…

  19. Iggy Uncensored said on November 23rd, 2005 at 10:26 pm

    Using Blogware to rotate between Yahoo! Publisher Network and Google AdSense

    Blogware support has stated that I couldn’t implement rotation of these contextual advertisements do to the way their service works. This wasn’t a big surprise to me. Since all the instructions I’ve seen indicate that you would have to be running or…

  20. [...] OK - I have a few PM’s asking me which network is good and which is bad. I don’t want to call anyone out, especially since I make decent money from them both. It has reinforced to me that poor ROI is not necessarily a sign of poor conversion, just poor tracking. This is exactly the reason we try and measure everything and test even the most obscure stuff. I would however reccomend testing the networks with a simple script for redirects. I adapted the one I use from a post Shoemoney made on his blog. (ask him for support not me lol) [...]

What do you think? Join the discussion...

How do I change my avatar?

Go to gravatar.com and upload your preferred avatar.