Initialization and configuration
Copy the whole tree of site0 to a folder called site1.
- /cms
- site0
- site1
In this chapter, we are going to organize how the site is configured and started.
To test the result online, enter http://www.frasq.org/cms/site1 in the address bar of your navigator. The output shows how the URL is analyzed by the program.
Create the folders includes and library in the folder site1. Move dump.php in library.
- /cms/site1
- includes
- library
- dump.php
- favicon.ico
- robots.txt
- sitemap.xml
- index.php
- .htaccess
The files with the parameters for initializing and configuring the program are grouped in the folder includes.
- /cms/site1
- includes
- settings.inc
- config.inc
- includes
Create the files settings.inc and config.inc in the folder includes with the following contents:
- ini_set('arg_separator.output', '&');
- ini_set('magic_quotes_runtime', 0);
- ini_set('magic_quotes_sybase', 0);
- ini_set('register_globals', 0);
- ini_set('session.cache_expire', 180);
- ini_set('session.cache_limiter', 'none');
- ini_set('session.cookie_lifetime', 0);
- ini_set('session.gc_maxlifetime', 1440);
- ini_set('session.name', '');
- ini_set('session.save_handler', 'files');
- ini_set('session.use_cookies', 1);
- ini_set('session.use_only_cookies', 1);
- ini_set('session.use_trans_sid', 0);
- ini_set('url_rewriter.tags', '');
- ini_set('error_reporting', E_ALL | E_STRICT);
- ini_set('display_errors', 1);
- ini_set('log_errors', 0);
settings.inc redefines parameters of PHP which are configured in the system file php.ini.
IMPORTANT: Change the configuration of the site when put online to not display errors and write them instead in a log by setting the parameter display_errors
to 0
and the parameter log_errors
to 1
.
- global $base_url, $base_path, $base_root;
- global $sitename, $webmaster;
- $sitename = 'frasq.org';
- $webmaster = 'nobody@frasq.org';
config.inc is reserved for the global parameters of the program.
The initialization of the program is done by a function called bootstrap
which needs the functions unset_globals
and validate_host_name
. Each function is defined in a separate file.
- /cms/site1
- library
- bootstrap.php
- unsetglobals.php
- validatehostname.php
- library
- function unset_globals() {
- if (ini_get('register_globals')) {
- $allowed = array('_ENV', '_GET', '_POST', '_COOKIE', '_FILES', '_SERVER', '_REQUEST', 'GLOBALS');
- foreach ($GLOBALS as $key => $value) {
- if (!in_array($key, $allowed)) {
- unset($GLOBALS[$key]);
- }
- }
- }
- }
unset_globals
clears a series of dangerous global variables set by PHP if the parameter register_globals
is true
. We have asked Apache in .htaccess with the directive SetEnv REGISTER_GLOBALS 0
and PHP in settings.inc with a call to init_set
to set this parameter to 0, but an internet provider might very well configure Apache and/or PHP so they reject any attempt to modify this parameter.
- function validate_host_name($host) {
- return preg_match('/^\[?(?:[a-z0-9-:\]_]+\.?)+$/', $host);
- }
validate_host_name
returns true
if the argument $host
is a valid host name, false
otherwise.
- require_once 'unsetglobals.php';
- require_once 'validatehostname.php';
Loads the code for the functions unset_globals
and validate_host_name
.
- function bootstrap() {
- global $base_url, $base_path, $base_root;
bootstrap
initializes the global variables $base_url
, $base_path
and $base_root
.
- if (isset($_SERVER['HTTP_HOST'])) {
- $_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']);
- if (!validate_host_name($_SERVER['HTTP_HOST'])) {
- header('HTTP/1.1 400 Bad Request');
- exit;
- }
- }
- else {
- $_SERVER['HTTP_HOST'] = '';
- }
Tries to pinpoint a fraudulent request by validating the name of the sender with validate_host_name
.
- unset_globals();
- @include 'settings.inc';
- @include 'config.inc';
Cleanses the global variables of PHP. Initializes and configures the program.
- if (isset($base_url)) {
- $base_url = trim($base_url, '/');
- $url = parse_url($base_url);
- if (!isset($url['path'])) {
- $url['path'] = '';
- }
- $base_path = $url['path'];
- $base_root = substr($base_url, 0, strlen($base_url) - strlen($base_path));
- }
- else {
- $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
- $base_url = $base_root .= '://'. $_SERVER['HTTP_HOST'];
- if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
- $base_path = '/' . $dir;
- $base_url .= $base_path;
- }
- else {
- $base_path = '';
- }
- }
- }
If the global variable $base_url
has been defined in config.inc, extracts from it the path part of the URL and initializes the global variables $base_path
and $base_root
. Otherwise, computes the values of $base_root
and of $base_path
from the PHP variables $_SERVER['HTTPS']
, $_SERVER['HTTP_HOST']
and $_SERVER['SCRIPT_NAME']
, then builds $base_url
by concatenating them.
Modify index.php to properly initialize the program with bootstrap
:
- define('ROOT_DIR', dirname(__FILE__));
- set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIR . DIRECTORY_SEPARATOR . 'library');
- set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIR . DIRECTORY_SEPARATOR . 'includes');
- require_once 'dump.php';
- require_once 'bootstrap.php';
- bootstrap();
- dump($base_url);
- dump($base_root);
- dump($base_path);
index.php starts by adding the directories library and includes to the PHP path.
After loading the code for dump
and bootstrap
, index.php calls bootstrap
.
The rest of the program prints the global variables $base_url
, $base_root
and $base_path
which were set in bootstrap
.
Enter http://localhost/cms/site1/search?q=foobar in the address bar of your navigator.
string(32) "http://localhost/cms/site1"
string(22) "http://localhost"
string(10) "/cms/site1"
Set $base_url
to http://localhost/cms/site1
in config.inc and reload the page. Check that the display is identical.
Comments