This page will list several example set_env.php files. The SmartestSmartyPractices article has a catch-all set_env.php file which should be trimmed down depending on your needs. set_env.php examples on this page will be trimmed down to their bare essentials.

By Tom Anderson toma

Example #1 - A php application running under Linux using a MySQL database which runs on the same server as the web server. No user login security implemented.

<?
/**
 * set_env.php 
 * @author Tom Anderson <toma@etree.org>
 * @version 2.5
 * @version mysql-1.0-example
 *
 * This script sets up smarty, pear, and all other
 * global settings for a php application.
 */

// Setup include path and error reporting levels.
$web_root = 'http://my.site.net/';
$app_root = '/home/projects/my.site.net/';
$pear = $app_root . '/pear/'; 
error_reporting(E_ALL - E_NOTICE);

// For better performance, see notes below.
$delim = ':';
ini_set('include_path', ".{$delim}$pear{$delim}$app_root/include{$delim}$app_root");

// Include pear database handler, auth object (remove if not used), and smarty
require_once 'DB.php';
require_once 'smarty/Smarty.class.php';

// Setup a default error handler
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'errhndl');
function errhndl ($err) {
    echo '<pre>' . $err->message;
    print_r($err);
    die();
} 

// Connect to the database and set fetch mode as necessary.  
$db = DB::connect(array(
    'username' => 'mysqluser',
    'password' => 'mysqlpass',
    'hostspec' => 'localhost',
    'database' => 'my_site_net',
    'phptype' => 'mysql'
));
$db->setFetchMode(DB_FETCHMODE_ASSOC);  // Other modes possible; I find assoc best.

// Setup smarty template object
$t = new smarty;
$t->template_dir = "$app_root/ihtml/";
$t->compile_dir = $app_root . '/compile';
$t->cache_dir = $app_root . '/cache';
$t->plugins_dir = array($app_root . '/include', $app_root . '/smarty/plugins');

// Change comment on these when you're done developing to improve performance
$t->force_compile = true;
//$t->caching = true;

## GLOBALS:  $db, $t
session_start();

// Assign any global smarty values here.
$t->assign('web_root', $web_root);
?>

Notes: