Solved

Problem with include_once()

Posted on 2003-12-08
4
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:
Richard Quadling 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:Richard Quadling
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

730 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