Browse Source
If the directory "banners_priority" exists and contains files, a file from it is served 33% of the time, an a file from "banners" 66% of the time. This may seem like an odd definition of "priority", but assuming far fewer priority banners than normal banners, this gives them extra weight in the selection. Before, any 1 banner in 100 had a 1% chance of being served; now if that banner is a priority banner it has a 33% chance.pull/40/head
Michael Walker
8 years ago
1 changed files with 45 additions and 19 deletions
@ -1,21 +1,47 @@ |
|||||
<?php |
<?php |
||||
//renaming to b.php |
// This script assumes there is at least one normal (non-priority) |
||||
$dir = "banners/"; |
// banner! |
||||
$files = scandir($dir); |
|
||||
$images = array_diff($files, array('.', '..')); |
// Get the files in a directory, returns null if the directory does |
||||
$name = $images[array_rand($images)]; |
// not exist. |
||||
|
function getFilesInDirectory($dir) { |
||||
// snags the extension |
if (! is_dir($dir)) { |
||||
$img_extension = pathinfo($name, PATHINFO_EXTENSION); |
return null; |
||||
|
} |
||||
// send the right headers |
|
||||
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1 |
return array_diff(scandir($dir), array('.', '..')); |
||||
header('Pragma: no-cache'); // HTTP 1.0 |
} |
||||
header('Expires: 0'); // Proxies |
|
||||
header("Content-type: image/" . $img_extension); |
// Serve a random banner and exit. |
||||
header("Content-Disposition: inline; filename=" . $name); |
function serveRandomBanner($dir, $files) { |
||||
|
$name = $files[array_rand($files)]; |
||||
// readfile displays the image, passthru seems to spits stream. |
|
||||
readfile($dir.$name); |
// snags the extension |
||||
exit; |
$ext = pathinfo($name, PATHINFO_EXTENSION); |
||||
|
|
||||
|
// send the right headers |
||||
|
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1 |
||||
|
header('Pragma: no-cache'); // HTTP 1.0 |
||||
|
header('Expires: 0'); // Proxies |
||||
|
header("Content-type: image/" . ext); |
||||
|
header("Content-Disposition: inline; filename=" . $name); |
||||
|
|
||||
|
// readfile displays the image, passthru seems to spits stream. |
||||
|
readfile($dir.$name); |
||||
|
exit; |
||||
|
} |
||||
|
|
||||
|
// Get all the banners |
||||
|
$bannerDir = "banners/"; |
||||
|
$priorityDir = "banners_priority/"; |
||||
|
|
||||
|
$banners = getFilesInDirectory($bannerDir); |
||||
|
$priority = getFilesInDirectory($priorityDir); |
||||
|
|
||||
|
// If there are priority banners, serve 1/3rd of the time. |
||||
|
if($priority !== null && count($priority) !== 0 && rand(0,2) === 0) { |
||||
|
serveRandomBanner($priorityDir, $priority); |
||||
|
} |
||||
|
|
||||
|
serveRandomBanner($bannerDir, $banners); |
||||
?> |
?> |
||||
|
Loading…
Reference in new issue