首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 廣告載入類 ADLoader:支援同步/非同步、OpenX 整合、瀏覽器相容

     編輯:PHP 相關知識

PHP 廣告載入類 ADLoader 完整實作

ADLoader 解決頁面多廣告位載入阻塞問題,支援非同步載入(預設)與同步載入(舊版 IE 強制),整合 OpenX 廣告系統,提供完整前後端解決方案。

核心特色

  • 非同步載入:不阻塞頁面渲染,依序間隔載入廣告
  • 同步降級:偵測 IE 自動切換同步模式
  • OpenX/URL 双模式支援
  • Zone ID Base64 編碼傳遞(非同步模式)
  • 完整前端 JS 處理邏輯 (ADLoader.js + crapLoader)

後端核心類別 (ADLoader.class.php)

<?php
/** 廣告載入管理類
 * Date: 2013-08-04
 * Author: fdipzone
 * Ver: 1.0
 */
class ADLoader {
    private static $_ads = array();
    private static $_step = 300;      // 載入間隔 ms
    private static $_async = true;    // 非同步模式
    private static $_config = array();
    private static $_jsclass = null;

    /** 載入廣告集合
     * @param String $channel 管道/版位代碼
     * @param Int $step 載入間隔
     * @param Boolean $async 是否非同步
     * @return String HTML/JS 代碼
     */
    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();
    }

    /** 設定配置 */
    public static function setConfig($config = array(), $jsclass = '') {
        self::$_config = $config;
        self::$_jsclass = $jsclass;
    }

    /** 取得廣告陣列 */
    private static function getAds($channel = '') {
        $cfg = self::$_config;
        self::$_ads = ($cfg && isset($cfg[$channel])) ? $cfg[$channel] : $cfg['default'];
    }

    /** Zone ID Base64 編碼 */
    private static function genZoneId() {
        if (!self::$_async) return;
        foreach (self::$_ads as &$ad) {
            if (isset($ad['zoneId'])) {
                $ad['zoneId'] = base64_encode('var zoneid=' . $ad['zoneId'] . ';');
            }
        }
    }

    /** 產生輸出 HTML */
    private static function genHtml() {
        $ads = self::$_ads;
        $html = array();
        if (self::$_jsclass && $ads) {
            $html[] = '<script type="text/javascript" src="' . self::$_jsclass . '"></script>';

            if (!self::$_async) {
                // 同步模式預載
                foreach ($ads as $ad) {
                    $html[] = '<div id="' . $ad['domId'] . '_container">';
                    $html[] = '<script type="text/javascript">ADLoader.preload(' . json_encode($ad) . ');</script>';
                    $html[] = '</div>';
                }
            }

            $html[] = '<script type="text/javascript">';
            $html[] = 'var ads=' . json_encode($ads) . ';';
            $html[] = '$(document).ready(function(){ ADLoader.load(ads, ' . self::$_step . ', ' . intval(self::$_async) . '); });';
            $html[] = '</script>';
        }
        return implode("\r\n", $html);
    }

    /** 偵測需強制同步的瀏覽器 */
    private static function checkBrowser() {
        $ua = $_SERVER['HTTP_USER_AGENT'];
        return strstr($ua, 'MSIE') === false;
    }
}
?>

廣告設定檔 (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),
    ),
);

前端使用範例 (demo.php)

<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<title>AD Loader Demo</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.banner1{margin:10px;border:1px solid #ccc;width:728px;height:90px;}
.banner2{margin:10px;border:1px solid #ccc;width:300px;height:250px;}
</style>
</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>

關鍵前端元件

  • ADLoader.js:管理廣告載入序列、間隔、非同步/同步切換
  • crapLoader:處理 document.write 攔截、Script 注入、HTML 片段渲染的核心庫(支援 OpenX 等傳統廣告代碼)

適用場景

  • 頁面多廣告位,需避免載入阻塞
  • 整合 OpenX / Google Ad Manager 等廣告系統
  • 需支援舊版 IE 相容性
  • 廣告代碼使用 document.write 且需非同步化

現代替代方案

新專案建議使用 Google Publisher Tags (GPT) 或 Prebid.js,提供更完善的非同步載入、Header Bidding、延遲載入、視圖可見度監測等功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved