views:

108

answers:

3

I'm getting really muddled up now and my brain hurts! :( lol

Root:

  • index.php

Includes:

  • cat.php
  • dog.php

index includes dog: include("includes/dog.php");

dog includes cat: include("cat.php");

When I run index, for cat it says:

  1. A link to the server could not be established
  2. Access denied for user ...

However, if I run dog, I get no problems...

I'm guessing its the path, but i've tried ./includes/cat.php to no joy...

+4  A: 

This is because when you include a relative path, it's relative to the entry point (the first PHP file, called by the webserver).

In dog, do

include(dirname(__FILE__) . '/cat.php'); // __FILE__ is always the name of the php file it's in
Bart van Heukelom
yup, Emma you are including file a/c dog.php but instead you should include a/c index.php. just think like dog.php is gonna paste in index.php and use your pathing a/c that
nik
Thanks Bart. Hopefully that will fix it. I need to clean up some bits first...Nik - I'm not sure I follow, sorry. Do you mean that I should include dog and cat from index and then not worry about doing those includes?
Emma
+1  A: 

It depends on where the script you are executing lies. When you execute /index.php the path of the script set to /, so all includes start from there. This means that you can find /includes/dog.php, but it's not possible to find /cats.php. Mind that, even if you are including cats.php from your /includes/dog.php script, this doesn't change the original execuption path.

When, on the other hand, you are executing /includes/dog.php, your path is set to /includes/, which is why PHP can also find cats.php.

Read Bart's comment on how to solve this.

Anax
+1  A: 

Another way to solve this is to set the include path of files, take a look at this.

http://ve2.php.net/manual/en/function.set-include-path.php

Kusanagi2k