GIDNetwork > AdSense ad_client id dynamically
« Windows XP64 Professional Setting the "no_adsense_for_me" cookie »

AdSense ad_client id dynamically

by: JdS - May 27, 2005

Since posting AdSense, now you can click them on your site, the 2 custom PHP functions I wrote initially, turned into a few more than just the 2 as I tested and refined each, over the last few days.

Still, these functions allow me (the webmaster) to view AdSense ads on my websites with the google_ad_client id set to "ca-test", simply based on a special cookie that I set for myself.

I have also written this collection of custom PHP functions assuming every other webmaster reading this someday, is like me, i.e. the webmaster is managing many domains on a web server and "including" it on each site from just one place.

For this reason alone, I have kept the domain specific & cookie setting information separate and off this file. Information like that is set and managed inside the individual (domain) cookie setting files.

I have also considered people who may want to extend/add to the collection. This is why the main function below is named adsense_content_code() where "content" is for Google AdSense for Content code. In case you decide to write one to handle Google AdSense for Search for example, you could simply create a new, similar function and name it adsense_search_code()!

Similarly all private functions that are unique to each type of ad code, contain the description in their function names e.g. note the "content" in _adsense_content_default_values().

Enough with the background "overview". Here's the complete script. If you decide to use/test it, copy and paste the example code and save it to a PHP file and filename it I have mine saved to a central location on my web server, say... /home/jds/shared/functions/


PHP Code Example:

 * Functions used to dynamically generate AdSense javascript code.
 * Functions used to dynamically generate AdSense for Content
 * javascript code.
 * @author       J de Silva                             <>
 * @copyright    Copyright &copy; 2005, J de Silva
 * @link    AdSense ad_client id dynamically

// ---------------------------------------------
// Set the following variables where appropriate
// ---------------------------------------------
    //                                        The default value for "google_ad_client".
    $_adsense['google_ad_client']        =    'pub-0000000000000000';
    //                                        A simple password. Required to access the cookie setting web page.
    $_adsense['password_to_set_cookie']  =    'P4Ssw0Rd';
    //                                        The NAME of the cookie that a siteowner sets for him/herself.
    $_adsense['siteowner_cookiename']    =    'no_adsense_for_me';
// ---------------------------------------------

/********** NOTHING TO EDIT BELOW **********/

function adsense_content_code$v )
    // loads an array of DEFAULT values as backup.
    $dvs        =    _adsense_content_default_values();
    $v          =    array_merge$dvs$v );
    // this is where "google_ad_client" id is reset to "ca-test" if the siteowner's cookie is found.
    _adsense_reset_client_id_for_siteowner$v );
    unset( $dvs );
        "<script type="text/javascript"><!--\n" .
        _adsense_adtest_line$v['ad_client'] ) .
        "google_ad_client = "$v[ad_client]";\n" .
        _adsense_alternate_line$v ) .
        "google_ad_width = $v[ad_width];\n" .
        "google_ad_height = $v[ad_height];\n" .
        "google_ad_format = "$v[ad_format]";\n" .
        "google_ad_channel ="$v[ad_channel]";\n" .
        "google_color_border = "$v[color_border]";\n" .
        "google_color_bg = "$v[color_bg]";\n" .
        "google_color_link = "$v[color_link]";\n" .
        "google_color_url = "$v[color_url]";\n" .
        "google_color_text = "$v[color_text]";\n" .
        "//--></script>\n" .
        "<script type="text/javascript"\n" .
        "  src="">\n" .
        "</script>\n" ;

/********** PRIVATE FUNCTIONS **********/

function _adsense_adtest_line( &$ad_client_id )
    if( $ad_client_id === 'ca-test' )
        return( "google_adtest = "on";\n" );
    return( null );

function _adsense_alternate_line( &$v )
    if( isset($v['alternate_color']) )
        return( "google_alternate_color = "{$v['alternate_color']}";\n" );
    if( isset($v['alternate_ad_url']) )
        return( "google_alternate_ad_url = "{$v['alternate_ad_url']}";\n" );
    return null;        

