Empty `include_path` in PHP default - php

If I have an empty include_path in PHP (like include_path=:), my include and require functions still work. It is like include tries the initial script's directory first, regardless of what your include_path is set to.
This gets more confusing to me when I set the include_path to something like /usr/lib/php5/, and then try to include('test.php') from a script that has no test.php in the same directory. I would imagine it would look for test.php in my specified include_path if it wasn't present in the current directory.
I am not looking for a solution; I can already solve my current problem. I am looking for an explanation as to how include_path works. Thanks!

basically if you just speicfy the name of the file to be included without any path info then then include_path will be used.
i.e.
include 'wow.php'; (this will use include_path to try to find it)
if it cant find it then it will look in the same directory as the file that tried to include it.
include '/blah/wow.php';
because this has some path info in it then it will ignore include_path all together.

Use this
include($_SERVER['DOCUMENT_ROOT'].'/test.php');

Related

PHP's include_path setting

I'm sure I'm just being a bit stupid, but the include_path docs seem to be a bit confusing; what is
Using a . in the include path allows for relative includes as it means the
current directory. However, it is more efficient to explicitly use include
'./file' than having PHP always check the current directory for every include.
actually trying to say?
what is
Using a . in the include path allows for relative includes as it means the
current directory. However, it is more efficient to explicitly use include
'./file' than having PHP always check the current directory for every include.
actually trying to say?
Assume you want to use include_path to search in folder /somepath/foo/includes for include files when you do not specify the path explicitly. You may have most of your include files laying there, so that should be not too inefficient.
Now you are including baz.php, xzy.php and 123.php from that directory – and if you have given . first in the include_path, then PHP will search the current directory first always, although in most cases the file will be located in the /somepath/foo/includes folder.
If you don’t specify . at the beginning of your include path and use ./abc.php explicitly when you are including a file from the current directory (special case, not one of your “normal” includes), then for all other of your include files that you just include using baz.php, xzy.php and 123.php, PHP will not have to search the current directory first – which is useless, since those files are located in your special include folder; so PHP can go look there in the first place.
It is talking about dot at the begining in the include_path.
Form PHP Documentation : If filename begins with ./ or ../, it is looked only in the current working directory.
./ is the current directory. It is largely the same as just someFile.php.In many cases \it doesn't check any standard places PHP might look for a file, instead checking only the current directory.
Edited. 'more efficient' means that it better to set path in such way with dot. In other way it will try to find not only in the current working directory. So, it can be a problem if you want to include file in the some directory, but the file is not exist, it will try to find in other path, and it can take some time and make wrong include or just an error.

PHP Polymorphic Root Requires

I have a directory/file tree as follows:
index.php
/frame/main_class.php
/frame/func/function_1.php
/frame/func/function_1.php
/cfg/config.php
//index.php
require('frame/main_class.php');
new main_class;
//frame/main_class.php
class main_class{
public function __construct(){
require('func/function_1.php');
require('func/function_2.php');
require('cfg/config.php');
}
}
The weird part is that it works. Maybe it is late and I am having a dumb-moment, but shouldn't "require('cfg/config');" be written "require('../cfg/config.php');" ?
And if it is using the root of index.php, then "require('func/function_1.php');" shouldn't work, right?
I have quadruple checked the remote server thinking that maybe there was a stray file or two... there isn't.
How can the two require statements have a different base path.....?
Does anyone know of a code snippet that could cause this to happen? I am working with some $_SERVER variables but I don't appear to be changing any of them....!?
"Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing." Explicitly saying include dirname(__FILE__) . '/path/to/file.php';avoids this confusion. – DCoder
Link to PHP Manual on "dirname".
The PHP engine will look for the requested files in the current directory, but it will also look for them in the list of paths defined in INCLUDE_PATH. If the include path lists the path from where your script is running then the given code will work. If not then it wont.
For that reason amongst others it's not a good idea to rely on the include path to resolve the path of included files. You should give the full path instead.

PHP absolute path in requireonce

