欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php广告加载类用法实例,php加载用法实例

程序员文章站 2022-06-08 22:38:34
...

php广告加载类用法实例,php加载用法实例

本文实例讲述了php广告加载类的用法,非常实用。分享给大家供大家参考。具体方法如下:

该php广告加载类,支持异步与同步加载。需要使用Jquery实现。

ADLoader.class.php类文件如下:

0){ 
      self::$_step = $step; 
    } 
 
    if(isset($async) && is_bool($async)){ 
      self::$_async = $async; 
    } 
 
    // 判断浏览器,如IE强制使用同步加载 
    if(!self::checkBrowser()){ 
      self::$_async = false; 
    } 
 
    self::getAds($channel); 
    self::genZoneId(); 
 
    return self::genHtml(); 
  } 
 
  /** 设置config 
  * @param String $config 广告配置 
  * @param String $jsclass js class文件路径 
  */ 
  public static function setConfig($config=array(), $jsclass=''){ 
    self::$_config = $config; 
    self::$_jsclass = $jsclass; 
  } 
 
 
  /** 根据channel创建广告集合 
  * @param String $channel 栏目 
  */ 
  private static function getAds($channel=''){ 
    $AD_Config = self::$_config; 
    if($AD_Config!=null){ 
      self::$_ads = isset($AD_Config[$channel])? $AD_Config[$channel] : $AD_Config['default']; 
    } 
  } 
 
  /** zoneid base64_encode 处理 */ 
  private static function genZoneId(){ 
 
    // 同步加载广告不需要处理zoneid 
    if(!self::$_async){ 
      return ; 
    } 
 
    $ads = self::$_ads; 
    for($i=0,$len=count($ads); $i'); 
 
      // 同步需要预先加载 
      if(!self::$_async){ 
        foreach($ads as $ad){ 
          array_push($html, ''); 
        } 
      } 
 
      array_push($html, ''); 
    } 
    return implode("\r\n", $html); 
  } 
 
  /** 判断是否需要强制同步加载的浏览器 */ 
  private static function checkBrowser(){ 
    $user_agent = $_SERVER['HTTP_USER_AGENT']; 
    if(strstr($user_agent,'MSIE')!=''){ 
      return false; 
    } 
    return true; 
  } 
 
} // class end 
?>

ADConfig.php文件如下:

 array( 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_728x90', 
      'zoneId' => 452 
    ), 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_300x250', 
      'zoneId' => 449 
    ), 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_l2_300x250', 
      'zoneId' => 394 
    ), 
  ), 
 
  'case_url' => array( 
    array( 
      'type' => 'url', 
      'domId' => 'ad_728x90', 
      'url' => 'adurl.php?zoneid=452' 
    ), 
    array( 
      'type' => 'url', 
      'domId' => 'ad_300x250', 
      'url' => 'adurl.php?zoneid=449' 
    ), 
    array( 
      'type' => 'url', 
      'domId' => 'ad_l2_300x250', 
      'url' => 'adurl.php?zoneid=394' 
    ) 
  ), 
 
  'case_sync_openx' => array( 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_728x90', 
      'zoneId' => 452 
    ), 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_300x250', 
      'zoneId' => 449 
    ), 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_l2_300x250', 
      'zoneId' => 394 
    ), 
  ), 
 
  'default' => array( 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_728x90', 
      'zoneId' => 452 
    ), 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_300x250', 
      'zoneId' => 449 
    ), 
    array( 
      'type' => 'openx', 
      'domId' => 'ad_l2_300x250', 
      'zoneId' => 394 
    ), 
  ), 
 
); 
 
?>

ADLoader.js文件如下:

/** 异步加载广告 
*  Date:  2013-08-04 
*  Author: fdipzone 
*  Ver:  1.0 
*/ 
var ADLoader = (function(){ 
 
  var _ads = [],   // 广告集合 
    _step = 300,  // 广告加载间隔 
    _async = true, // 是否异步加载 
    _loaded = 0;  // 已经加载的广告数 
   
  /** loadAd 循环加载广告 
  * @param int c 第几个广告 
  */ 
  function loadAD(c){ 
    if(_loaded>=_ads.length){ 
      return ; 
    } 
 
    if($('#'+_ads[c].domId).length>0){ // 判断dom是否存在 
 
      if(_async){ // 异步执行 
 
        crapLoader.loadScript(getScript(_ads[c]), _ads[c].domId, { 
          success: function(){ 
            completeAd(); 
          } 
        }); 
       
      }else{ // 将同步加载的广告显示 
 
        var ad_container = $('#'+_ads[c].domId+'_container'); 
        ad_container.find('embed').attr('wmode','transparent').end().find('object').each(function(k, v){ 
          v.wmode = 'transparent'; // 将flash变透明 
        }); 
        $('#'+_ads[c].domId)[0].appendChild(ad_container[0]); 
        ad_container.show(); 
         
        completeAd(); 
      } 
    }else{ // dom不存在 
      completeAd(); 
    } 
  } 
 
  /** 加载完广告后处理 */ 
  function completeAd(){ 
    _loaded ++; 
    setTimeout(function(){ 
      loadAD(_loaded); 
    }, _step);     
  } 
 
  /** 获取广告 
  * @param Array ad 广告参数 
  */ 
  function getScript(ad){ 
    var ret = null; 
 
    switch(ad.type){ 
      case 'openx': // openx code ad 
        ret = 'data:text/javascript;base64,' + ad.zoneId + 'dmFyIG0zX3UgPSAobG9jYXRpb24ucHJvdG9jb2w9PSdodHRwczonPydodHRwczovL2Fkcy5ubWcuY29tLmhrL3d3dy9kZWxpdmVyeS9hanMucGhwJzonaHR0cDovL2Fkcy5ubWcuY29tLmhrL3d3dy9kZWxpdmVyeS9hanMucGhwJyk7CnZhciBtM19yID0gTWF0aC5mbG9vcihNYXRoLnJhbmRvbSgpKjk5OTk5OTk5OTk5KTsKaWYgKCFkb2N1bWVudC5NQVhfdXNlZCkgZG9jdW1lbnQuTUFYX3VzZWQgPSAnLCc7CmRvY3VtZW50LndyaXRlICgiPHNjciIrImlwdCB0eXBlPSd0ZXh0L2phdmFzY3JpcHQnIHNyYz0nIittM191KTsKZG9jdW1lbnQud3JpdGUgKCI/em9uZWlkPSIgKyB6b25laWQpOwpkb2N1bWVudC53cml0ZSAoJyZhbXA7Y2I9JyArIG0zX3IpOwppZiAoZG9jdW1lbnQuTUFYX3VzZWQgIT0gJywnKSBkb2N1bWVudC53cml0ZSAoIiZhbXA7ZXhjbHVkZT0iICsgZG9jdW1lbnQuTUFYX3VzZWQpOwpkb2N1bWVudC53cml0ZSAoZG9jdW1lbnQuY2hhcnNldCA/ICcmYW1wO2NoYXJzZXQ9Jytkb2N1bWVudC5jaGFyc2V0IDogKGRvY3VtZW50LmNoYXJhY3RlclNldCA/ICcmYW1wO2NoYXJzZXQ9Jytkb2N1bWVudC5jaGFyYWN0ZXJTZXQgOiAnJykpOwpkb2N1bWVudC53cml0ZSAoIiZhbXA7bG9jPSIgKyBlc2NhcGUod2luZG93LmxvY2F0aW9uKSk7CmlmIChkb2N1bWVudC5yZWZlcnJlcikgZG9jdW1lbnQud3JpdGUgKCImYW1wO3JlZmVyZXI9IiArIGVzY2FwZShkb2N1bWVudC5yZWZlcnJlcikpOwppZiAoZG9jdW1lbnQuY29udGV4dCkgZG9jdW1lbnQud3JpdGUgKCImY29udGV4dD0iICsgZXNjYXBlKGRvY3VtZW50LmNvbnRleHQpKTsKaWYgKGRvY3VtZW50Lm1tbV9mbykgZG9jdW1lbnQud3JpdGUgKCImYW1wO21tbV9mbz0xIik7CmRvY3VtZW50LndyaXRlICgiJz48XC9zY3IiKyJpcHQ+Iik7'; 
        break; 
       
      case 'url': // url ad 
        ret = ad.url; 
        break; 
    } 
    return ret; 
  } 
 
  /** 同步加载广告 
  * @param Array ad 广告参数 
  */ 
  function writeAd(ad){ 
    switch(ad.type){ 
      case 'openx': 
        var m3_u = (location.protocol=='https:'?'https://ads.nmg.com.hk/www/delivery/ajs.php':'http://ads.nmg.com.hk/www/delivery/ajs.php'); 
        var m3_r = Math.floor(Math.random()*99999999999); 
        if (!document.MAX_used) document.MAX_used = ','; 
        document.write (""); 
        break; 
       case 'url': 
        document.write (''); 
        break; 
    } 
  } 
 
  obj = { 
 
    /** 加载广告 
    * @param Array  ads  广告集合 
    * @param int   step 广告加载间隔 
    * @param boolean async true:异步加载 false:同步加载 
    */ 
    load: function(ads, step, async){ 
      _ads = ads; 
 
      if(typeof(step)!='undefined'){ 
        _step = step; 
      } 
 
      if(typeof(async)!='undefined'){ 
        _async = async; 
      } 
 
      loadAD(_loaded); 
    }, 
 
    /** 预加载广告 */ 
    preload: function(ad){ 
      if($('#'+ad.domId).length>0){  // 判断dom是否存在 
        writeAd(ad); 
      } 
    } 
  } 
  return obj; 
 
}()); 
 
/* crapLoader */ 
var crapLoader = (function() { 
   
  var isHijacked = false, 
    queue = [], 
    inputBuffer = [], 
    writeBuffer = {}, 
    loading = 0, 
    elementCache = {}, 
    returnedElements = [], 
    splitScriptsRegex = /(