Include Connection file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cutebird75
Forum Newbie
Posts: 1
Joined: Wed Apr 23, 2008 2:21 am

Include Connection file

Post 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.
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: Include Connection file

Post 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
Post Reply