I'm using a simple pre-made authorisation tool to secure my site. The tool requires this line of code to be applied to the top of every .php page. Auth.php lives on the root level.
<?php ${(require_once('Auth.php'))}->protectme(); ?>
I need to be able to add this line of code to every file, including files in sub-folders. But I'm unsure of how to apply the method as shown in require_once in php to ensure it always is linked absolutely, alongside the protectme(); function.
Any enlightenment is appreciated.
Cheers.
Set a variable to the absolute path. $_SERVER['DOCUMENT_ROOT'] is a PHP global variable that stores the servers, root path. You just need to insert the rest of the path information to the script. For instance if your website exists on /var/www/ and your script exists at /var/www/scripts you would do the following.
$path = $_SERVER['DOCUMENT_ROOT'] . '/scripts/Auth.php';
require_once($path);
You can use a relative path to get to it.
One level up: ../Auth.php
Two levels up: ../../Auth.php
etc.
You should alter your php.ini to change the include path to the the path to that (all all your other) included files. Then you can include them without a path on every page regardless of their own location.
More Details
Add the root level directory to your include_path - PHP will do the rest for you. No complicated variables, constants or whatever.
In addition to everything that has been said already, I suggest centralizing all common functionality. Create a file, like common.php with all includes that you need for you application and then include that from all your scripts.
Also, a nice way to do relative includes is by using dirname() function in combination with __FILE__ magic constant. For example:
require_once dirname(__FILE__) . '/../lib/common.php';
If you do not have access to php.ini, I've used something like this before
include $_SERVER['DOCUMENT_ROOT']."/"."include_me.php";
The easiest way since it's a site wide change is to add its directory first in the include path in php.ini.
If you can't change php.ini, there are a few other options for adding it to the include path.

Explanation for include/require functions behavior needed

I did a lot of reading but I cannot answer one question I have with php include behavior.
On php.net it says that it looks into the directories that are in include_path variable. After that it looks in the current directory. If I put relative path (starting with dot) or absolute path it ignores the include_path.
So far so good.
I get confused when I see examples on the internet that start with something like that:
include('LibName/SomeFile.php');
Would php take every path from include_path and append 'LibName/SomeFile.php to look for the file? What is the behavior?
It's exactly what you said it is. It checks each directory in include_path to see if 'LibName/SomeFile.php' references a file relative to those paths. If not, it tries the current directory.
PHP considers each entry in the include path separately when looking for files to include. It will check the first path, and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error. You may modify or set your include path at runtime using set_include_path().
You can find more info here: http://www.php.net/manual/en/ini.core.php#ini.include-path

PHP include file

i have two files:(localhost/template/)
index.php
template.php
each time when i create an article(an article system is what i'm trying to do),i create a folder and then i copy the index.php in that folder. I want to include template php in index.php but as a static url('cause the articles will be like a folder-subfolder/subfolder/.. structure )
i tried: include('localhost/template/template.php') with no result. how should i include it? thanks
The include method works on the file system path, not the "url path". Solution is to either
Give it an absolute path.
-- include('/some/path/to/template.php');
Change the relative path so it is correct after each copy you create.
-- include('../../template.php');
Change the include path of PHP so that the file is in, well, the include path.
-- Can be done either in the php.ini file, in the .htaccess file or with the set_include_path function. (Depending on how much you want to set it for, and what you have permission for)
You could include it relative to the current directory, like so:
require_once(dirname(__FILE__) . '/template.php');
dirname(FILE) will translate to the directory of the current script (index.php) and the line above will append '/template.php' resulting in the full path to the template.php side-by-side to the index.php file.
I find it best to include files this way vs without a full path to avoid issues with the PHP search path, for example. It's very explicit this way.
UPDATE: I misunderstood the original question. It sounds like template.php isn't copied, only index.php is. So you'll have something that could be like:
template/template.php
template/index.php (just a template)
foo/bar/index.php
foo/bar2/index.php
Since people can hit the foo/bar/index.php for example without funneling through a central script, you'll have to somehow find the template no matter where you are.
You can do this by setting the PHP include_path, for example through a .htaccess on a Apache server:
php_value include_path ".:/home/<snip>/template"
Then in each index.php you can include template.php and it'll search the current directory first, then try your template directory.
You could also compute the relative path when copying the script and put an include in there with the proper number of '..' to get out (e.g. '../../template/template.php'). It's kinda fragile, though.
You don't want the "localhost" in there. Includes work using the actual path on your server.
So you can either use relative ones such as posted above, or absolute in terms of server so this could be "/opt/www/" on linux or "c:\Program Files\Apache\htdocs" on windows. This will be different from system to system so to find out yours use the dirname(__FILE__) technique shown by wojo.
If you're trying to include the file as an url, you'll need to start it with http:// and have allow_url_include set to true in PHP settings. This is highly discouraged as it opens doors for security breaches.
Instead, you should either add localhost/template to your include path or use relative urls like include('../template.php').
The path looks wrong, you should include it with a path relative to where the calling file is, e.g. include('template/template.php'); or include('../template/template.php');

Categories