Page 1 of 1

Include Connection file

Posted: Wed Apr 23, 2008 2:26 am
by cutebird75
Hello every body!
I am not very genius in programming. I am working on a online system.
I have my connection file in "home/config/connection.php" directory.
Now I am accessing this file through include ('../connection.php') on first level and include('../../connection.php') on second level. I have to mention all this in each file.
but the problem is that if I have to change my directory structure or relocate files to a new place then I will have to change path manually in all files. if there is any specific solution to solve this problem.please tell me about it in detail.
Thanks lot.

Re: Include Connection file

Posted: Wed Apr 23, 2008 2:49 am
by Kadanis
One way to do it is to have a single include file which is located somewhere for example /var/www/config.php

Then in this file set up variables or constants for the paths, i.e

config.php

Code: Select all

 
<?php
$connection_dir = '/path/to/connection/dir/';
?>
 
Then at the top of each file you include the absolute path to the config.php and the variable paths to the other files.

i.e.

any_other_file.php

Code: Select all

 
require_once('/var/www/config.php');
require_once($connection_dir . 'connection.php');
 
This means that should your directory structure change, as long as you keep the config file in the same place you can reset all the other include paths.

I'm sure there are other ways of doing this, but this is how I've done it in the past