conradical — 2010-03-13T16:37:09-05:00 — #1
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 site
I currently have this
$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-13T16:53:52-05:00 — #2
$siteroot = rtrim($_SERVER['DOCUMENT_ROOT'],'/');
Try that, It looks like the problem lies with multiple directory separators:
If you would like to access that variable from within classes or functions its probably best to make it a constant.
inclick — 2010-03-13T17:01:56-05:00 — #3
I would suggest using a configuration file that where you would define the path:
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-13T17:03:10-05:00 — #4
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-13T17:26:38-05:00 — #5
The code I provided above will work as we use this as a standard practice for our system configuration.
conradical — 2010-03-13T17:32:20-05:00 — #6
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-13T17:37:13-05:00 — #7
When you go to production, you would use the server path to the file (the FULL path is advised).
Glad to see that worked.
conradical — 2010-03-13T17:40:40-05:00 — #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>
inclick — 2010-03-13T17:48:42-05:00 — #9
You wouldn't, you would need to define a new constant:
You would the write your link like this:
<a href="<?php echo YOUR_WEB_URL; ?>/information/faq/questions.php">FAQ</a>
conradical — 2010-03-13T17:53:40-05:00 — #10
Oh sweet! Thanks.
hash — 2010-03-13T18:46:35-05:00 — #11
You don't need a constant. / is already defined for you.
will always go to http://domain.com/index.php
inclick — 2010-03-13T19:07:19-05:00 — #12
While this is true, it is a best practice to not use or depend on relative URLs.
hash — 2010-03-13T19:14:35-05:00 — #13
It's not a relative url
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-13T19:44:26-05:00 — #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-13T20:28:03-05:00 — #15
It's not recommended at all, and completely pointless to boot.
conradical — 2010-03-13T21:20:33-05:00 — #16
Okay - what be be your suggestions to my question?
cutcopypaste — 2010-03-13T21:36:22-05:00 — #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..
rguy84 — 2010-03-13T21:44:06-05:00 — #18
How do you figure those aren't relative? href="index.html" == "./index.html" give me index of current directory.
If you are at http://site.com/current/directory/index.php and link to /some/dir/example.php it knows you mean http://site.com/some/dir/example.php not http://site.com/current/directory/some/dir/example.php cause a single / means go to root then find x
hash — 2010-03-13T21:52:41-05:00 — #19
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.
hash — 2010-03-13T21:55:19-05:00 — #20
The first was relative for illustration of why the second wasn't. I bolded the bit in your post which means absolute.
next page →