« Python script for producing svn diffs | Main | Setting Up Database Connection Credentials With .ini Files »

March 27, 2009

Setting Environment Paths With paths.inc.php

Paths for each individual application are set withing the application directory itself in a file called paths.inc.php.

<?php

$r = $_REQUEST;

global $debug;

$paths = array();

if(file_exists("H:\\xampp\\htdocs\\")){ $docroot = "H:\\xampp\\htdocs\\"; }
if(file_exists("/Applications/xampp/htdocs/")){ $docroot = "/Applications/xampp/htdocs/"; }
if(file_exists("/userdata/home/depts/lis/")){ $docroot ="/userdata/home/depts/lis/"; }

$paths["host"] = $_SERVER["HTTP_HOST"];

$paths["baseref"] = "http://".$paths["host"]."/lis/simple_pages/";

$paths["js"] = "http://".$paths["host"]."/lis/js/";

$paths["js_path"] = $docroot."js/";

$paths["images"] = "http://".$paths["host"]."/lis/images/";

$paths["docroot"] = $docroot;

$config['BaseAddress'] = $paths['baseref'];

$asterix = '<span class="mandatory" style="color: red;">*</span>';

$db_name = 'libfaq';

$user_id = 1;

?>

This should be the first include in any application script:

<?

include("paths.inc.php");

session_start();

if(!$_SESSION['user_id']){ header('Location: login.php'); }

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

# Turn off warnings

error_reporting(E_ERROR | E_PARSE);

$debug = false;

$title = "LIS Simple Pages Site - Table of Contents";
.........

The script does several things. Firstly, it creates the $paths hash. Secondly, it aliases $_REQUEST to $r. N.B. If your name any other variable $r things will go bady for you. $r can be used with the add / update functions to add and update rows in a table where the column names match the web form elements.

$booking_id = add_new_row('booking);

$reply = update_table('booking', $r, $booking_id);

The $db_name filehandle is also set here (as opposed to in db_name.inc.php as previously). This refers to the .ini file name stump where your DB settings and credentials are stored. This is usually named after the database itself, but doesn't have to be.

Note also that a default $user_id is set here too. This is important for the add / update functions to work in applications where no user_id is set in the session. Normally this is picked up from the session after login and is the user_id from the user table, in which case this value gets overwritten.

Tags: Lightweight PHP Web Application Framework

Posted by pj at March 27, 2009 11:34 AM

Comments