0

I have a DB file content my DB info. the file db.php located on my mother's folder [here apps folder]. Now, how can I include the db.php file anywhere on the project using base_url()? Here is my base url function:

function base_url() {
    return "http://localhost/apps";
}

I tried like this way but it does not work.

$link=base_url()."/"."db.php";      
include('$link');

Can anybody please help>

Thanks riad

2
  • What do you mean by "not works"? Do you get a compile error, or the output behavior is different? Commented Jul 19, 2010 at 10:27
  • Why it doesn't work? Wich error message do you get? Commented Jul 19, 2010 at 10:27

4 Answers 4

3

base_url() needs to return a file path here, not a http:// URL.

(It would then probably make sense to rename it to base_path())

Sign up to request clarification or add additional context in comments.

2 Comments

but the base url i used whole the porject..so the filename cannot be return.that's why i added the file name after the base_url function. the $link variable properly shown the result.But cannot include the fiel.OR any other way u know to include the file using url??
@riad including the file through an URL is stupid unless it is what you specifically want. Any PHP code in it will be parsed before inclusion, and a web server process started. You will usually want to use a file path.
2

multiple problems.

  1. include('$link'); will never ever work because the single quotes mean the literal string $link and not the value of the variable, you'll be needing to use include($link) or include("$link");
  2. include is a language construct not a function, so you only need include $link;
  3. stipulating "/" as the directory separator could cause problems, PHP provides a constant for you to use DIRECTORY_SEPARATOR
  4. your base_url function should return a file path not an URL
  5. base_url should probably just be a constant seeing as it won't change

here's a version which should work for you

function application_root()
{
  return dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
$link = application_root() . 'db.php';
include $link;

3 Comments

I disagree with 2. and 3. and not sure about 5. but +1 for noticing 1. :)
@Pekka unsure how you can disagree with 2?? include simply is a construct - as for 3, if the script has to be any kind of portable, then best to use the native OS directory separator perhaps..
Re 2) I meant, it is fine to put the argument into brackets for consistency's sake. Your statement is correct though, they are not needed. Re 3) Nope, separators are automatically translated on Windows as well, it is usually okay to use forward slashes.
1
include('../db.php');

"../" will reapeat (eg '../../../db.php') as depth of the file from which you are including

you can use $_SERVER['DOCUMENT_ROOT'] var as the base URL/PATH too

2 Comments

I think having to deal with ../.. is what he wants to avoid with his function.
thanks sadat.i use $link =$_SERVER['DOCUMENT_ROOT']."/apps/db.php"; include $link; it works..
0

Within single quotes variable occurrences like your $link are not substituted by their values. But you can simply write this:

$link = base_url()."/db.php";
include $link;

Now if this works as expected depends on whether you want the source code of db.php to be included or just the output of db.php. Because working on the filesystem gets you the source code while requesting it via HTTP gets you the output.

1 Comment

@riad yes, because you are using a URL (additionally, an incorrect one) as your inclusion path. I'll bet you a beer it's not what you want. But I've said it thrice now, I'm not going to repeat myself any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.