php广告加载类用法实例


Posted in PHP onSeptember 23, 2014

本文实例讲述了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 
    ), 
  ), 
 
); 
 
?>

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 ("<scr"+"ipt type='text/javascript' src='"+m3_u); 
        document.write ("?zoneid=" + ad.zoneId); 
        document.write ('&cb=' + m3_r); 
        if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used); 
        document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : '')); 
        document.write ("&loc=" + escape(window.location)); 
        if (document.referrer) document.write ("&referer=" + escape(document.referrer)); 
        if (document.context) document.write ("&context=" + escape(document.context)); 
        if (document.mmm_fo) document.write ("&mmm_fo=1"); 
        document.write ("'><\/scr"+"ipt>"); 
        break; 
       case 'url': 
        document.write ('<script type="text/javascript" src="' + ad.url + '"></script>'); 
        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 = /(<script[\s\S]*?<\/script>)/gim, 
    globalOptions = { 
      autoRelease: true, 
      parallel: true, 
      debug: false 
    }, 
    defaultOptions = { 
      charset: undefined, 
      success: undefined, 
      func: undefined, 
      src: undefined, 
      timeout: 3000 
    },publ, 
    head = document.getElementsByTagName("head")[0] || document.documentElement, 
    support = { 
      scriptOnloadTriggeredAccurately: false, 
      splitWithCapturingParentheses: ("abc".split(/(b)/)[1]==="b") 
    }; 
   
  function checkQueue () { 
    if(queue.length) { 
      loadScript( queue.shift() ); 
    } else if(loading === 0 && globalOptions.autoRelease) { 
      debug("Queue is empty. Auto-releasing."); 
      publ.release(); 
    } 
  } 
 
  function checkWriteBuffer (obj) { 
    var buffer = writeBuffer[obj.domId], 
      returnedEl; 
 
    if(buffer && buffer.length) { 
      writeHtml( buffer.shift(), obj ); 
 
    } else { 
      while (returnedElements.length > 0) { 
        returnedEl = returnedElements.pop(); 
        var id = returnedEl.id; 
        var elInDoc = getElementById(id); 
        if (!elInDoc) { continue; } 
        var parent = elInDoc.parentNode; 
        elInDoc.id = id + "__tmp"; 
        parent.insertBefore(returnedEl, elInDoc); 
        parent.removeChild(elInDoc); 
      } 
      finished(obj); 
    } 
  } 
 
  function debug (message, obj) { 
    if(!globalOptions.debug || !window.console) { return; } 
    var objExtra = ""; 
    if(obj) { 
      objExtra = "#"+obj.domId+" "; 
      var depth = obj.depth; 
      while(depth--) { objExtra += "  "; } 
    } 
    console.log("crapLoader " + objExtra + message); 
  } 
 
  function extend (t, s) { 
    var k; 
    if(!s) { return t; } 
    for(k in s) { 
      t[k] = s[k]; 
    } 
    return t; 
  } 
 
  function finished (obj) { 
    if(obj.success && typeof obj.success === "function") { 
      obj.success.call( document.getElementById(obj.domId) ); 
    } 
    checkQueue(); 
  } 
 
  function flush (obj) { 
    var domId = obj.domId, 
      outputFromScript, 
      htmlPartArray; 
 
    outputFromScript = stripNoScript( inputBuffer.join("") ); 
    inputBuffer = []; 
 
    htmlPartArray = separateScriptsFromHtml( outputFromScript ); 
 
    if(!writeBuffer[domId]) { 
      writeBuffer[domId] = htmlPartArray; 
    } else { 
      Array.prototype.unshift.apply(writeBuffer[domId], htmlPartArray); 
    } 
    checkWriteBuffer(obj); 
  } 
 
  function getCachedElById (domId) { 
    return elementCache[domId] || (elementCache[domId] = document.getElementById(domId)); 
  } 
 
  function getElementById (domId) { 
    return ( publ.orgGetElementById.call ? 
      publ.orgGetElementById.call(document, domId) : 
      publ.orgGetElementById(domId) ); 
  } 
 
  function getElementByIdReplacement (domId) { 
    var el = getElementById(domId), 
      html, frag, div, found; 
 
    function traverseForElById(domId, el) { 
      var children = el.children, i, l, child; 
      if(children && children.length) { 
        for(i=0,l=children.length; i<l; i++) { 
          child = children[i]; 
          if(child.id && child.id === domId) { return child; } 
          if(child.children && child.children.length) { 
            var tmp = traverseForElById(domId, child); 
            if (tmp) return tmp; 
          } 
        } 
      } 
    } 
 
    function searchForAlreadyReturnedEl(domId) { 
      var i, l, returnedEl; 
      for(i=0,l=returnedElements.length; i<l; i++) { 
        returnedEl = returnedElements[i]; 
        if(returnedEl.id === domId) { return returnedEl; } 
      } 
    } 
 
    if(el) { return el; } 
 
    if (returnedElements.length) { 
      found = searchForAlreadyReturnedEl(domId); 
      if (found) { 
        return found; 
      } 
    } 
 
    if(inputBuffer.length) { 
      html = inputBuffer.join(""); 
      frag = document.createDocumentFragment(); 
      div = document.createElement("div"); 
      div.innerHTML = html; 
      frag.appendChild(div); 
      found = traverseForElById(domId, div); 
      if (found) { 
        returnedElements.push(found); 
      } 
      return found; 
    } 
  } 
 
  var globalEval = (function () { 
    return (window.execScript ? function(code, language) { 
      window.execScript(code, language || "JavaScript"); 
    } : function(code, language) { 
      if(language && !/^javascript/i.test(language)) { return; } 
      window.eval.call(window, code); 
    }); 
  }()); 
 
  function isScript (html) { 
    return html.toLowerCase().indexOf("<script") === 0; 
  } 
 
  function runFunc (obj) { 
    obj.func(); 
    obj.depth++; 
    flush(obj); 
  } 
 
  function loadScript (obj) { 
    loading++; 
    // async loading code from jQuery 
    var script = document.createElement("script"); 
    if(obj.type) { script.type = obj.type; } 
    if(obj.charset) { script.charset = obj.charset; } 
    if(obj.language) { script.language = obj.language; } 
 
    logScript(obj); 
 
    var done = false; 
    // Attach handlers for all browsers 
    script.onload = script.onreadystatechange = function() { 
      loading--; 
      script.loaded = true; 
      if ( !done && (!this.readyState || 
          this.readyState === "loaded" || this.readyState === "complete") ) { 
        done = true; 
        script.onload = script.onreadystatechange = null; 
        debug("onload " + obj.src, obj); 
        flush(obj); 
      } 
    }; 
 
    script.loaded = false; 
    script.src = obj.src; 
    obj.depth++; 
 
    // Use insertBefore instead of appendChild to circumvent an IE6 bug. 
    // This arises when a base node is used (#2709 and #4378). 
    head.insertBefore( script, head.firstChild ); 
    setTimeout(function() { 
      if(!script.loaded) { throw new Error("SCRIPT NOT LOADED: " + script.src); } 
    }, obj.timeout); 
  } 
 
  function logScript (obj, code, lang) { 
    debug((code ? 
      "Inline " + lang + ": " + code.replace("\n", " ").substr(0, 30) + "..." : 
      "Inject " + obj.src), obj); 
  } 
 
  function separateScriptsFromHtml (htmlStr) { 
    return split(htmlStr, splitScriptsRegex); 
  } 
 
  function split (str, regexp) { 
    var match, prevIndex=0, tmp, result = [], i, l; 
 
    if(support.splitWithCapturingParentheses) { 
      tmp = str.split(regexp); 
    } else { 
      // Cross browser split technique from Steven Levithan 
      // http://blog.stevenlevithan.com/archives/cross-browser-split 
      tmp = []; 
      while(match = regexp.exec(str)) { 
        if(match.index > prevIndex) { 
          result.push(str.slice(prevIndex, match.index)); 
        } 
 
        if(match.length > 1 && match.index < str.length) { 
          Array.prototype.push.apply(tmp, match.slice(1)); 
        } 
 
        prevIndex = regexp.lastIndex; 
      } 
 
      if(prevIndex < str.length) { 
        tmp.push(str.slice(prevIndex)); 
      } 
 
    } 
 
    for(i=0, l=tmp.length; i<l; i=i+1) { 
      if(tmp[i]!=="") { result.push(tmp[i]); } 
    } 
    return result; 
  } 
 
  function stripNoScript (html) { 
    return html.replace(/<noscript>[\s\S]*?<\/noscript>/ig, ""); 
  } 
 
  function trim (str) { 
    if(!str) { return str; } 
    return str.replace(/^\s*|\s*$/gi, ""); 
  } 
 
  function writeHtml (html, obj) { 
    if( isScript(html) ) { 
      var dummy = document.createElement("div"); 
      dummy.innerHTML = "dummy<div>" + html + "</div>"; // trick for IE 
      var script = dummy.children[0].children[0]; 
      var lang = script.getAttribute("language") || "javascript"; 
      if(script.src) { 
        obj.src = script.src; 
        obj.charset = script.charset; 
        obj.language = lang; 
        obj.type = script.type; 
        loadScript(obj); 
      } else { 
        var code = trim( script.text ); 
        if(code) { 
          logScript( obj, code, lang); 
          globalEval( code, lang); 
        } 
        flush(obj); 
      } 
    } else { 
      var container = getCachedElById(obj.domId); 
      if(!container) { 
        throw new Error("crapLoader: Unable to inject html. Element with id '" + obj.domId + "' does not exist"); 
      } 
       
      html = trim(html); // newline before <object> cause weird effects in IE 
      if(html) { 
        container.innerHTML += html; 
      } 
      checkWriteBuffer(obj); 
    } 
  } 
 
  function writeReplacement (str) { 
    inputBuffer.push(str); 
    debug("write: " + str); 
  } 
 
  function openReplacement () { 
    // document.open() just returns the document when called from a blocking script: 
    // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-open 
    return document; 
  } 
 
  function closeReplacement () { 
    // document.close() does nothing when called from a blocking script: 
    // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-close 
  } 
 
  publ = { 
    hijack: function(options) { 
      if(isHijacked) { return; } 
      isHijacked = true; 
      extend(globalOptions, options); 
      if(globalOptions.parallel && !support.scriptOnloadTriggeredAccurately) { 
        globalOptions.parallel = false; 
        debug("Browsers onload is not reliable. Disabling parallel loading."); 
      } 
 
      document.write = document.writeln = writeReplacement; 
      document.open = openReplacement; 
      document.close = closeReplacement; 
      document.getElementById = getElementByIdReplacement; 
    }, 
 
    release: function() { 
      if(!isHijacked) { return; } 
      isHijacked = false; 
      document.write = this.orgWrite; 
      document.writeln = this.orgWriteLn; 
      document.open = this.orgOpen; 
      document.close = this.orgClose; 
      document.getElementById = this.orgGetElementById; 
      elementCache = {}; 
    }, 
 
    handle: function(options) { 
      if(!isHijacked) { 
        debug("Not in hijacked mode. Auto-hijacking."); 
        this.hijack(); 
      } 
      var defaultOptsCopy = extend({}, defaultOptions); 
      var obj = extend(defaultOptsCopy, options); 
      obj.depth = 0; 
 
      if (!obj.domId) { 
        obj.domId = "craploader_" + new Date().getTime(); 
        var span = document.createElement("span"); 
        span.id = obj.domId; 
        document.body.appendChild(span); 
      } 
 
      if (options.func) { 
        runFunc(obj); 
        return; 
      } 
 
      if(globalOptions.parallel) { 
        setTimeout(function() { 
          loadScript(obj); 
        }, 1); 
      } else { 
        queue.push(obj); 
        setTimeout(function() { 
          if(loading === 0) { 
            checkQueue(); 
          } 
        }, 1); 
      } 
    }, 
 
    loadScript: function(src, domId, options) { 
      if (typeof domId !== "string") { 
        options = domId; 
        domId = undefined; 
      } 
      this.handle(extend({ 
        src:  src, 
        domId: domId 
      }, options)); 
    }, 
 
    runFunc: function(func, domId, options) { 
      if (typeof domId !== "string") { 
        options = domId; 
        domId = undefined; 
      } 
      this.handle( extend({ 
        domId: domId, 
        func:   func 
      }, options) ); 
    }, 
 
    orgGetElementById  : document.getElementById, 
    orgWrite      : document.write, 
    orgWriteLn     : document.writeln, 
    orgOpen       : document.open, 
    orgClose      : document.close, 
    _olt        : 1, 
    _oltCallback    : function() { 
      support.scriptOnloadTriggeredAccurately = (publ._olt===2); 
    } 
  }; 
 
  return publ; 
}());

