<?php
/**
* This PHP file is within the webroot, in the 'public/appendix-samples' directory on this webserver.
* This means that it can be accessed directly from a web browser, like you can
* for example with a URL like 'https://php.danq.dev/appendix-samples/sample-include-2.php'.
*
* However, we can use a trick to have it "halt early" if accessed directly, and only output
* the four verses of "10 Green Bottles" if it's included from another file.
*/
// If this file is accessed directly, halt execution and output a "Forbidden" error:
if( $_SERVER['REQUEST_URI'] === $_SERVER['SCRIPT_NAME'] ) {
header('HTTP/1.1 403 Forbidden');
die('This page cannot be accessed directly.');
}
// Otherwise, choose a random number from 6 to 10 and use that as the bit we're up to of
// the "10 Green Bottles" song, counting down for four verses from that number:
$bottles = rand(6, 10);
echo '<blockquote style="border: 1px solid black; padding: 1em; margin: 1em 0;">';
// Four times, print a verse and subtract one bottle:
for( $i = 0; $i < 4; $i++ ) {
echo '<p>';
echo "{$bottles} green bottles hanging on the wall,<br>";
echo "{$bottles} green bottles hanging on the wall,<br>";
echo "And if one green bottle should accidentally fall,<br>";
$bottles--; // subtract one bottle!
echo "There'll be {$bottles} green bottles hanging on the wall.";
echo '</p>';
}
echo '</blockquote>';