Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with include_once()

Posted on 2003-12-08
4
Medium Priority
?
842 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 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

610 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