First off I googled a million pages and just could not find answers :shifty:.

I am trying to do this:1. In a configuration.php file I want to set up the path to my siteI currently have this

<?php
$siteroot = $_SERVER['DOCUMENT_ROOT'];
?>

In my other files I want to simply create blank containers and just include pages. So for instance in Index.php I want to include header.php, sidebar.php and footer.php.

I currently have this:At the top of my page I am calling the configuration.php: <?php require_once ("includes/configuration.php");?>

And in the spot where I want my header to appear I use this:

<?php include("$siteroot/includes/module-header.php"); ?>

And likewise for all the other includes - sidebar.php and footer.php.

This does not work, I get this error message:

[b]Warning[/b]: include(D:/wamp/www//includes/module-header.php) [[function.include](http://localhost/imi/function.include)]: failed to open stream: No such file or directory in [b]D:\\wamp\\www\\imi\\index.php[/b] on line [b]105[/b]

Now I want this to work in my local server and seamlessly work in production.

I have no understanding and what code I am using was from one of my many google adventures... it apparently is incorrect.

oddz
—
2010-03-13T21:53:52Z —
#2

$siteroot = rtrim($_SERVER['DOCUMENT_ROOT'],'/');

Try that, It looks like the problem lies with multiple directory separators:

include(D:/wamp/www//includes/module-header.php)

If you would like to access that variable from within classes or functions its probably best to make it a constant.

define('SITE_ROOT',rtrim($_SERVER['DOCUMENT_ROOT'],'/'));

inclick
—
2010-03-13T22:01:56Z —
#3

I would suggest using a configuration file that where you would define the path:

define('YOUR_BASE_DIR', 'D:/wamp/www');

In your script, you would perform your include like this:

require_once(YOUR_BASE_DIR . "/includes/module-header.php");

Defining the path is always better than relying on what can otherwise be a moving target. Avoid relative paths whenever possible.

conradical
—
2010-03-13T22:03:10Z —
#4

oddz said:

$siteroot = rtrim($_SERVER['DOCUMENT_ROOT'],'/');

Try that, It looks like the problem lies with multiple directory separators:

include(D:/wamp/www//includes/module-header.php)

If you would like to access that variable from within classes or functions its probably best to make it a constant.

`php

define('SITE_ROOT',rtrim($SERVER['DOCUMENTROOT'],'/'));

`

I will not be using it within classes or functions. My needs are to simply have a variable that I can use instead of a path.

The example you gave does not work - I yet get that error message.

inclick
—
2010-03-13T22:26:38Z —
#5

The code I provided above will work as we use this as a standard practice for our system configuration.

conradical
—
2010-03-13T22:32:20Z —
#6

inclick said:

I would suggest using a configuration file that where you would define the path:

`php

define('YOUR_BASE_DIR', 'D:/wamp/www');

`In your script, you would perform your include like this:

`php

require_once(YOUR_BASE_DIR . "/includes/module-header.php");

`Defining the path is always better than relying on what can otherwise be a moving target. Avoid relative paths whenever possible.

That works - so when I take this to production, should the base directory change to http://www.sitename.com/" or the server path? If either one will work which is recommended?

inclick
—
2010-03-13T22:37:13Z —
#7

conradical said:

That works - so when I take this to production, should the base directory change to http://www.sitename.com/" or the server path? If either one will work which is recommended?

When you go to production, you would use the server path to the file (the FULL path is advised).

Glad to see that worked.

-Bing

conradical
—
2010-03-13T22:40:40Z —
#8

Thank you. I have one more question.

So if I want to now use that variable (YOUR_BASE_DIR) in a link how would I call it?

For example i want to link to a file in information/faq/questions.php

How would i call the variable -<a href="<?php What php code ?>/information/faq/questions.php">FAQ</a>

And to your other comment re php paths, it is actually best practice to depend on a relative path to define your root.

inclick
—
2010-03-14T00:44:26Z —
#14

Defining the base URL is still recommended, particularly when you start scaling out to multiple servers or use a CDN. If you have no plans to do that, then sure, you could simply use the relative href.

hash
—
2010-03-14T01:28:03Z —
#15

It's not recommended at all, and completely pointless to boot.

conradical
—
2010-03-14T02:20:33Z —
#16

hash said:

It's not recommended at all, and completely pointless to boot.

Okay - what be be your suggestions to my question?

cutcopypaste
—
2010-03-14T02:36:22Z —
#17

this looks like what i've been looking at in this thread. i would need to include that configuration.php in every php file no matter what folder it is? that would be using relative paths i imagine.. just getting utterly confused trying to solve a pretty similar problem..

For html you just use /<a href="/some/directory/img.php"> <-- that url is the absolute path from the web servers root<a href="img.php"> <-- that url is relative to what directory you are currently in

For php paths, you are no longer talking about the web server root, but the servers file system. So you need $root to be the path to your project, eg /home/hash/websites/myproject/You can set this based off the DOCUMENT_ROOT, or based on an entry point in your script, or other ways, like hard coding a config file.