Solved

Problem with include_once()

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

778 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