php include() from server root? - php

I'm deploying from my WAMP testing environment to an online test...
Locally I had my include paths something like this:
include('C/wamp/www...')
how do i find the equivalent path on my server?
i've tried using '/' to get to the root but i get this error:
Warning:
require_once(/test123/mvc/views/txt/index_nav_txt.php)
[function.require-once]: failed to
open stream: No such file or directory
in
/home/user/public_html/test123/mvc/views/components/st_footer.php
on line 37
Fatal error: require_once()
[function.require]: Failed opening
required
'/test123/mvc/views/txt/index_nav_txt.php'
(include_path='.:/usr/lib/php:/usr/local/lib/php')
in
/home/user/public_html/test123/mvc/views/components/st_footer.php
on line 37

You would actually need:
require_once("/home/codlife/public_html/test123/mvc/views/txt/index_nav_txt.php");
notice the edition of /home/codlife/public_html/
The initial / Takes you to the root of the server and your code is located inside /home/codlife/public_html/

do you mean
$_SERVER['DOCUMENT_ROOT']
which basically gives you the full path to your working website directory i.e. c:/wamp/www/(windows) or /var/www/vhost/domain.com/httpdocs/ (linux)

You should probably read up on include_path ( http://php.net/include_path ) - This is generally set to include the document root (where your website is) and can be altered so that you don't have to repeatedly include the same paths.

how do i find the equivalent path on my server?
You don't find it - you tell it where it should be. Admittedly this is not always practical when you buy a hosting package (which IME are usually badly supported and come with virtually no documentation).
First thing to note is regardless of where / how the code is hosted, you should always use paths relative to the directories configured on the php include path (or relative to the PHP script initially invoked by the browser request - the '.' entry from the include_path cited in the error) - never absolute paths. You can easily find this out with:
<?php
print ini_get('include_path');
?>
Judging from the path cited in the error message, it appears to be a POSIX system. The root of the filesystem as seen by the webserver might be quite different from the root as seen from your FTP or SSH software, but they are probably the same.
Note that if this is a shared host, then you probably won't have access to put files in /usr/lib/php or /usr/local/lib/php - so your only option is to use a relative path - which is going to get very messy -
You could do some clever coding around this - but do have a look at packages such as Dokuwiki and phpmyadmin to see how they organise the include files in a relocateable way without any dependance on manipulating the php.ini settings.
Alternatively you may be able to override the include_path via .htaccess, e.g.
php_value include_path ".:/home/codlife/public_html:/usr/lib/php:/usr/local/lib/php"
(which would set a base include_path to your document root)
HTH
C.

Use a configuration file where you store things like:
$application_root = '/home/code_life/public_html/';
In this file use all your environment specific variables or constants. When you deploy the application on a different machine, you just update configuration file.
Example:
You have in your root application a folder called settings with settings.php where you can define:
define('DIR_ROOT', dirname(dirname(__FILE__)) . '/');
Now, on every machine, the DIR_ROOT will be the root of your application and you don't have to change anything.

Related

Using PHP's $_SERVER["DOCUMENT_ROOT"] under an open_basedir restriction

I've spent the past few months building a website on localhost. Throughout the site, instead of using relative paths, I used $_SERVER["DOCUMENT_ROOT"] to access the root of my server so I could reference files from there. However, I just decided to upload my site to 000webhost. As I soon found out, and for those of you who use them, you are probably aware, that their server root is different than your actual directory in which you upload your files. I believe it's virtual hosting... anyway, my $_SERVER["DOCUMENT_ROOT"] now throws errors along the lines of this on the site:
Warning: include_once() [function.include-once]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/mypath) is not within the allowed path(s)
Every other site I looked at said that you should just replace $_SERVER["DOCUMENT_ROOT"] with the home directory provided to you by 000webhost. However, if I want to change hosting services in the future, I'm screwed. Is there any way to use $_SERVER to access a set virtual directory or use htaccess or something to make my code work? I tried using DocumentRoot in a htaccess file in my root directory, but it threw a 404 error when trying to access the page.
I would never recommend using DOCUMENT_ROOT. For example, it is useless if you ever want to run scripts via the command line or cron.
I would instead, set the include_path via one of your scripts. Something that is included in every page (like some sort of config or bootstrap script).
For example, say you have config.php in your app's root directory (could be doc root but it's not important) that is included on every page
set_include_path(implode(PATH_SEPARATOR, array(
__DIR__, // this is the directory of this file, config.php
get_include_path()
)));
Then, you can safely use relative paths from this location in your include / require statements
include 'foo.php'; // APP_ROOT/foo.php
include 'somedir/bar.php'; // APP_ROOT/somedir/bar.php
When setting the include path, it doesn't really matter where the script resides, just construct the appropriate path. Say config.php lived in APP_ROOT/configs/config.php, the include path would be
realpath(__DIR__ . '/..') // realpath just resolves the path traversal
Decided to go with the following method:
http://www.000webhost.com/forum/customer-assistance/4857-document_root-environment-variable.html
Works!

