4

I'm building the basic framework for my website and I have a config.php file stored in a scr directory. Additionally, this directory has a folder called php where I store a file called sidebar.php

Basically, the config.php file will hold my database configuration variables and is also the only file called by every page. I wish to include php/sidebar.php in this file so any variables I create in sidebar.php can be used on all pages.

However, I also want sidebar.php to have access to the database configuration variables. So these two files will effectively be including each other.

The include from config to sidebar is:

include 'php/sidebar.php';

From sidebar to config is:

include '../config.php';

However, the above statement (sidebar to config) yields the below error message:

Warning: include(../config.php) [function.include]: failed to open stream:

No such file or directory in C:\xampp\website\scr\php\sidebar.php on line 3

Am I going about this all wrong cross-linking the two files or is there a decent reason why it's not working

6
  • 1
    If config is included in all pages, and sidebar is included by config....why would you need sidebar to re-include config (which just included it)? Reminds me of a 5th grade math problem. -- Short answer, don't worry about re-including config as config will have just included the sidebar. Also, look in to using include_once Commented Mar 21, 2011 at 20:21
  • sidebar isn't a page, it's just a script. I'm not re-including it. I'm just including it. All the displayed pages have config included, but the scripts do not. Commented Mar 21, 2011 at 20:25
  • Imagine the process: home.php includes config.php, config.php includes sidebar.php, sidebar.php includes config.php...OOPS! We've already loaded config.php in home.php. This is what @Brad Christie meant by "re-including" it. Commented Mar 21, 2011 at 20:28
  • @Drackir ahhh thanks for clearing it up :) Cheers Commented Mar 21, 2011 at 20:31
  • @Daniel: You're missing the point. If EVERY page includes "config.php", it's trivial that any other script would need to include config again. That is to say, keep all content pages synonymous with <?php include_once('config.php'); ?> as the top line (or before any other includes), and never worry about that file again. Commented Mar 21, 2011 at 20:33

5 Answers 5

6

Generally speaking, a secure way of doing includes is to use dirname(__FILE__), or __DIR__ if you're using PHP >= 5.3, to have an absolute path -- written relatively from the current one.

For instance, you could use :

include dirname(__FILE__) . '/php/sidebar.php';

Note : `dirname(__FILE__)` and `__DIR__` point to the directory of the file in which they are written.

With that, no need to worry what is included from where and includes what : you always reference files using absolute pahts.


Relevant pages in the manual :
Also, as **@jwir3** pointed out in his comment, you'll sometimes want to use [**`require_once()`**][1] or [**`include_once()`**][2], to make sure a given file is not included more than once.

Including a file more than once can lead to trouble, especially if it contains constants, functions, or classes definitons.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, you might want to consider using require_once or include_once instead of include, as there could be cyclicality issues.
@jwir3 thanks for your comment ! I've edited my answer to include a note about that :-)
There were too many files reading config.php at the same time so it was causing an error. I include_once on my pages now and it works. Cheers for your help guys.
3

I guess that the php parser is already reading the file. You should use include_once instead of include to avoid this problem.

Aditionally you should also strongly consider require_once as it protect against the case where the included file doesn't exists, especially for a configuration file, your app should crash instead of ignoring it (And that's what require does).

PS : As other said the cyclic dependency should be broken anyway it's a pretty good sign of bad design.

2 Comments

as valid as the other posts are to helping me improve the workings of my framework, this is the one that fixed the problem. There were too many files reading config.php at the same time so it was causing an error. I include_once on my pages now and it works.
Well while cyclic dependency are a problem i don't see all the fuss about FILE and DIR, include & all are always using the script dir and if you begin the path by . or .. it even ignore the include_path. But I'll add a note on require as it is better than include.
2

You only need to include the sidebar from the config, not the other way around:

// in config.php
include('php/sidebar.php');

// in other files, such as page.php
include ('config.php');

1 Comment

This should've been the accepted solution... simple, non-intrusive, and more importantly, hitting home a fundamental concept.
0

If you include sidebar inside config, it should have access to any variables in a global scope. You may have to global $varName inside a function, though.

Include config on every page, include sidebar inside config, and access the config variables from sidebar. Don't bother double including.

Comments

0

Ok, so you've got

/path/to/your/site/scr/config.php
/path/to/your/site/php/sidebar.php

and you want sidebar.php to load config.php?

include('../scr/config.php');

will do the trick.

1 Comment

/path/to/your/site/scr/config.php /path/to/your/site/scr/php/sidebar.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.