A very annoying require() -and include()- problem.

From: Zaan (removethis_zaan_at_removethis_gmx.net)
Date: 01/30/04


Date: Fri, 30 Jan 2004 16:43:14 +0100


[I'm working on PHP version 4.3.4 (which may be of importance, I don't
know). It's the vanilla precompiled www.entropy.ch release for Mac OS
X.]

Hello,

My issue is the following: to promote consistency on a site I'm
building, I decided to go for a php scheme where some recurring
elements (navigation, mostly) are written in a file of their own, which
would then get called from each file needing it by the function
require(). The following example may make this clear (the HTML is
clipped somewhat, but is W3C compliant in full...):

Suppose this is the main file, text.php

   <?php require('./lib/head_1.php'); ?>
   <TITLE>text</TITLE>
   <?php require('./lib/head_2.php'); ?>
   the actual body text, html formatted, would go here.
   <?php require './lib/foot.php'; ?>

Then, head_1.php would look like:

   <?php $nav_basis = "http://localhost/~username/; ?> // will
change once the site is done and is uploaded to the actual server, of
course.
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <HTML>
   <HEAD>
   <LINK rel="icon" href="<?=$nav_basis?>favicon.ico"
type="image/x-icon">
   <LINK rel="shortcut icon" href="<?=$nav_basis?>favicon.ico"
type="image/x-icon">
   <LINK rel="style***" href="<?=$nav_basis?>lib/zaan.css"
type="text/css">

and head_2.php would look like:

   </HEAD>
   <BODY>
   <div>

etcetera, in short: navigational elements with css formatting.

The main point is the variable $nav_basis, which is needed because not
all my files are in the same directory, and I would really would like
to have one file with standard stuff that works throughout the site as
it should. You might suggest using the <BASE> tag in html, but that is
no good: many files on the site are older and large html files with
ample relative crossreferences, which will irrevocably get broken by
such a tag, not to mention get a lot more confusing during future
updates.

My problem now is that Apache, or its PHP module won't properly parse
the PHP that I require(). I do not understand this. The PHP manual
seems to imply that PHP code in an require()ed file will be executed if
tagged properly, but I am having no luck.
As a result, the source file sent to my browser (by Apache, mind you,
not the local filesystem) contains the two head_x.php files as clear
text, showing <?php tags and everything as they are in the source file,
and nothing executed, obviously.
PHP code that I'd write in the main file (text.php) would get executed,
however.
include() shows the same behaviour.

I wonder what the problem is here. Is it my installation of PHP that is
faulty, is it something that I forgot, is it something else?

Can anybody help?

Thanks in advance,
Zaan


Quantcast