Reverting to a different file when PHP fails to find a specific file (include function)

Sorry for you advanced guys, I'm actually teaching myself some PHP so this may seem like a beginner's question.
I'm using a testing server and then uploading to a remote server. The index.php file is located in "C:\XAMPP\htdocs\php_site" on my local pc and in "home/www/myname.atwebpages.com/" on the remote server. Now the code I'm trying to run is just a simple:
define ('ROOT', $_SERVER['DOCUMENT_ROOT']);
include ROOT."menu/menu.php";
This code works fine for the remote server. However, when attempted on my local machine, it spits out this error:
Warning: include(C:/XAMPP/htdocs/menu/menu.php): failed to open stream: No such file or directory in C:\XAMPP\htdocs\php_site\index.php on line 21
Clearly, it's not looking in the php_site folder. Instead, it's tying to find a menu folder in the htdocs directory, but it's not there. The menu folder is located inside the site folder, php_site. If I chance around the code to work on the local machine, it no longer works on the remote server. I'm a little confused as to how to get around this problem.
I think $_SERVER['DOCUMENT_ROOT'] is defined by apache, so you'd need to change the config there. Or, define the ROOT constant relative to where you actually put your files, so if you do something like:
define ('ROOT', dirname(__FILE__));
Put that in a constants file in the same folder as your index.php.
Your document root on the remote and local machines is different. On your local machine your document root is the htdocs directory, and the php_site folder is merely a sub-folder, and thus the path is wrong.
I suggest either making the ROOT directory be a relative directory to the index page, or have a constants file in the root directory of the PHP site that defines the root directory as the directory it is in (which would be in the php_site directory on your local machine, the same directory as your index page). define ('ROOT', dirname(__FILE)); would work in this situation.
Another idea is to use a try-catch to catch the failure of the include statement, and attempt to try another directory, perhaps using define ('ROOT', $_SERVER['DOCUMENT_ROOT']); first, and if it fails, attempt to use define ('ROOT', $_SERVER['DOCUMENT_ROOT'] . 'php_site/'); instead.

php include with wamp

