Setting Up Global Configuration

I have found that it is best to store information in as few places as possible. That way "when" you have to change information you dont have to dig through numerous files changing each one. Lets create a configuration file that can be used by any script that needs to access global information for tasks such as connecting to the database. This way if you need to change site configuration data, you can do it in this one file rather than searching through them all.

File: config.php

<?php
// Site configuration
define('SITE_NAME', 'My Example Site');
define('SITE_CONTACT', 'admn@mysite.com');
define('SITE_URL', $_SERVER['SERVER_NAME']);
define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT']);
define('DEBUG', true); // if set to true, displays additional information to screen

// Database configuration
define('DB_HOST','localhost');
define('DB_USER', 'yourUsrname');
define('DB_PASS', 'yourPassword');
define('DB_NAME', 'yourDbName');
define('USERS_TABLE', 'users');
define('USER_COLUMN', 'username');

// create mysql connection
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// function to escape posted data
function escape_data ($data) {
    if (!$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME)){
        die(mysqli_error($dbc));
    }

    $data = trim($data);
   
    if (get_magic_quotes_gpc()){
        $data = stripslashes($data);
    }
   
    return mysqli_real_escape_string($dbc,$data);
}
?>

First we define constants that we will use in the rest of our code. Then we define the mysql connection function and a function for escaping user data.