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

PHP 廣告加載類:支援異步與同步模式的完整實現

     編輯:PHP 綜合知識

本文介紹一個功能完善的 PHP 廣告加載類 ADLoader,支援異步與同步兩種加載模式,並能根據瀏覽器特性自動切換策略,依賴 jQuery 庫實現前端渲染。

核心功能特性

  • 支援多頻道廣告配置管理
  • 可配置廣告加載間隔時間
  • 智能檢測瀏覽器,IE 強制使用同步模式
  • Zone ID 進行 Base64 編碼保護
  • 自動生成包含廣告數據與加載邏輯的 HTML/JS 代碼

核心類實現:ADLoader.class.php

<?php
/**
 * 廣告加載管理類
 * Date: 2013-08-04
 * Author: fdipzone
 * Ver: 1.0
 */
class ADLoader {
    private static $_ads = array();        // 廣告集合
    private static $_step = 300;           // 廣告加載間隔(毫秒)
    private static $_async = true;         // 是否異步加載
    private static $_config = array();     // 廣告配置
    private static $_jsclass = null;       // 廣告 JS 類文件路徑

    /**
     * 加載廣告集合
     * @param string $channel 頻道名,對應配置文件
     * @param int $step 加載間隔
     * @param bool $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 = '') {
        $AD_Config = self::$_config;
        if ($AD_Config != null) {
            self::$_ads = isset($AD_Config[$channel]) ? $AD_Config[$channel] : $AD_Config['default'];
        }
    }

    /** Zone ID Base64 編碼處理 */
    private static function genZoneId() {
        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/JS 代碼 */
    private static function genHtml() {
        $ads = self::$_ads;
        $html = array();

        if (self::$_jsclass != null && $ads) {
            // 引入廣告 JS 類
            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') !== false) {
            return false;
        }
        return true;
    }
}
?>

廣告配置文件: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),
    ),
);
?>

使用示例

<?php
require_once 'ADLoader.class.php';
require_once 'ADConfig.php';

// 設置配置
ADLoader::setConfig($config, '/js/ADLoader.js');

// 加載廣告 (頻道: case_openx, 間隔: 500ms, 異步模式)
echo ADLoader::load('case_openx', 500, true);
?>

設計亮點

  1. 靜態成員設計:所有屬性與方法均為靜態,無需實例化即可使用,適合全局廣告管理。
  2. 瀏覽器適配:自動檢測 IE 瀏覽器並降級為同步模式,確保兼容性。
  3. 安全性考量:Zone ID 經 Base64 編碼輸出,避免直接暴露廣告位 ID。
  4. 配置分離:廣告配置獨立於邏輯代碼,便於維護與擴展。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved