当前位置:Gxlcms > PHP教程 > php广告加载类用法实例_PHP

php广告加载类用法实例_PHP

时间:2021-07-01 10:21:17 帮助过:2人阅读

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

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

ADLoader.class.php类文件如下:

<?php 
/** 广告加载管理类 
*  Date:  2013-08-04 
*  Author: fdipzone 
*  Ver:  1.0 
* 
*  Func: 
*  public load     加载广告集合 
*  public setConfig  广告配置 
*  private getAds    根据channel创建广告集合 
*  private genZoneId  zoneid base64_encode 处理 
*  private genHtml   生成广告html 
*  private checkBrowser 检查是否需要同步加载的浏览器 
*/ 
 
class ADLoader{ // class start 
 
  private static $_ads = array();   // 广告集合 
  private static $_step = 300;    // 广告加载间隔 
  private static $_async = true;   // 是否异步加载 
  private static $_config = array(); // 广告设置文件 
  private static $_jsclass = null;  // 广告JS class 
 
 
  /** 加载广告集合 
  * @param String $channel 栏目,对应config文件 
  * @param int   $step  广告加载间隔 
  * @param boolean $async  是否异步加载 
  */ 
  public static function load($channel='', $step='', $async=''){ 
    if(isset($step) && is_numeric($step) && $step>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<$len; $i++){ 
      if(isset($ads[$i]['zoneId'])){ 
        $ads[$i]['zoneId'] = base64_encode('var zoneid='.$ads[$i]['zoneId'].';'); 
      } 
    } 
    self::$_ads = $ads; 
  } 
 
  /** 生成广告html */ 
  private static function genHtml(){ 
    $ads = self::$_ads; 
    $html = array(); 
    if(self::$_jsclass!=null && $ads){ 
      array_push($html, ''); 
 
      // 同步需要预先加载 
      if(!self::$_async){ 
        foreach($ads as $ad){ 
          array_push($html, ''); 
          array_push($html, ''); 
          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文件如下:

<?php 
/** 广告配置文件 **/ 
 
return array( 
 
  'case_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 
    ), 
  ), 
 
  '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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gKGRvY3VtZW50Lm1tbV9mbykgZG9jdW1lbnQud3JpdGUgKCImYW1wO21tbV9mbz0xIik7CmRvY3VtZW50LndyaXRlICgiJz48XC9zY3IiKyJpcHQ+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 ("<\/scr"+"ipt>"); 
        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 = /( 
  
 
  
  
  
  
   
 <?php 
  function showAD($channel='', $step='', $async=''){ 
   include('ADLoader.class.php'); 
   $ad_config = include('ADConfig.php'); 
   ADLoader::setConfig($ad_config, 'ADLoader.js'); 
   return ADLoader::load($channel, $step, $async); 
  } 
 
  echo showAD('case_openx'); // 异步加载 
  //echo showAD('case_url');  // url方式异步加载 
  //echo showAD('case_sync_openx', 300, false); // 同步加载 
 ?> 
 
  


adurl.php文件如下:

<?php
$zoneid = isset($_GET['zoneid'])? intval($_GET['zoneid']) : 0; 
if($zoneid){ 
?> 
var zoneid = <?=$zoneid ?>; 
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 ("<\/scr"+"ipt>"); 
<? } ?>

本文所述完整实例源码点击此处本站下载。

希望本文所述对大家的php程序设计有所帮助。

人气教程排行