How to Include Config.PHP Efficiently

How to include config.php efficiently?

there is a way to include a file automatically (auto_prepend_file ini setting), however the biggest improvement you can make is to abandon the usage of multiple php files and use index.php as a single entry point for the whole website.

suppose you write a SO clone ;) with the pages "questions", "tags", "users", etc. On each page you need some generic php stuff (db, session) + common html elements (header, footer). A popular approach is to have a bunch of php files (questions.php, tags.php, users.php) each of them includes the common stuff. For example, users.php will look like this then

include 'db.php';
include 'session.php';
include 'html.header.php';
.....users-specific stuff
include 'html.footer.php';

This is quite tedious (you have to repeat lots of code) and inflexible (think adding a sidebar to all pages on the site). My suggestion is to make includes "inside out" that is, have a "common stuff" file that includes page-specific code:

 # index.php
db functions
session functions
html header

$page = isset($_GET['page'])
? preg_replace("/\W+/", "", $_GET['page'])
: "home";
include "$page.php";

html footer

Thus you'll have a single entry point on the website - this is more flexible and better for debugging. The only drawback is that urls are less "nice" (user.php vs index.php?page=user), but this can be easily solved with mod_rewrite

Fastest way to store easily editable config data in PHP?

Serialize is a better option than JSON for storing PHP variables.

I like to use var_export for saving config file, and using include for loading config info. This makes it easy to save config data progmatically AND makes the data easy to read/write for a person as well:

config.php:

return array(
'var1'=> 'value1',
'var2'=> 'value2',
);

test.php:

$config = include 'config.php';
$config['var2']= 'value3';
file_put_contents('config.php', '<?php return ' . var_export($config, true) . '; ?>');

Updated config.php now contains the following data:

return array(
'var1'=> 'value1',
'var2'=> 'value3',
);

Creating a config file in PHP

One simple but elegant way is to create a config.php file (or whatever you call it) that just returns an array:

<?php

return array(
'host' => 'localhost',
'username' => 'root',
);

And then:

$configs = include('config.php');

Including config.php inside out, code explanation

preg_replace is being used to strip non alphanumeric characters from the page variable.

This is a very thin attempt at security. The code is attempting to prevent an injection attack.

Consider if the user requested the page http://www.example.com/index.php?page=/etc/passwd
Without sanitizing the input, the password file for the domain would happily be dumped to the screen.

The pattern \W+ removes invalid characters such as the '/' character, preventing simple attacks such as this.

What is the best practice for including PHP files?

EDIT 2016

Well, 5 years since I replied this. I am still alive. A lot has changed.

Now I use autoloaders to include my files. Here is official info for autoloaders with examples.

Basically, the idea is to have a proper folder structure (PSR-4 standards for instance) and having a class within each file. This way you can use autoloaders and PHP will load your files automatically.

OLD ANSWER

Usually, I have a config file like this:

define(root, $_SERVER['DOCUMENT_ROOT']);
.... // other variables that are used a lot

include (root . '/class/database.php');
.... // other includes that are mostly called from each file, like a db class, or user class, functions etc etc...

if (defined('development'))
{
// turn error reporting on
}
else
{
// turn it off
}

etc etc... You got the point of config.

And I include the config.php on each file. I forgot how to do it right now, but apache can do the automatic include for you. Therefore, you can say to apache to include your config file by default.

Then, I have controller classes, which call the views. There in each function I call the view.

someController.php

function index() { include root . '/views/view_index.php'; }

finally, from the view, if I need to include the header and footer view I do it like this:

view_index.php

<?include root . '/view/shared/header.php';?>
<div class="bla bla bla">bla bla bla</div>
<?include root . '/view/shared/footer.php';?>

I always use include in this structure rather than include_once since the latter requires extra check. I mean, since I am pretty sure that I include files only once, I don't need to use include_once. This way, you also know which include is where. For instance, you know that crucial files like db.php, or functions.php are located in config.php. Or you know that include views are located in controllers. That's pretty useful for me, I hope that helps you, too.

Efficiency for including files of functions (in PHP)

Definitely separate them, for maintainability sake. I doubt performance will suffer at all, but even if it does (just a teensy bit) you're better off writing maintainable, readable code.



Related Topics



Leave a reply



Submit