2. DB Admin - andperry256/my-base-theme-and-dbadmin GitHub Wiki

DB Admin (Intro & Basics)

This page describes some of the basics of setting up a DB Admin installation. Individual features are described in greater detail in subsequent pages.

Location of the DB Admin Package

The directory dbadmin from this repository needs to be copied on to the host system. The global variable $DBAdminDir must be set up to reference the path to this directory on the system. Likewise the global variable $DBAdminURL must be set up to reference it as a URL.

Location of the Site Application

In accordance with the requirements for My Base Theme, the application for running DB Admin on the given site must be located within the custom pages hierarchy. If there is a single database associated with the site then the admin address would typically be of the form https://www.mysite.com/dbadmin/ with the application held in sub-directory dbadmin within the custom pages hierarchy. If there are multiple databases requiring admin management, these would typically (but not necessarily) be set up as child pages of a main central admin page (e.g. https://www.mysite.com/dbadmin/db1/, https://www.mysite.com/dbadmin/db2/ etc.).

The _home.php Script

Here is a typical _home.php script for a DB Admin application within the custom pages hierarchy:-

require("{$_SERVER['DOCUMENT_ROOT']}/path_defs.php");
define('MASTER_LOCATION',$MasterLocation);
require_once("$PrivateScriptsDir/mysql_connect.php");
$RelativePath = "dbadmin";
require("$CustomPagesPath/$RelativePath/db_funct.php");
$SupportMobile = true;
if (!isset($NoAction))
{
  require("$DBAdminDir/admin_authentication.php");
  if ($UserAuthenticated)
  {
    require("$DBAdminDir/base.php");
  }
}

Firstly the path_defs.php file is included. This contains various global definitions and variable settings. One of the variables that it sets is $Location which is typically set to "real" for a live web site and "local" for a local offline server. The definition MASTER_LOCATION (see above) is normally set set to "real" or "local" to indicate which is the master database, so that a warning can be output on the screen if the user is not on the master database.

The mysql_connect.php file contains the database connect functions although a file of this name is not mandatory (see below).

The variable $RelativePath is mandatory. It is set to the sub-directory path for DB Admin within the custom pages hierarchy, which by definition equates to the sub-path under the root site URL for the DB Admin web address.

The db_funct.php file contains the functions described below under Essential Functions although a file of this name is not mandatory.

The variable $SupportMobile must be set to true if the facility is to switch into a separate mobile mode on a small screen.

The remainder of the script is concerned with the actual loading and running of the DB Admin application. The first conditional statement allows a variable $NoAction to be preset if this script needs to be included by another script just for loading function and variable definitions and not to run the actual application. The admin_authentication.php file is part of this package and is used to verify that there is a valid logged on user, displaying a login screen where necessary. Finally once the user has been authenticated the base.php script within the DB Admin package is activated to run the application itself.

DB Admin Styles

There is a stylesheet within the DB Admin package and this needs to be loaded in order for the application interface to be correctly formatted. This would normally be done using an init.php script for the current page within the custom pages hierarchy, and a typical script is as follows:-

require_once("{$_SERVER['DOCUMENT_ROOT']}/path_defs.php");
print("\n<link rel='stylesheet' id='dbadmin-styles-css'  href='$DBAdminURL/styles.css?v=$link_version' type='text/css' media='all' />\n");

Essential Functions

The following two functions must be created for the database being managed:-

  • admin_db_connect() - This performs the database connection and returns a database link variable for use with the PHP mysqli functions.
  • admin_db_name() - This returns the actual name of the database.

In all my site implementations so far, these functions are located within the script file db_funct.php and call functions defined within mysql_connect.php. This structure is not mandatory however; all that matters is that these two functions are correctly defined on loading the _home.php script.