Solved

Problem with include_once()

Posted on 2003-12-08
4
835 Views
Last Modified: 2013-12-13
Hi there,

i have a problem with include_once function,

at the begining of the page,
i have this line of code

a.php
----------------------------
<?php
include_once('AllFunctions.inc');
......
......
.....
?>

In AllFunctions.inc has these code
-------------------------------------------
<?php
include_once('FunctionA.inc');
?>

when i try to access a.php
it returns this error:
Warning: Failed opening 'FunctionA.inc' for inclusion (include_path='.:/usr/local/lib/php') in ../inc/AllFunctions.inc on line 2

i know it has got something to do with the include_path
the include_path from phpinfo():
.:/usr/local/lib/php

how do i fix the problem??

Thank you very much~
0
Comment
Question by:Slowang
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9896767
either supply a path to the function that is valid from the current script.
Or add path's to the default path where else to look.

So if the file you want to include is in the same folder as the current script you can simply do this:
include_once('myscript.php');

if the file is not in the same folder as the current script; but in a folder that is included in the "include_path" setting you can also do this:
include_once('myscript.php');

otherwise you will have to supply a complete path to the file in the functioncall:
include_once('/folder/subfolder/myscript.php');
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 250 total points
ID: 9904011
1 - Is FunctionsA.inc actually available?

2 - include/include_once/require/require_once are not functions and you do not need to write them as so.

e.g.

include "include.inc";

is perfectly fine and is recommended as functions return values and include() does not.

3 - check with phpinfo(); the value of the include_path.

As a default, it starts with ...

.

This means the current path.

So, say the *.inc files are in the webroot (bad idea and why you have an include_path option!).

http://www.somesite.com/page.php

If page.php has some include files, then they (assuming they are in the webroot) would need to be in the same directory as page.php.

e.g.

/home/site1/public_html
 page.php
 include.inc

Now say the page www.somesite.com/catalog/another_page.php is called and this page requires the _same_ include files.

You will NOT be able to ...

include "include.inc";

as the include.inc file is NOT in the directory catalog!



Solutions.

1 - Don't use any sort of directory structure for your site and have all your php files and the include files in the webroot.

(EEK!) This is more of an idea of what most websites are like. I would not really suggest using this at all!

2 - Edit the php.ini (or the *ix equivalent) and add in a path to the include_path setting to something like ...

/home/site1/php_includes

and place all your include files here.

(If you own the server, then this is probably the safest one).

3 - If you are using PHP V4.3.0+, then use the functions get_include_path() and set_include_path() to add the path in option 2 to the include path.

If you do not own the server, then this is the next best choice.

Basically, for the duration of the script, you can add a directory to the inclulde path and it will be available for the scripts.

<?php
$sOrigInclude = set_include_path(get_include_path() . ';/home/site1/includes');
?>

If you are on an earlier version, then you can use the traditional ini_get()/ini_set() functions.

Regards,

Richard.
0
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9912172
[quote]2 - include/include_once/require/require_once are not functions and you do not need to write them as so.

e.g.

include "include.inc";

is perfectly fine and is recommended as functions return values and include() does not.[/quote]

true, those are not functions according to the manual.
Recommended to remove the ( ) ?
I don't agree, as with () it becomes more clear to what is related to the include and what is not; I find that my code is much more easy to read that way.
If you prefer to not use the () that is perfectly fine.

the statement that include does not return a value is however false:
from the php-manual: http://www.php.net/manual/en/function.include.php
---------
Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function.
---------

another value that is returned is true or false --> was the include succesfull or not?!?
in short: include's allways return a value!
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 9912294
Aha!

Absolutely right.

I've read the include help fine, I got ...

Note: Because this is a language construct and not a function, it cannot be called using variable functions

mixed up in my head.

As I always use require_once, I rarely have to deal with include'd code.

Richard.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Permalink issue in Wordpress archive 1 32
.php tree directory? 5 55
php refresh button on the browser 2 35
PHP: concatenate query 12 35
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now