bootstrap
bootstrap
SYNOPSIS
bootstrap()
DESCRIPTION
bootstrap
initializes the context of the program.
If the configuration parameter $db_url
is set, bootstrap
opens a DB connection.
bootstrap
defines the global variables $base_url
, $base_path
and $base_root
.
bootstrap
always opens a session and defines the global variable $session_name
if it's not already defined in the configuration file.
bootstrap
is the first function called by index.php, the unique entry point of the program.
CODE
- require_once 'session.php';
- require_once 'unsetglobals.php';
- require_once 'validatehostname.php';
Loads the code for the functions session_open
, unset_globals
and validate_host_name
.
- function bootstrap() {
- global $base_url, $base_path, $base_root;
- global $db_url, $session_name, $login_lifetime;
bootstrap
initializes the global variables $base_url
, $base_path
, $base_root
and $session_name
.
- 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';
- @include 'db.inc';
Cleanses the global variables of PHP. Initializes and configures the program.
- if ($db_url == 'mysql://username:password@localhost/databasename') {
- $db_url = false;
- }
- if ($db_url) {
- require_once 'pdo.php';
- db_connect($db_url);
- }
Checks if the DB connector is set to the default value.
Opens a connection with the DB if $db_url
isn't false
.
- if ($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.
- if (!$session_name) {
- list( , $session_name) = explode('://', $base_url, 2);
- $session_name = 'izend@' . $session_name;
- if (ini_get('session.cookie_secure')) {
- $session_name .= 'SSL';
- }
- }
Builds a unique session name if $session_name
isn't already set.
- session_open(md5($session_name));
Opens a session whose name is the MD5 of $session_name
.
- if (isset($_SESSION['user']['lasttime'])) {
- $now = time();
- if ($now - $_SESSION['user']['lasttime'] > $login_lifetime) {
- unset($_SESSION['user']);
- }
- else {
- $_SESSION['user']['lasttime'] = $now;
- }
- }
Disconnects a user who has been inactive for too long.
The configuration parameter $login_lifetime
defines the maximum time in seconds between two requests.
The session variable $_SESSION['user']
determines if a user is identified.
The field $_SESSION['user']['lasttime']
is initialized when the user logs in if the parameter $login_lifetime
isn't false
.
SEE ALSO
validatehostname, unsetglobals, db, session, userisidentified
Comments