Oops, when I posted that I didn't copy over my define.
I have a standard set of DEFINE I usually put in place for certain stock values. Clean a few of them up, extract out needed sub-sections, etc, etc...
This is the full set lifted straight out of my codebase:
Code:
define('SCRIPT_PATH', cleanPath(pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME)));
define('ROOT_HTTP', '/' . SCRIPT_PATH . (SCRIPT_PATH == '' ? '' : '/'));
define('ROOT_LOCAL', pathinfo($_SERVER['SCRIPT_FILENAME'], PATHINFO_DIRNAME) . '/');
define('HOST_PROTOCOL', 'http' . (
isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off') ? 's' : ''
) . '://');
define('HOST_HTTP', HOST_PROTOCOL . $_SERVER['HTTP_HOST']);
define('BASE_HTTP', HOST_HTTP . ROOT_HTTP);
define('PATH_HTTP',
'/' . parse_url(cleanPath($_SERVER['REQUEST_URI']), PHP_URL_PATH)
);
define('FULL_HTTP', HOST_HTTP . PATH_HTTP);
define('CANONICAL_PATH', Request::getPath());
define('CANONICAL_URI', HOST_PROTOCOL . WORKING_DOMAIN . '/' . CANONICAL_PATH);
define('CANONICAL_URIe', urlencode(CANONICAL_URI));
You can pick and choose the ones you need. The thing about this code is it parses andd removes the filename from $_SERVER['SCRIPT_NAME'] so we can figure out what our root directory is automatically. This means you can place this in a subdirectory too, if you want to run something different in the hosting root.
In my template, I generally will include:
Code:
echo '<base href="', BASE_HTTP, '">';
Which makes sure the fact that while the served URI could be something like /articles/articleName, the base href is the root from which the script itself exists....
Unrelated to doing a single index approach but something I highly recommend, this file:
/libs/gzip.lib.php
Code:
<?php
/*
gzip.lib.php
Version 1.0 Jason M. Knight, August 2009
Uses a proper exit handler to provide automation of gzip compression of our
PHP output with little if any headaches.
ASSUMES:
CONTENT_ENCODING contains either 'x-gzip' or 'gzip' based on the value in
HTTP_ACCEPT_ENCODING. See "defines.php" to see how this is set.
If STRIP_WHITESPACE is defined whitespace between tags or at the start of
lines will be stripped, as will comments. Whitespace between a tag and
CDATA or between attributes will be left alone.
*/
ob_start();
ob_implicit_flush(0);
register_shutdown_function(function() {
header('Content-Encoding: ' . CONTENT_ENCODING);
$contents = ob_get_contents();
if (defined('STRIP_WHITESPACE')) $contents = preg_replace(
['#<!--.*?-->#s', '#>\s+<#', '#\n\s+<#'],
['', '><', '<'],
$data
);
ob_end_clean();
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00",
substr(gzcompress($contents, 6), 0, -4);
});
called via this as the VERY FIRST THING (I'm talking not even whitespace before the <?php) in your index.php:
Code:
<?php
// start compression first since it does a heading and starts buffering
define('CONTENT_ENCODING',
(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false) ? 'x-gzip' :
(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) ? 'gzip' :
false
);
if (CONTENT_ENCODING) include('libs/gzip.lib.php');
Will automatically add proper gzip compression to serving your markup, headache free. You don't need to do anything at the end, it even works if you use die() or for error messages as it uses register_shutdown_function to set the proper encoding, compress the buffer, and adjust the header for maximum compatibility.
I'm heading to bed and have doctors visits first thing in the morning, but tomorrow evening or wednesday if I can spare the time, I'll toss together a quick working demo for you of how I approach it... I'll basically give you a gutted down copy of my system with docs, since I've been meaning to write documentation explaining it anyways. It'll even have a rudimentary template system in place to make working with the HTML and CSS easier.
Single entry via a blacklist/whitelist is to a varying extent a swan-dive into the deep end of the learning pool... There's a LOT of ground to cover particularly if one wants to automate it so that you can then focus on what's really important -- CONTENT.