Forum Moderators: coopster
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
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
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;
}
}
}
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");
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