โš™
ad.php Configurator
Monetization setup tool

What this script does

The configurator builds a ready-to-use PHP file ad.php โ€” a lightweight module for monetizing traffic on your website. You set offers (links), display rules, banners, and exclusions, and get a single file that you simply include via include.

Why you need it:

  • Offer redirect โ€” a visitor is automatically redirected to a random affiliate link (with a delay and repeat-show protection).
  • Targeting โ€” separate link pools for desktop, mobile, and languages (ru, en, de, etc.).
  • Click hijack โ€” a click on empty space opens the ad in a new tab without breaking navigation on links and buttons.
  • Banners โ€” up to 5 positions (header, in_content, sidebar, footer, popup) with an image, link, or raw HTML.
  • Exclusions โ€” do not redirect search bots, do not show again for N hours, ignore traffic from specified referrers.

Why it's useful: the module needs no frameworks or databases, runs on any PHP host, plugs easily into an existing template, and gives you flexible control over monetization without editing your core site code.

๐Ÿ”— Redirect links
Fallback pool of offers. One URL per line (or comma-separated). One is picked at random per visit. Use this for anything that doesn't need device or language targeting.
If empty โ€” general links are used.
If empty โ€” general links are used.
Format: lang|url1,url2 โ€” one language per line.
Example: ru|https://ru1.com,https://ru2.com
If a language is detected and has its own pool โ€” that pool is used first, then the device pool, then the general pool.
๐ŸŒ Language targeting
Comma-separated 2-letter codes. Empty โ€” all allowed.
Fallback if detection fails.
Detection order: ?lang=xx โ†’ cookie โ†’ Accept-Language header โ†’ domain TLD โ†’ default. Result saved in a cookie for 30 days.
โšก Behavior
0 โ€” never auto-redirect. Max 600.
0 โ€” off. Max 8760 (1 year).
๐Ÿšซ Exclusions
One per line. If the incoming referrer contains this substring โ€” no redirect. Example: google.com, yandex.ru
๐Ÿ–ผ Banners
Positions: header top, in_content article, sidebar side, footer bottom, popup modal.

โœ… Your ad.php is ready

6816 bytes ยท 196 lines
<?php

$AD_LINKS          = [
    'https://example.com/offer1',
    'https://example.com/offer2',
];
$AD_LINKS_PC       = [];
$AD_LINKS_MOBILE   = [];
$AD_LINKS_BY_LANG  = [];

$ALLOWED_LANGS     = [];
$DEFAULT_LANG      = 'en';

$REDIRECT_DELAY    = 5;
$CLICK_HIJACK      = true;
$AUTO_REDIRECT     = true;
$REDIRECT_BOTS     = false;
$EXCLUDE_REFERERS  = [];
$COOKIE_HOURS      = 0;
$SHOW_PRELOADER    = true;
$PRELOADER_TEXT    = 'Loading...';

$AD_BANNERS_ENABLE = false;
$AD_BANNERS        = [];

function ad_is_mobile() {
    $ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
    $m = ['Android','iPhone','iPad','iPod','Windows Phone',
          'BlackBerry','Opera Mini','IEMobile','Mobile','webOS'];
    foreach ($m as $a) if (stripos($ua, $a) !== false) return true;
    return false;
}

function ad_is_bot() {
    $ua = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
    $b = ['bot','crawler','spider','yandex','google','bing','baidu',
          'duckduck','facebookexternalhit','telegrambot','whatsapp',
          'slurp','semrush','ahrefs','mj12','dotbot'];
    foreach ($b as $x) if (strpos($ua, $x) !== false) return true;
    return false;
}

function ad_detect_lang() {
    global $DEFAULT_LANG;
    if (!empty($_GET['lang']) && preg_match('/^[a-z]{2}$/i', $_GET['lang'])) {
        return strtolower($_GET['lang']);
    }
    if (!empty($_COOKIE['ad_lang']) && preg_match('/^[a-z]{2}$/i', $_COOKIE['ad_lang'])) {
        return strtolower($_COOKIE['ad_lang']);
    }
    $al = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
    if ($al && preg_match_all('/([a-z]{2})(?:-[a-z]{2})?(?:;q=([0-9.]+))?/i', $al, $m, PREG_SET_ORDER)) {
        foreach ($m as $row) {
            $code = strtolower($row[1]);
            if (!in_array($code, ['und','xx'])) return $code;
        }
    }
    $host = $_SERVER['HTTP_HOST'] ?? '';
    $parts = explode('.', $host);
    $tld = strtolower(end($parts));
    $tld_map = ['ru'=>'ru','ua'=>'uk','by'=>'be','kz'=>'kk','de'=>'de',
                'fr'=>'fr','it'=>'it','es'=>'es','pl'=>'pl','tr'=>'tr',
                'jp'=>'ja','cn'=>'zh','br'=>'pt','mx'=>'es'];
    if (isset($tld_map[$tld])) return $tld_map[$tld];
    return $DEFAULT_LANG;
}

function ad_pick_link($list) {
    if (empty($list)) return '';
    return $list[array_rand($list)];
}

