龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php广告加载类用法实例

时间:2014-09-25 11:45来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了php广告加载类用法实例,采用jQuery技术可实现异步与同步加载,具有非常广泛的实用价值,需要的朋友可以参考下

本文实例讲述了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, '<script type="text/javascript" src="'.self::$_jsclass.'"></script>'); 
 
      // 同步需要预先加载 
      if(!self::$_async){ 
        foreach($ads as $ad){ 
          array_push($html, '<div id="'.$ad['domId'].'_container" style="display:none">'); 
          array_push($html, '<script type="text/javascript">'); 
          array_push($html, 'ADLoader.preload('.json_encode($ad).');'); 
          array_push($html, '</script>'); 
          array_push($html, '</div>'); 
        } 
      } 
 
      array_push($html, '<script type="text/javascript">'); 
      array_push($html, 'var ads='.json_encode($ads).';'); 
      array_push($html, '$(document).ready(function(){ ADLoader.load(ads, '.self::$_step.', '.intval(self::$_async).'); });'); 
      array_push($html, '</script>'); 
    } 
    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 
    ), 
  ), 
 
); 
 
?>

精彩图集

赞助商链接