demo.php示例程序如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
 <head> 
 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
 <title> AD Loader </title> 
 <style type="text/css"> 
 .banner1{margin:10px; border:1px solid #CCCCCC; width:728px; height:90px;} 
 .banner2{margin:10px; border:1px solid #CCCCCC; width:300px; height:250px;} 
 </style> 
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
 </head> 
 
 <body> 
 <div class="banner1" id="ad_728x90"></div> 
 <div class="banner2" id="ad_300x250"></div> 
 <div class="banner2" id="ad_l2_300x250"></div> 
   
 <?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); // 同步加载 
 ?> 
 
 </body> 
</html>

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 type='text/javascript' src='"+m3_u); 
document.write ("?zoneid=" + zoneid); 
document.write ('&cb=' + m3_r); 
if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used); 
document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : '')); 
document.write ("&loc=" + escape(window.location)); 
if (document.referrer) document.write ("&referer=" + escape(document.referrer)); 
if (document.context) document.write ("&context=" + escape(document.context)); 
if (document.mmm_fo) document.write ("&mmm_fo=1"); 
document.write ("'><\/scr"+"ipt>"); 
<? } ?>

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

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

PHP 相关文章推荐
树型结构列出指定目录里所有文件的PHP类
Oct 09 PHP
如何开发一个虚拟域名系统
Oct 09 PHP
Ajax+PHP边学边练 之五 图片处理
Dec 03 PHP
php DOS攻击实现代码(附如何防范)
May 29 PHP
解析PHP可变函数的经典用法
Jun 20 PHP
分享下PHP register_globals 值为on与off的理解
Sep 26 PHP
php cli换行示例
Apr 22 PHP
codeigniter框架The URI you submitted has disallowed characters错误解决方法
May 06 PHP
PHP中trim()函数简单使用指南
Apr 16 PHP
php比较两个字符串长度的方法
Jul 13 PHP
PHP排序算法之希尔排序(Shell Sort)实例分析
Apr 20 PHP
laravel框架select2多选插件初始化默认选中项操作示例
Feb 18 PHP
php实现根据url自动生成缩略图的方法
Sep 23 #PHP
php实现的支持imagemagick及gd库两种处理的缩略图生成类
Sep 23 #PHP
PHP图片库imagemagick安装方法
Sep 23 #PHP
安装ImageMagick出现error while loading shared libraries的解决方法
Sep 23 #PHP
php实现的支持断点续传的文件下载类
Sep 23 #PHP
php实现的click captcha点击验证码类实例
Sep 23 #PHP
PHP实现自动登入google play下载app report的方法
Sep 23 #PHP
You might like
PHP堆栈调试操作简单示例
2018/06/15 PHP
解决在Laravel 中处理OPTIONS请求的问题
2019/10/11 PHP
jQuery的一些特性和用法整理小结
2010/01/13 Javascript
file模式访问网页时iframe高度自适应解决方案
2013/01/16 Javascript
关于JS字符串函数String.replace()
2013/04/07 Javascript
jquery导航制件jquery鼠标经过变色效果示例
2013/12/05 Javascript
动态加载脚本提升javascript性能
2014/02/24 Javascript
jquery无法设置checkbox选中即没有变成选中状态
2014/03/27 Javascript
javascript中实现兼容JAVA的hashCode算法代码分享
2020/08/11 Javascript
学习JavaScript设计模式(单例模式)
2015/11/26 Javascript
使用Javascript判断浏览器终端设备(PC、IOS(iphone)、Android)
2017/01/04 Javascript
Vue-cli创建项目从单页面到多页面的方法
2017/09/20 Javascript
webpack4 处理CSS的方法示例
2018/09/03 Javascript
jQuery操作元素追加内容示例
2020/01/10 jQuery
使用React-Router实现前端路由鉴权的示例代码
2020/07/26 Javascript
[05:31]DOTA2上海特级锦标赛主赛事第三日RECAP
2016/03/05 DOTA
[01:13:08]2018DOTA2亚洲邀请赛4.6 淘汰赛 mineski vs LGD 第二场
2018/04/10 DOTA
[49:21]TNC vs VG 2019DOTA2国际邀请赛淘汰赛 胜者组赛BO3 第三场 8.20.mp4
2019/08/22 DOTA
Python设计模式之单例模式实例
2014/04/26 Python
Python 正则表达式实现计算器功能
2017/04/29 Python
python安装numpy&amp;安装matplotlib&amp; scipy的教程
2017/11/02 Python
书单|人生苦短,你还不用python!
2017/12/29 Python
使用requests库制作Python爬虫
2018/03/25 Python
pygame游戏之旅 如何制作游戏障碍
2018/11/20 Python
Django框架模板文件使用及模板文件加载顺序分析
2019/05/23 Python
python ImageDraw类实现几何图形的绘制与文字的绘制
2020/02/26 Python
django模型动态修改参数,增加 filter 字段的方式
2020/03/16 Python
python matplotlib.pyplot.plot()参数用法
2020/04/14 Python
websocket+sockjs+stompjs详解及实例代码
2018/11/30 HTML / CSS
SmartBuyGlasses德国:购买太阳镜和眼镜
2019/08/20 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
第二批党的群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
公司副总经理岗位职责
2014/10/01 职场文书
赢在执行观后感
2015/06/16 职场文书
浅谈Redis的几个过期策略
2021/05/27 Redis
java后台调用接口及处理跨域问题的解决
2022/03/24 Java/Android