function ad_banner($position, $force = false) {
    global $AD_BANNERS, $AD_BANNERS_ENABLE;
    if (!$force && !$AD_BANNERS_ENABLE) return;
    if (empty($AD_BANNERS[$position])) return;

    $b = $AD_BANNERS[$position];

    if (!empty($b['extra'])) {
        echo $b['extra'];
        return;
    }

    $img   = $b['img']   ?? '';
    $href  = $b['href']  ?? '';
    $alt   = $b['alt']   ?? '';
    $width = (int)($b['width'] ?? 0);

    if ($img === '') return;

    $style = $width > 0
        ? ' style="max-width:' . $width . 'px;width:100%;height:auto;display:block;margin:0 auto;"'
        : ' style="max-width:100%;height:auto;display:block;margin:0 auto;"';
    $imgTag = '<img src="' . htmlspecialchars($img, ENT_QUOTES) . '" alt="' . htmlspecialchars($alt, ENT_QUOTES) . '"' . $style . ' loading="lazy">';

    if ($href !== '') {
        $imgTag = '<a href="' . htmlspecialchars($href, ENT_QUOTES) . '" target="_blank" rel="noopener nofollow">' . $imgTag . '</a>';
    }

    echo '<div class="ad-banner ad-banner-' . htmlspecialchars($position, ENT_QUOTES) . '" style="text-align:center;margin:12px 0;">' . $imgTag . '</div>';
}

$ad_is_mobile = ad_is_mobile();
$ad_is_bot    = ad_is_bot();
$ad_lang      = ad_detect_lang();

$ad_skip = false;

if (!empty($ALLOWED_LANGS) && !in_array($ad_lang, $ALLOWED_LANGS)) {
    $ad_skip = true;
}

if (!isset($_COOKIE['ad_lang'])) {
    @setcookie('ad_lang', $ad_lang, time() + 30*86400, '/');
}

$ad_do_redirect = false;
$target_url = '';

if (!$ad_skip) {
    $pool = [];
    if (!empty($AD_LINKS_BY_LANG[$ad_lang])) {
        $pool = $AD_LINKS_BY_LANG[$ad_lang];
    } elseif ($ad_is_mobile && !empty($AD_LINKS_MOBILE)) {
        $pool = $AD_LINKS_MOBILE;
    } elseif (!$ad_is_mobile && !empty($AD_LINKS_PC)) {
        $pool = $AD_LINKS_PC;
    } else {
        $pool = $AD_LINKS;
    }
    $target_url = ad_pick_link($pool);

    $blocked = false;
    foreach ($EXCLUDE_REFERERS as $ref) {
        if (!empty($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], $ref) !== false) {
            $blocked = true; break;
        }
    }
    if ($ad_is_bot && !$REDIRECT_BOTS) $blocked = true;
    if ($COOKIE_HOURS > 0 && isset($_COOKIE['ad_shown'])) $blocked = true;

    if ($AUTO_REDIRECT && !$blocked && $target_url !== '') {
        $ad_do_redirect = true;
        if ($COOKIE_HOURS > 0) {
            setcookie('ad_shown', '1', time() + $COOKIE_HOURS * 3600, '/');
        }
    }
}

if ($ad_do_redirect):
    ?>
    <?php if ($SHOW_PRELOADER): ?>
    <div id="ad-preloader" style="position:fixed;top:0;left:0;right:0;z-index:999999;
        background:rgba(15,23,42,.92);color:#e6edf7;
        display:flex;align-items:center;justify-content:center;gap:10px;
        padding:8px 14px;font:13px/1.3 system-ui,sans-serif;
        box-shadow:0 2px 10px rgba(0,0,0,.3);">
        <span style="width:14px;height:14px;border:2px solid #4f8cff;border-top-color:transparent;
            border-radius:50%;display:inline-block;animation:ad-spin .8s linear infinite;"></span>
        <span><?= htmlspecialchars($PRELOADER_TEXT, ENT_QUOTES, 'UTF-8') ?></span>
    </div>
    <style>@keyframes ad-spin{to{transform:rotate(360deg)}}</style>
    <?php endif; ?>

    <script>
    (function() {
        var targetUrl   = <?= json_encode($target_url, JSON_UNESCAPED_SLASHES) ?>;
        var delay       = <?= (int)$REDIRECT_DELAY ?> * 1000;
        var clickHijack = <?= $CLICK_HIJACK ? 'true' : 'false' ?>;
        var redirecting = false;

        function go() {
            if (redirecting) return;
            redirecting = true;
            try { window.location.href = targetUrl; } catch(e) {}
        }

        if (delay > 0) setTimeout(go, delay);

        if (clickHijack) {
            function handler(e) {
                if (e.target && e.target.closest &&
                    e.target.closest('[data-ad-skip], a, button, input, textarea, select, label')) return;
                e.preventDefault();
                e.stopPropagation();
                window.open(targetUrl, '_blank', 'noopener,noreferrer');
                setTimeout(go, 100);
            }
            document.addEventListener('click', handler, true);
            document.addEventListener('touchstart', handler, {capture:true, passive:false});
        }
    })();
    </script>
    <?php
endif;
How to install:
1. Save the code as ad.php next to your main script.
2. Include it in your header: <?php @include __DIR__.'/ad.php'; ?>
3. Place banners where you want them: <?php ad_banner('header'); ?>
5. To exclude an element from click-hijack โ€” add data-ad-skip to it.