[PHP Includes] Simple - but confused?

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

I am stuck on using an Include in PHP. I have tried:

Basic Include:
Code:
<?php
include 'includes/theader.inc';
include 'tutorials.inc';
include 'includes/tfooter.inc';
?>

Http Include:

Code:
<?php
include 'http://www.bf2tricks.ubersol.co.uk/verynew/includes/theader.inc';
include 'http://www.bf2tricks.ubersol.co.uk/verynew/tutorials.inc';
include 'http://www.bf2tricks.ubersol.co.uk/verynew/includes/tfooter.inc';
?>

The three files are:

theader.inc (In an includes folder)
tfooter.inc (In an includes folder)
tutorials.php (In the same folder)

I am trying to include them all together to make my page and I get nothing: Click Here. What am I doing wrong?

Thanks...

[Edit]

As there is no index page as of yet, you can view my file structure: Click Here
 
Last edited:
are your file paths right? :confused:

oh... and you may want to put error_reporting(E_ALL); at the top of your page so E_NOTICE errors are revealed for more info...
 
Hi there,

Thanks for the response. Yes I believe that all the paths are correct. You can check yourself by looking at my file structure: Click Here

I have added the error reporting, but nothing happens either, unless I have to look in a specific area for a log of errors? (Never used error reporting before)

Thanks...
 
Beansprout said:
Seeing as www.ubersol.co.uk doesn't even work I wouldn't hold out much hope of, well, anything working.

Hmm, :(.........well I payed 11p I can't really complain :D. I can access my cPanel OK.

Beansprout said:
What's the code for the PHP files?

Sorry I don't understand what you mean by this? All the PHP that is in the "site" is shown there.

Thanks...
 
As in the source code for those php files. Put the files up as .phps files too - the source code will then be displayed instead of parsed :)

Also try a phpinfo(); to see if things are alive.
 
Hehe :)

Thanks for all your help anyway...theres my 11p down the drain :(

Thanks...

[edit]

Just looked at my error log in cPanel and there are 100s of "PHP Warnings"......:(
 
Last edited:
Use the basic includes, but shove a chdir(<directory>) in before the includes (where <directory> is the directory you expected the script to run fron - use the full path).
 
Why would you ever do that? Just do this:

config.php:
Code:
<?php

define('PATH', dirname(__FILE__));

// other settings here

?>

includes/main.php:
Code:
<?php

require_once dirname(__FILE__).'/../config.php';

// We can no use PATH to correspond to the base directory one level below this.
require_once PATH.'/includes/somethingelse.php';

// set up database connection and whatnot

?>

foo.php:

Code:
<?php

require_once 'includes/main.php';

// we are now connected to the database as well as being able to access PATH

?>
 
Dj_Jestar said:
Code:
require_once dirname(__FILE__).'/../config.php';
can become:

Code:
require_once realpath('../config.php');

:)


But in foobar.php, realpath('../'); would (IIRC, it's a long time since I've looked into it) correspond to the directory below foobar.php and not the directory below "includes", hence using __FILE__ which corresponds to the actual filename of the script, regardless of whether it's being included in another script.
 
__FILE__ would also relate to which ever file is doing the including, unfortunately. :)

am 90% sure anyway.

The best bet is to have a constant or variable stored to maintain the location of your includes as well as the base path.

config.php:
Code:
if ((!defined('INCLUDES')) && (($includes = realpath('../includes')) !== false)) {
    define('INCLUDES', $includes);
} else {
    die("Specified path: '{$includes}' does not exist");
}
unset($includes);

if ((!defined('BASEPATH')) && (($path = realpath('/path/to/base/dir/')) !== false)) {
    define('BASEPATH', $path);
} else {
    die("Specified path: '{$path}' does not exist");
}
unset($path);

main.php:
Code:
require_once(BASEPATH . 'config.php');
require_once(INCLUDES . 'foobar.php');

and foobar.php:
Code:
require_once(INCLUDES . 'foobar2.php');

On a sidenote, some argue that using constants for details such as directories, and more specifically login credentials (DB's mostly) is more of a risk than using plain old variables which you can unset() once finished with, preventing the minute possibility that someone can inject code to display all current defines and variables, but that's a different topic.
 
Last edited:
robmiller said:
Why would you ever do that? Just do this:
It's a bit of a PHP oddity. Some sites have open_basedir restrictions set, and using the full path can violate those checks. For some reason, the chdir route tends to work better.

In most cases either will work. :)
 
hmm.. just noticed my if's are slightly bugged.. :o

Code:
$path = '../includes';

if ((!defined('INCLUDES')) && (($dest = realpath($path)) !== false)) {
    define('INCLUDES', $dest);
} else {
    die("Specified Path Not Found: {$path}");
}

unset($path);
 
Back
Top Bottom