user3422952
user3422952

Reputation: 317

PHP - include function directory path

I'm having trouble including a header in my website. The code I'm using is:

<?php include_once('header.php'); ?>

The page that I'm including this PHP code on is a PHP page itself, so surely it should be working?

The page is located inside folders, e.g. "http://mydomain.com/stuff/morestuff/index.php" Whereas on the FTP the header.php is located at "http://mydomain.com/header.php" - is this why it's not including the header file? Because it can't find it?

But when I do:

<?php include_once('http://mydomain.com/header.php'); ?>

There aren't any errors but the page is blank.

How do I fix this? Basically, how do I include a header on my website?

How do I access the "header.php" file that is located at the root of my website directory?

Upvotes: 1

Views: 91

Answers (5)

michal.hubczyk
michal.hubczyk

Reputation: 697

I think that PravinS' answer should work. Your way is also correct, but I think that you maight have disabled allow_url_include in php.ini.

Just use /header.php, and leave allow_url_include option disabled for security reasons (unless you need it).

Upvotes: 0

Paul Kotets
Paul Kotets

Reputation: 209

Use error_reporting(-1); and ini_set('display_errors', true); in the top of your main file in order to show all errors. There will be a PHP warning with detailed description why file isn't included

Upvotes: 0

Fabio
Fabio

Reputation: 23480

You need to navigate two folder up from index script

<?php include_once('../../header.php'); ?>

You can always use ../ to navigate in upper folder.

From documentation

For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

Upvotes: 1

Boopathi Rajan
Boopathi Rajan

Reputation: 1210

try this

<?php include_once('../../header.php'); ?>

Upvotes: 2

PravinS
PravinS

Reputation: 2584

include header.php like this

<?php include_once('/header.php'); ?>

Upvotes: 1

Related Questions