Forum Moderators: coopster

Message Too Old, No Replies

problem with require(); function

         

ahmed24

9:00 am on Sep 6, 2006 (gmt 0)

10+ Year Member



hi,

i am using the require(); function within a script of mine in the following way:

require('includes/config.php');

i have problems when i put the file name as includes/config.php. however, if i put the file in the same folder as the script and put the include path as:

require('config.php');

then it seems to work but it doesnt work when i put require('includes/config.php');

it gives me error saying Undefined variable.

can anyone help me on this issue.

thank you

vincevincevince

9:02 am on Sep 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The safest way is to use absolute server paths, e.g.
require('/var/www/html/includes/config.php');
or, automatically:
requires($_SERVER['DOCUMENT_ROOT'].'includes/config.php');

tomda

9:09 am on Sep 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Open the phpinfo() of your host and find out what is the _SERVER["REDIRECT_PHP_INCLUDE_PATH"] which is the default path for include files.

If none is set than creating a folder include/ and using require("include/config.php") should work.

But the best practice is to modify your ini.file like this - ini_set("include_path", "YOUR_PATH\include"); or ask your host to do it if you can't have access to ini - so that require(config.php); will by default get the file in the include folder.

My 2 cents

ahmed24

9:19 am on Sep 6, 2006 (gmt 0)

10+ Year Member



i've tried both options but i am still getting error notice saying Undefined variable, it only seems to work if i use the include in the same folder.

any suggestions?

tomda

9:49 am on Sep 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please, let us know what is your error message.

The error message should be
Warning: main(includes/conf.php): failed to open stream: No such file or directory in PATH

and not Undefined variable as you mentionned!

ahmed24

10:32 am on Sep 6, 2006 (gmt 0)

10+ Year Member



error message is:

Notice: Undefined variable: old_news in C:\Domains\1412\wwwroot\test.php on line 18

but the funny thing is, if i keep the config file in the same location as the script and remove the includes/ then i do not get that error.

jrlooney

1:02 pm on Sep 6, 2006 (gmt 0)

10+ Year Member



can you post code so we can see where the variable is being used?

ahmed24

1:15 pm on Sep 6, 2006 (gmt 0)

10+ Year Member




require('includes/config.php');
$filename = "includes/summary.html";


if(file_exists($filename)){
$fh = fopen($filename, "r");
$old_news = fread($fh, filesize($filename));
fclose($fh);
}


$articles = explode("<!--ARTICLE-->", $old_news);


$i=0;
foreach ( $articles as $article ){
if(count($articles)>$i){
if($max_latest >= $i++){
print $article;
}
}
}

tomda

1:25 pm on Sep 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post also the code (OR A PART OF IT) of the config.php.
I guess that $old_news is set in this file.

ahmed24

1:33 pm on Sep 6, 2006 (gmt 0)

10+ Year Member



the config.php file doesnt define $old_news variable, it only contains the following variable:


$max_summary = 10;
$max_latest = 10;
$password = "test";

vincevincevince

12:40 am on Sep 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suspect that config.php is require()ing or include()ing another file using a relative path.

You will need to change this internal reference to an absolute path in the same way as above for the reference to config.php itself.

Everything is relative to the script you first run, so even if config.php is in "includes", if your main script is at the level above "includes", config.php can't use "otherinclude.php" to access something else in "includes". It would need to write "includes/otherinclude.php" or "/full/server/path/includes/otherinclude.php".

The use of the variable $_SERVER[DOCUMENT_ROOT] is very useful as it provides the path to the directory your index page is in. Get into the habit of writing include("$_SERVER[DOCUMENT_ROOT]/includes/config.php"); instead of just include("includes/confing.php");

ahmed24

7:16 am on Sep 7, 2006 (gmt 0)

10+ Year Member



the config.php file is not require()ing or include()ing another file at all. those are the only 3 lines of code i have in the config file (variable defination only)

tomda

7:23 am on Sep 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK Ahmed, please read carefully our comments above.

1/ The problem is that your $old_news is not defined.

2/ Is it not defined because it fails to open your $filename I guess

3/ It fails to open your $filename because you need to put the entire path (follow Vincevince's example), therefore
$filename = "".$_SERVER[DOCUMENT_ROOT]."/includes/summary.html";

Try it and report back
Cheers

ahmed24

1:33 pm on Sep 7, 2006 (gmt 0)

10+ Year Member



i got it to work, i put all the config file variables within the script and took out the config include and then did this to the $filename variable, and it's working fine now. Thanks for all your help

$filename = "".$_SERVER['DOCUMENT_ROOT']."/includes/summary.html";