Solved

Problem with include_once()

Posted on 2003-12-08
4
834 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

758 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

12 Experts available now in Live!

Get 1:1 Help Now