Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include config.php efficiently?

Tags:

php

I'm always interested in more efficient ways and learning new things. Right now, I am using the code <?php include('config.php'); ?> in each and every file. Depending on where the file is in my the folder structure, I would have <?php include('../config.php'); ?> or even <?php include('../../config.php'); ?>. How can I make it more efficient? Is there a way to just have it my config.php in root and make everything in root require config.php?

like image 423
Strawberry Avatar asked Nov 11 '09 04:11

Strawberry


People also ask

What is the way to include the file config PHP?

Copy the below code to include the 'config. php' file and get a print the name of the database and username. include ( 'config. php' );

How can I access config variable in PHP?

php include("config. php"); at the top of the PHP file you want to access it in. You will then be able to access the config variables form that PHP file it is declared in.

Where is config file in PHP?

The PHP configuration file allows you to configure the modules enabled, the email settings or the size of the upload files. It is located at installdir/php/etc/php. ini. For example, to modify the default upload limit for PHP, update the PHP configuration file following these instructions.

How you can import a file in PHP?

PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


2 Answers

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

like image 165
user187291 Avatar answered Oct 16 '22 15:10

user187291


Put the path containing config.php in your php include path and then you can simply do this:

include 'config.php';

or better yet:

require_once 'config.php';

require is preferred over include because it triggers an error instead of a warning when a file cannot be included for some reason (e.g. file not found, permissions error, etc.). The _once suffix is a good addition to make sure the same file isn't needlessly included multiple times.

Also, please note that you don't need the parenthesis around 'config.php' in your include call. include is not a function in php. It's a language construct. The parenthesis just serve as a needless grouping not unlike this example: $myVar = (2 + 2);.

like image 24
Asaph Avatar answered Oct 16 '22 16:10

Asaph