I'm developing a site on my local wamp stack. I have created an alias to view the site so i go to localhost/eee/ to view it. Ideally i would like to go to www.eee.lo but ever since upgrading to win8 I can't get it to work.
So this is the problem, i'm making modules for the website so i don't have to change all the code etc... And i don't want to have to go around changing all the url's when i migrate to the online server so i'm creating a file called _control.php which has this;
$_SITELOC = "localhost/eee/";
And then each time i want to include a file i will go;
include "$_SITELOC/scripts/inc/_header.php";
But this doesn't work and i can't work out why as if i echo it rather than include it and then i take what it prints and put it into the url it goes to the correct file. But it throws errors on the include, it gives two warnins;
Warning: include(localhost/eee/scripts/inc/_header.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'localhost/eee/scripts/inc/_header.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
I read somewhere that it might be to do with the include path so i tried;
set_include_path(get_include_path() . PATH_SEPARATOR . $_SITELOC."/scripts/inc/");
but this too did not work and now i'm not sure where to go.
Thanks, Chris
localhost/eee/ is your public address that you can use in your web browser. This public address should more appropriately be written as http://localhost/eee/. When you move to web server, you get the public address http://www.eee.lo/.
When including files, you have to use file paths. For example, if you have your www (or httpd, whatever) directry in D:\ on windows, then your include path should start with D:\www\eee\.
So, basically you have to use two variables to keep paths.
$_SITELOC = "http://localhost/eee/"; //For all URLs used in your HTML document.
$_INCPATH = "D:\www\eee\\"; //For all internal file includes.
In practice, you will need both of these, and it is good practice to keep the website address and internal paths out of your main script because when uploaded to remote server, not only your public address changes, but you will also have to deal with absolutely different internal (include) paths.
Your idea is basically good, to define one (root) path of the application and include files based on it, but unfortunately you're not doing it quite right. You have basically two ways of doing that.
One way (which I personally find better) is to include local files in your file system, where you can define the root path, i.e. like
define ('ROOT', 'your/document/root/path');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
The other way would be to include a web resource, what you're trying to do, but you've forgotten to specify the scheme (protocol) you want to use, i.e.
define ('ROOT', 'http://localhost/eee');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
For more information, see the examples, provided by the documentation for include
Note: If you want to include the source of a php file, i.e. file with definitions of functions, etc., use the first approach. Including files, using the second approach will only include the output produced by that file.
If you include() a URL, you will (probably) be including the output of the script's execution, when you want to include the script's source. It seems like you actually want to include by local file system path.

PHP Relative Include with Virtual Directory on IIS7

I have a root configuration virtual directory (named "config" pointing to a directory inside /wwwroot/) specified in IIS7. I want to access this virtual dir programmatically via an "include" function such as:
include '/config/main.php';
I get Warning: require(config/main.php) [function.require]: failed to open stream errors. I have tried the following:
/config/, \config\, ./config/, config/, ~/config/, $SERVER_['DOCUMENT_ROOT']/config/
To no avail. Why does PHP have to be such a dink about site relative references!? Not to rain on the parade, but ASP can handle site relative references (or document relative with ../blah.php) with ease!
Any ideas? I have looked at the set_include_path function and I'm going to try that out in the meantime, but it basically seems like that would be hard-coding my directory paths - annoying for moving code from dev to prod environments.
Try it:
dirname(__FILE__).'\\config\\main.php';
Issue is sort of closed because I couldn't find a satisfactory answer, I simply just rejigged my problem so that there's no virtual directories. Damn PHP and relative paths! It seems to be a problem specifically with IIS.
If anyone finds a better solution the problem I will be more than happy to switch the selected answer.

Warning on Include: Can't Find File In Correct Path?

Warning: include(/2008/2009/assets/inc/base/error.inc.php) [function.include]:
failed to open stream: No such file or directory in
C:\Program Files\Apache Group\Apache2\htdocs\2008\2009\assets\inc\base\header.inc.php on line 82
I am receiving the above error when including a file on a WAMP setup. Is it possible that Windows is interpreting the /2008/ to mean c:/2008 rather than the actual http://localhost/2008?
To give a bit more detail, I have a constant defined as ROOT that I use all over my site. (ROOT in this instance is set to /2008/2009/). I use ROOT before paths for images, links, css files, include/require files, etc. The CSS and links are properly scaling to localhost/2008/2009, but include files are not being found.
Any help would be greatly appreciated.
PS php.ini has include path defined as:
include_path = C:\Program Files\Apache Group\Apache2\htdocs"
Update
I was using URL rewriting to change item/x to item-display.php?id=x, this change in folders is what ROOT was built off of. This made anything displayed to the browser (imgs, links, etc) all work flawlessly, but made anything internal not work, as item-display.php is actually one folder down. I moved item-display.php to an item folder, and this made the actions consistent both client side and server side.
You’re using an absolute path rather than a relative path.
If you are using the include path, you have to specify a relative URL, starting with the first directory or ./. Btw, you should define your path with something like dirname(__FILE__) in a file whose position in your project is not going to change or by looking into server. Otherwise, installing your application on another server is going to be unnecessarily complicated.

Categories