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:
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.
<?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;
ad.php next to your main script.<?php @include __DIR__.'/ad.php'; ?><?php ad_banner('header'); ?>data-ad-skip to it.