function _adsense_content_default_values()
    // verify that the $_adsense['google_ad_client'] variable above, was set.
    $verified false;
    if( isset($GLOBALS['_adsense']['google_ad_client']) )
        if( $GLOBALS['_adsense']['google_ad_client'] )
            if( $GLOBALS['_adsense']['google_ad_client'] !== 'pub-0000000000000000' )
                $verified true;
    if( !$verified )
        trigger_error"The default AdSense GOOGLE_AD_CLIENT id was not set."E_USER_ERROR );
            'ad_client'     =>    $GLOBALS['_adsense']['google_ad_client'],
            'ad_width'      =>    468,
            'ad_height'     =>    60,
            'ad_format'     =>    '468x60_as',
            'ad_type'       =>    'text',
            'ad_channel'    =>    null,
            'color_border'  =>    '336699',
            'color_bg'      =>    'FFFFFF',
            'color_link'    =>    '0000FF',
            'color_url'     =>    '008000',
            'color_text'    =>    '000000'

function _adsense_reset_client_id_for_siteowner( &$v )
    if( isset( $_COOKIE$GLOBALS['_adsense']['siteowner_cookiename'] ] ) )
        $v['ad_client']     = 'ca-test';
        $v['ad_channel']    = null;

In case you decide to try this out...

The first thing you need to do is to edit the following values where it asks you to:

PHP Code Example:


// ---------------------------------------------
// Set the following variables where appropriate
// ---------------------------------------------
    //                                        The default value for "google_ad_client".
    $_adsense['google_ad_client']        =    'pub-0000000000000000';
    //                                        A simple password. Required to access the cookie setting web page.
    $_adsense['password_to_set_cookie']  =    'P4Ssw0Rd';
    //                                        The NAME of the cookie that a siteowner sets for him/herself.
    $_adsense['siteowner_cookiename']    =    'no_adsense_for_me';
// ---------------------------------------------

// ...

The $_adsense['password_to_set_cookie'] variable and value, is used to access the cookie setting page for each domain that you manage. It's not the most secure way to limit access to a private page such as this but I figured it's better than nothing - so extend it into something more robust if that is what you'd prefer to do.

Example use

There is only one function that i need to use to generate the AdSense for Content javascript codes inside any web page or script i.e. the adsense_content_code() function.

Let's say I wanted to insert one 728x90 (text & image) and another 250x250 (text only) AdSense ad unit in my index.php page here. This is probably how I am going to do it:

PHP Code Example:

 * FILENAME: index.php

// existing lines of script to generate the page content...

// include the adsense functions file
include_once( '/home/jds/shared/functions/' );

// the 728x90 AdSense ad-unit
$as[0]['ad_client']        = 'pub-0000000000000000'// optional
$as[0]['ad_width']         = 728;
$as[0]['ad_height']        = 90;
$as[0]['ad_format']        = "728x90_as";
$as[0]['ad_type']          = "text_image";
$as[0]['alternate_ad_url'] = "";
echo adsense_content_code$as[0] );

// some other existing code...

// the 250x250 AdSense ad-unit, this time with channel info too
$as[1]['ad_client']        = 'pub-0000000000000000'// optional
$as[1]['ad_channel']       = '0000000000';
$as[1]['ad_width']         = 250;
$as[1]['ad_height']        = 250;
$as[1]['ad_format']        = "250x250_as";
$as[1]['ad_type']          = "text";
$as[1]['alternate_ad_url'] = "";
echo adsense_content_code$as[1] );

// the rest of my web page / script...

If you decide to use these functions, please remember this one very important thing. You must always set the following 3 values in an array before submitting it to the adsense_content_code() function:
ad_width | ad_height | ad_format
everything else is optional.

Would you like to comment? This story has been viewed 14,176 times.
« Windows XP64 Professional Setting the "no_adsense_for_me" cookie »


Copyright © GIDNetwork™ 2001 - 2018

Another website by J de Silva

Page generated in : 0.01470 sec.