PHP Ad Rotator (File-Based, No DB)
Here is a lightweight PHP ad rotator you can drop into any website to rotate banner or text ads on each page load. Perfect for affiliate links, sponsor slots, internal promos, or “house ads” — without needing a database.
What it does:
- Rotates ads on every refresh (or per visit) by choosing one ad from a list.
- Supports weighted rotation (show some ads more often than others).
- Optional “no-repeat” behavior (doesn’t show the same ad twice in a row).
- Outputs clean HTML you can style with your existing CSS.
Install:
- Create:
/includes/ads.php - Paste the script below into it.
- Wherever you want an ad slot, include:
<?php require __DIR__ . '/includes/ads.php'; echo render_ad_slot('sidebar'); ?>
Tip: Make multiple slots (header, sidebar, inline) by calling render_ad_slot() with different slot names.
<?php
declare(strict_types=1);
/**
* Tiny PHP Ad Rotator (No DB)
*
* Usage:
* require __DIR__ . '/includes/ads.php';
* echo render_ad_slot('sidebar');
*
* Notes:
* - Each "slot" has its own list of ads.
* - Weighted rotation supported via "weight".
* - Optional no-repeat per slot using a cookie.
*/
if (!function_exists('render_ad_slot')) {
function render_ad_slot(string $slot): string {
$ads = ad_inventory($slot);
if (!$ads) return '';
$picked = pick_weighted_ad($slot, $ads);
// Render
$html = '<div class="ad-slot" data-slot="'.h($slot).'">';
$html .= $picked['html'];
$html .= '</div>';
return $html;
}
function ad_inventory(string $slot): array {
// Define your ads per slot.
// "html" should already include rel="sponsored nofollow noopener" where appropriate.
// If you want a label, include it in the HTML.
$all = [
'sidebar' => [
[
'id' => 'house_tool',
'weight' => 3,
'html' => '
<div class="textad">
<span class="adcopy"><span aria-hidden="true">📣</span> Try our lightweight tools & scripts — copy/paste installs.</span>
<span class="adsponsored">Sponsored</span>
<a class="adcta" href="/tools/" rel="sponsored nofollow">Explore tools →</a>
</div>'
],
[
'id' => 'affiliate_1',
'weight' => 2,
'html' => '
<div class="textad">
<span class="adcopy"><span aria-hidden="true">🔥</span> Private deals & offers — updated daily.</span>
<span class="adsponsored">Sponsored</span>
<a class="adcta" href="https://example.com/?ref=abc" target="_blank" rel="sponsored nofollow noopener">Check it out →</a>
</div>'
],
[
'id' => 'banner_1',
'weight' => 1,
'html' => '
<a class="bannerad" href="https://example.com/" target="_blank" rel="sponsored nofollow noopener">
<img src="/assets/ads/sample-banner-1.jpg" alt="Sponsor" loading="lazy" />
</a>'
],
],
'inline' => [
[
'id' => 'inline_house',
'weight' => 1,
'html' => '
<div class="textad inline">
<span class="adcopy"><span aria-hidden="true">✨</span> Want more tiny scripts like this? Browse the library.</span>
<span class="adsponsored">Sponsored</span>
<a class="adcta" href="/blog/" rel="sponsored nofollow">See posts →</a>
</div>'
],
],
];
return $all[$slot] ?? [];
}
function pick_weighted_ad(string $slot, array $ads): array {
// Optional: do not repeat last ad for this slot (cookie-based)
$cookieKey = 'ad_last_' . preg_replace('/[^a-z0-9_]/i', '_', $slot);
$lastId = $_COOKIE[$cookieKey] ?? '';
// Build weighted list sum
$sum = 0;
foreach ($ads as $a) {
$w = (int)($a['weight'] ?? 1);
$sum += max(1, $w);
}
// Pick with a few retries to avoid repeating last ad
for ($try = 0; $try < 6; $try++) {
$r = random_int(1, max(1, $sum));
$acc = 0;
foreach ($ads as $a) {
$w = max(1, (int)($a['weight'] ?? 1));
$acc += $w;
if ($r <= $acc) {
$id = (string)($a['id'] ?? '');
if ($id !== '' && $id === $lastId && count($ads) > 1) {
break; // retry
}
// Set cookie to remember last ad for this slot
if ($id !== '') {
// session cookie is enough; keep it light
setcookie($cookieKey, $id, 0, '/', '', is_https(), true);
}
return $a;
}
}
}
// fallback
$a = $ads[array_rand($ads)];
$id = (string)($a['id'] ?? '');
if ($id !== '') setcookie($cookieKey, $id, 0, '/', '', is_https(), true);
return $a;
}
function is_https(): bool {
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') return true;
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && stripos((string)$_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0) return true;
return false;
}
function h(string $s): string {
return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
}
}
?>
Comments (0)
No comments yet — be the first.