Tom Tom - 3 months ago 11
PHP Question

Included PHP file cannot access its parent's variables

I'm using

include
to make my website's headers, navigation and footer easier to edit.

I've got that working, but I need the title to change per page.
index.php
is my page, and
head.php
is the file containing my headers.

index.php

<html>
<head>
<?php
$page_title = "Home";
include("http://www.example.com/head.php");
?>
</head>
<body>
<?php include("http://www.example.com/navigation.php"); ?>
<h3>Welcome</h3>
<p>example.com</p>
</div>

<?php include("http://www.example.com/footer.php"); ?>
</body>
</html>


head.php

<title>
<?php echo $page_title; ?>
</title>


I am using IIS and have set
allow_url_include = On
in my
php.ini
file.

The problem is that the title is never set. In Chrome's developer tools, the headers are like this:

<title>
</title>


The variable isn't being parsed from the parent file to the included file, but I've read that this is meant to work perfectly.

Tom Tom
Answer

After some more testing, I found that you can't parse variables from a parent PHP file to an included one when the page is loaded externally, ie using allow_url_include = On in php.ini. I used this to avoid the issues referencing web root with PHP, but it turns out this is the way to do it.