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.
It's a great script but if you look at google TOS, it's against the rules to modify the actual code...
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.
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.
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 > )
<? // 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.
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.
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.
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...
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
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?
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.
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.
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.
I find it perfectly right.. as long as the final code displayed is exactly like the original code.. what say???