Skip to content

Instantly share code, notes, and snippets.

@dimo414
Last active November 11, 2016 19:28
Show Gist options
  • Save dimo414/3029725 to your computer and use it in GitHub Desktop.
Save dimo414/3029725 to your computer and use it in GitHub Desktop.
Sample PHP website structure, demonstrating environment abstraction
<?php
/*
This file handles all necessary setup that has to happen on each page load.
It should have no need to be aware of where the server is running from.
*/
session_start();
// This file isn't tracked by version control, and might not exist
@require_once($_SERVER['DOCUMENT_ROOT'].'/includes/config.inc.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/settings.inc.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/func.inc.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/classes/template.class.php');
if(DEBUG_MODE){
error_reporting(E_ALL | E_STRICT);
}
$template = new template();
if(!defined('MYSQL_USER')){ // indicates the config file does not exist
$template->error("<strong>The config file has not been created, or is not configured properly.</strong><br />
Copy the file <em>includes/config.base.inc.php</em> to
<em>includes/config.inc.php</em> and change the necessary parameters.");
niceExit("Bad Config File");
}
$db = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
if(mysqli_connect_errno()){
if(DEBUG_MODE){
$template->error('Failed To Connect To Database: '.mysqli_connect_errno().': '.mysqli_connect_error());
}else{
$template->error('Failed To Connect To Database. Try reloading the page or contact the admin.');
}
}
?>
<?php
/*
TEMPLATE configuration file - notice that this file is *not* included by common.inc.php
instead, you are expected to manually copy this file to /includes/config.inc.php and make
whatever changes need to be made, like specifying a different mysql password, or
disabling DEBUG_MODE, for instance.
Reasonable defaults are provided as a starting point.
*/
define('MYSQL_USER', 'root');
define('MYSQL_PASS', '');
define('MYSQL_HOST', 'localhost');
define('MYSQL_DB', 'coolsite');
define('DEBUG_MODE',true);
// Any other settings that may depend on where the code is running go here
?>
<?php
/*
This file *is* tracked by version control, and should be the same across all environments.
This contains settings for the project, if you find that helpful. It's not necessary for all projects.
*/
define('POSTS_PER_PAGE', 10);
define('COMMENTS_TO_SHOW', 5);
?>
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/common.inc.php');
// WEBPAGE GOES HERE
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment