Home Web Design Computing Music

DLib Library

Note that the instructions on this and subsequent pages assume that you are fully conversant with normal PHP programming methods. If this is not the case then pick up one of the many PHP books that are currently available.

Unzip the downloaded file into an area of your site that is OUTSIDE of the normal publicly accessible area. For example, you may have the following directories on a Unix/Linux server running Apache:

/var/www/htdocs   <-- where the document root points
/var/www/cgi-bin  <-- where CGI programs live

You could put the library at the same level:

/var/www/dlib6     <-- library files

You should also make the dlib subdirectory (inside the dlib6 directory) available inside the document root so that it appears as:

/var/www/htdocs/dlib

There are two ways of doing this:

  1. Copying the entire contents of the dlib subdirectory. This is NOT the recommended method because, if you have several different sites on the same web server, each would need a separate dlib - this would prove a nightmare to maintain. On earlier versions of Windows servers this may be the only option available to you (later versions have the MKLINK command which is similar to the Unix/Linux/Mac OS X command below).
  2. Under Unix/Linux/Mac OS X-based servers you can use the symbolic link command ln to link back to the common dlib inside the dlib6 directory. To do this, use the following commands in a terminal on your server:
cd /var/www/htdocs
ln -s /var/www/dlib6/dlib dlib

Note that you MUST remember that making any changes inside the pseudo /var/www/htdocs/dlib directory will actually be changing the main files! However, you shouldn't ever need to do this.

For the PHP pages inside htdocs you will need to add a single include file that will allow those pages access to the library. Create a file include.php with the following code and put it in htdocs:

<?php

if (strpos ($_SERVER ['SCRIPT_NAME'], "include.php") !== false)
  die ("Illegal access");

ini_set ("include_path", ".:/var/www/dlib6");
include_once "lib.php";

?>

The file is doing three things:

  1. making sure that it is not being run directly (and dieing if so);
  2. setting the include path to the current directory (the '.' dot) as well as to where the library has been installed;
  3. attempting to call the main library include file (lib.php) which, itself, will include all the other files in the library.

Notes:

  • If you are running several different web sites off a single server (e.g. you may be using Apache's Virtual Name Servers) then it is only necessary to install one instance of the library on the server. This makes upgrading very simple.
  • Any other PHP file on your web site that needs to access the library only needs to include the include.php file.
  • If you are going to create a set of other PHP files/functions that will be common to your entire web site then you could also place those files in their own directory outside the document root such as /var/www/commonphp and then add another include_once call within include.php to gain access to those as well. You would also need to add their directory to the ini_set function call.
  • Users running on a Windows system will need to modify the ini_set statement to reflect that system's directory separator and volume differences.)

The final example shows how you can use the same include.php file to handle a local development server on Windows and a live server that uses Unix/Linux. This interrogates the host it is running on to figure out which type of format it should be using which means that separate files need not be created for running the same code on different operating systems. Note that we also set a variable $live which may come in useful in other parts of the system for allowing separate functions or code to run on the local development and live servers, and we set a value in $dbTablePrefix which prefixes all the tables used with, in this case, xyz_. This is useful when you are running several sites on one physical database - a restriction still common with some older hosting services - so that you can easily separate each group of tables from each other.

<?php

if (strpos ($_SERVER ['SCRIPT_NAME'], "include.php") !== false)
  die ("Illegal access");

$host = $_SERVER ['HTTP_HOST'];

switch ($host)
{
  case "www.somewhere.com" :
    ini_set ("include_path", ".:/var/www/dlib6:/var/www/commonphp");
    $live = true;
    break;

  default :
    ini_set ("include_path", ".;C:/Program Files/Apache Group/Apache2/dlib6;C:/Program Files/Apache Group/Apache2/commonphp");
    $live = false;
}

$dbTablePrefix = "xyz_";
include_once "lib.php";
include_once "mystuff.php";

?>