PHP Code to display randomly sized AdSense blocks.

Discussion in 'AdSense' started by JamesColin, Apr 25, 2006.

  1. #1
    Here's how to create a table with two columns, one holds an adsense block and the other one an image. The size is not fixed and will be displayed at random, it's just to avoid quick ad blindness for new visitors and make some changes for regular visitors.

    
    // create an array with the sizes you want (beware the last array() must not have a comma at the end)
    $mygoogleads = array (
    	array(468,60),
    	array(125,125),
    	array(234,60),
    	array(180,150),
    	array(120,240),
    	array(300,250),
    	array(250,250),
    	array(336,280)
    );
    // sort the array in random order
    shuffle($mygoogleads); 
    // and simply take the first one (the first one begins at 0, second is 1, etc.)
    $mygoogleadwidth = $mygoogleads[0][0];
    $mygoogleadheight = $mygoogleads[0][1];
    // create the adsense code
    $mygooglead = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
    <tr><td width=\"{$mygoogleadwidth}\"><script type=\"text/javascript\"><!--
    google_ad_client = \"pub-1234567890\";
    google_alternate_ad_url = \"http://www.yoursite.com/images/alternate_ad_{$mygoogleadwidth}x{$mygoogleadheight}.jpg\";
    google_ad_width = \"$mygoogleadwidth\";
    google_ad_height = \"$mygoogleadheight\";
    google_ad_format = \"{$mygoogleadwidth}x{$mygoogleadheight}_as\";
    google_ad_type = \"text_image\";
    google_ad_channel =\"1234567890\";
    google_color_border = \"FFFFFF\";
    google_color_bg = \"FFFFFF\";
    google_color_link = \"0000FF\";
    google_color_url = \"000000\";
    google_color_text = \"000000\";
    // --></script><script type=\"text/javascript\"
      src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
    </script></td><td width=\"5\"><img src=\"http://www.yoursite.com/images/the-stretched-image.jpg\" width=\"5\" height=\"{$mygoogleadheight}\" border=\"0\" /></td></tr></table>
    ";
    
    //display the code where you want in your page
    echo $mygooglead;
    
    PHP:
    Note that you can make the same with colors, or that you can add channel information in your array, like this array(468,60,"87474738738") and use this to fill in the google_ad_channel in order to see which format has the highest CTR over a period of time.

    In the code I use google_alternate_ad_url with an image url, I made as many images as I have adsense block formats in my array, I give them the name alternate_ad_WIDTHxHEIGHT.jpg but you can simply use a color or the collapsing ad unit code given by google.

    At last, I've put the HTML code in the $mygooglead variable, this is not necessary if you just want to output the adsense code right where you write your PHP, but in my case I find it useful to have the code in a variable.

    If you want more information ask here.
     
    JamesColin, Apr 25, 2006 IP
    jackburton2006 likes this.
  2. jackburton2006

    jackburton2006 Peon

    Messages:
    5,296
    Likes Received:
    282
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Good heads up! I don't use this technique myself, but I'm sure it'll help others.
     
    jackburton2006, Apr 25, 2006 IP
  3. tomzx

    tomzx Peon

    Messages:
    385
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's a great script but if you look at google TOS, it's against the rules to modify the actual code...
     
    tomzx, Apr 25, 2006 IP
  4. Dio

    Dio Well-Known Member

    Messages:
    725
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    120
    #4
    I think it would be safer to put the AdSense code untouched in an include maybe, and call different includes rather than fiddle with the source code.
     
    Dio, Apr 25, 2006 IP
  5. JamesColin

    JamesColin Prominent Member

    Messages:
    7,874
    Likes Received:
    164
    Best Answers:
    1
    Trophy Points:
    395
    Digital Goods:
    1
    #5
    The code is not different from Google's, it is untouched. It's just changing the size, that's it really. It's exactly the same as having many codes in include files and switching them, but I think it is more compact that way. The resulting HTML code is the same as what you'd get from AdSense's account wizard.
     
    JamesColin, Apr 25, 2006 IP
  6. Dio

    Dio Well-Known Member

    Messages:
    725
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    120
    #6
    I'd check it with them just to be on the safe side. I tend to err on the side of over caution. :)
     
    Dio, Apr 25, 2006 IP
  7. tomzx

    tomzx Peon

    Messages:
    385
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I agree with Dio. Better make multiple include file or a file with multiple references, but I wouldn't touch the actual code (start from < and end at > ;))
     
    tomzx, Apr 25, 2006 IP
  8. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    <?
    // create an array with the sizes you want (beware the last array() must not have a comma at the end)
    $mygoogleads = array (
        array(468,60),
        array(125,125),
        array(234,60),
        array(180,150),
        array(120,240),
        array(300,250),
        array(250,250),
        array(336,280)
    );
    // sort the array in random order
    shuffle($mygoogleads);
    // and simply take the first one (the first one begins at 0, second is 1, etc.)
    $mygoogleadwidth = $mygoogleads[0][0];
    $mygoogleadheight = $mygoogleads[0][1];
    
    $googlecode = <<<CODE
    <script type="text/javascript"><!--
    google_ad_client = "pub-1234567890";
    google_alternate_ad_url = "http://www.yoursite.com/images/alternate_ad_{$mygoogleadwidth}x{$mygoogleadheight}.jpg";
    google_ad_width = "$mygoogleadwidth";
    google_ad_height = "$mygoogleadheight";
    google_ad_format = "{$mygoogleadwidth}x{$mygoogleadheight}_as";
    google_ad_type = "text_image";
    google_ad_channel ="1234567890";
    google_color_border = "FFFFFF";
    google_color_bg = "FFFFFF";
    google_color_link = "0000FF";
    google_color_url = "000000";
    google_color_text = "000000";
    // --></script><script type="text/javascript"
      src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    CODE;
    
    // create the adsense code
    $mygooglead = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">".
    							"<tr><td width=\"{$mygoogleadwidth}\">".$googlecode."</td>".
    							"<td width=\"5\"><img src=\"http://www.yoursite.com/images/the-stretched-image.jpg\" width=\"5\" height=\"{$mygoogleadheight}\" border=\"0\" /></td></tr></table>";
    //display the code where you want in your page
    echo $mygooglead; 
    ?>
    
    PHP:
    This basically does the same thing but it's easier to understand/paste Google code.
     
    mariush, Apr 25, 2006 IP
  9. Troutnut

    Troutnut Peon

    Messages:
    76
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I do something like that, except I switch colors rather than formats. I use untouched code pasted from Google's interface, and I keep every version stored in a database. It's much easier than messing with include files.
     
    Troutnut, Apr 25, 2006 IP
  10. toomuch72

    toomuch72 Peon

    Messages:
    165
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I agree this is Edited code(and against TOS).
    Wether the finished product that goes out to your guest is edited or not does not matter but it is quite possibel to do the same exact thing with out ever touching the code. Nice part about PHP it only sends out from your server what it needs to show so what may look like alot of code does not use alot of bandwidth.

    Here is a quick example or rotated ads without touching the code:
    Where you see ****PUT ADSENSE CODE HERE*** simply paste the different sized codes you have copied and pasted from the adsense website.

    <?$randnumber=rand(1,8);
    if ($randnumber==1){?>****PUT ADSENSE CODE HERE468by60***<?}
    if ($randnumber==2){?>****PUT ADSENSE CODE HERE728by90***<?}
    if ($randnumber==4){?>****PUT ADSENSE CODE HEREanother size***<?}
    if ($randnumber==5){?>****PUT ADSENSE CODE HEREyet another***<?}
    if ($randnumber==6){?>****PUT ADSENSE CODE HEREget the pic yet***<?}
    if ($randnumber==7){?>****PUT ADSENSE CODE HERE***<?}
    if ($randnumber==8){?>****PUT ADSENSE CODE HERE***<?}?>

    It will look pretty lenghty when you are done but it does the trick without violating the TOS.
     
    toomuch72, Apr 25, 2006 IP
  11. fablex

    fablex Peon

    Messages:
    468
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #11
    if the output of the php script is exactly the same as if you would see if the code was just copy+paste in the page, it isnt against the TOS right? so, you can make a small php script to change the sizes of it i guess, and make an include then where the size has to come...
     
    fablex, Apr 26, 2006 IP
  12. Sonu2007

    Sonu2007 Banned

    Messages:
    467
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    thx
    nice info.
     
    Sonu2007, Apr 26, 2006 IP
  13. JamesColin

    JamesColin Prominent Member

    Messages:
    7,874
    Likes Received:
    164
    Best Answers:
    1
    Trophy Points:
    395
    Digital Goods:
    1
    #13
    You're welcome Prajyot

    Thank you mariush, your changes make the code much more readable!

    fablex: that what I was saying but the message didn't pass :)
     
    JamesColin, Apr 26, 2006 IP
  14. toomuch72

    toomuch72 Peon

    Messages:
    165
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #14
    No I would say that is false the program policies clearly states
    That means wether the end result is the same as the orginal code or not any alterations prior to it being delivered to the end user is a policy violation. It is so easy to write code that does not change the code in any way prior to pasting it.

    Will google ever notice? Quite possibly if there is ever an error with your host and the PHP module goes down then the code will come out in the garbled state. I'm sure Google will take notice at this time because it is a major change.

    Will Google ever take action if they notice? Prolly not. But do you want to risk it? Why? When you don't have to?
     
    toomuch72, Apr 29, 2006 IP
  15. Troutnut

    Troutnut Peon

    Messages:
    76
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #15
    That could happen anyway if the connection to the host is broken and only part of the code gets sent. I would imagine Google has to overlook that happening on occasion.

    The way I do it with a database, I don't always pass the code entirely unmodified, either. I got permission to put the line google_adtest='on'; in there, and my script does that whenever I have my site in "testing mode" or whenever the ads are being served to myself.
     
    Troutnut, Apr 29, 2006 IP
  16. profs77

    profs77 Well-Known Member

    Messages:
    441
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    118
    #16
    Is that <<<code code; stuff PHP 5.0?
     
    profs77, Apr 29, 2006 IP
  17. JamesColin

    JamesColin Prominent Member

    Messages:
    7,874
    Likes Received:
    164
    Best Answers:
    1
    Trophy Points:
    395
    Digital Goods:
    1
    #17
    I don't think the <<<code part is PHP5 only, it's another way to fill in a variable with having to escape (adding backslash in front of) every quote in your string.
    Note that you don't need to write CODE, you can write any word, as long as you use the same word (with ; at the end) to end the variable filling.
     
    JamesColin, Apr 29, 2006 IP
  18. shareaway

    shareaway Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    what kind of increase in earning do people see when they vary adsense blocks or colors?
     
    shareaway, Apr 29, 2006 IP
  19. JamesColin

    JamesColin Prominent Member

    Messages:
    7,874
    Likes Received:
    164
    Best Answers:
    1
    Trophy Points:
    395
    Digital Goods:
    1
    #19
    I didn't do any study on that, so I cannot tell you if it is an increase or a decrease in earnings. I guess a non changing ad placement would perform better if it is optimised for CTR and nothing can beat it.
     
    JamesColin, Apr 29, 2006 IP
  20. chandubhai

    chandubhai Banned

    Messages:
    556
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I find it perfectly right.. as long as the final code displayed is exactly like the original code.. what say???
     
    chandubhai, Apr 29, 2006 IP