Jump to content

Proper way to do includes for small site


philosopherdog

Recommended Posts

Hi,

I'm pretty new to php and need a bit of basic help.

 

I have a header and footer include file in a "views" folder. The header itself has a function that's included from another directory. Now when I try to include the header in by index.php in the root it throws and error saying it can't find my function include. Obviously the include directory is relative to the file it's included in. So, what's the trick to fixing this problem? And what's the lesson for future projects? Thanx in advance.

Link to comment
Share on other sites

Obviously the include directory is relative to the file it's included in.

No: it's relative to the directory of the first executed file. If that first file includes a file in some other directory, the include path will not "update" to reflect that other directory.

 

Use absolute file paths to include files.

// relative to the current file
require_once __DIR__ . "/path/../to/../function.php"; // PHP 5.3+
require_once dirname(__FILE__) . "/path/../to/../function.php";

// relative to the web root
require_once $_SERVER["DOCUMENT_ROOT"] . "/path/to/function.php";

Link to comment
Share on other sites

Yes, my mistake. Thanx for the quick reply. Just so I understand what's happening here __DIR__ grabs the current directory. So, let's say my header.php is in the views folder. require_once __DIR__ . "/path/../to/../header.php"; will append the current directory to this path. So, let's say the current directory is forum/views. So it would render as

require_once 'forum/views/header.php';

And when I try to include it from the root index.php instead of the one in the views folder it will render it as

require_once 'header.php';

Is this correct?

Now is there a way to add this to a config file so that I don't have to make my include files so verbose? How do I do that? I found that the $_SERVER["DOCUMENT_ROOT"] option was a nuisance because I'm developing using a local server, which means I have to change all my paths when I deploy. Thanx.

Link to comment
Share on other sites

__DIR__ is the directory containing the currently-executing file (exactly like how __FILE__ is the full path to the file). So if you use

require_once __DIR__ . "/header.php";

in /forum/views/*.php then it'll include /forum/views/header.php, and if you use it in /index.php then it'll include /header.php.

 

I found that the $_SERVER["DOCUMENT_ROOT"] option was a nuisance because I'm developing using a local server, which means I have to change all my paths when I deploy.

Why's that? Are you serving the site out of a subfolder or something?

Link to comment
Share on other sites

cool thanx.

Why's that? Are you serving the site out of a subfolder or something?

Yes. Actually, when I tried that it wanted the whole path to the apache server as part of the include. I'm assuming this is going to be different when I deploy it since it's going up on a windows server (unfortunately).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.