newbie: setting the page title

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Stupid newbie question:

I want all of my web pages to look like this

<?php $title = 'My site';
include(' ');  ?>
.... HTML of page body goes here
<?php include(' ');  ?>

But, I can't even get the first bit working :-(

here is page_begin.php

<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
<link type="text/css" rel="stylesheet" href="/layout.css">
<TITLE>' <?php print $title ?>  '</TITLE>

The $title evaluates to an empty string - what obvious thing am I
overlooking? I thought that include would inline the php file, thus
making $title available.

2nd question. I tried making the body of page_begin.php a function, so
that I could use this in my index.php

<?php include(' ');
writeHeader('My site');  ?>

but I got
Fatal error: Call to undefined function: writeheader() in
/home/mySite/public_html/index2.php on line 1

what gives?

Re: newbie: setting the page title

 ( decided we needed to hear...
Quoted text here. Click to load it

Your problem is with the include lines. Whilst you *could*
include from a URL (if allow_url_fopen is turned on), it
probably does not do exactly what you expect.
Instead, use a local file path to your include files, such
as include('layout/page_begin.php'); if page_begin.php is
in the layout subdir of the current script's dir.

Quoted text here. Click to load it

(Remove REMOVE for email address)

Re: newbie: setting the page title

Thanks Dave.

I wonde rif it is a server configuration probelm.  When I try a path
without http I get

Warning: main(/layout/page_begin.php): failed to open stream: No such
file or directory in /home/mysite/public_html/index2.php on line 1

oh, sh*t !!!!     it seems that I have to

what a weird setup!

Surely an absoulte path should be /layout, not

oh, well, at least I know now, and I would never have discovered it,
had I not had to explain it to you.

Thanx a 1,000,000

Re: newbie: setting the page title wrote:
Quoted text here. Click to load it

Yes and no.  But it makes sense when you think about it...

/layout is the absolute path relative to the document root for the site
(/home/mysite/public_html).  But /home/mysite/public_html/layout is the
absolute path to the file relative to the file system itself.

Using /layout is fine when you're accessing something through the web
server - the server automagically prepends the document root value to
the file name before making a call to the system.

However, PHP accesses the file system directory - it doesn't go through
the web server.  So it doesn't prepend the document root to the
filename.  So, if you just use /layout, it will look for the file in
/layout, instead of /home/mysite/public_html/layout.

The easiest way to handle it is to just prepend the document root
yourself.  This will make the absolute path completely transparent, and
if you change web servers you can still access the file:

   include($_SERVER['DOCUMENT_ROOT'] . '/layout/page_begin.php');

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: newbie: setting the page title

For more clarification, you are obviously working on a Unix or Linux
machine.  If you are familiar with Windows, think of it this way:

You are trying to open the file \layout\page_begin.php, which the
system wouldn't be able to find.  For the Windows machine, it would
have to be the entire path, which would be
c:\inetpub\wwwroot\site\layout\page_begin.pgp, or something similar.
Your full path on your server just starts with your /home directory
instead of c:\

About including from http, Dave had it right when he said it doesn't do
what you expect, but he didn't explain that.  When you include a php
file from a web address ( ) the server
executes that php file and sends you the output.  So if the php file


echo '$title = "some title";';


Then the server would execute that line, and send the string '$title =
"some title";' to the script requesting it.  Since your include file
probably didn't have any echo statements, no output was being sent and
that's why it was blank.  This is correct though, you don't want your
php files outputting php code (generally), and so the correct way to
include a file is without the http, you need to go through the local
filesystem instead, which reads the actual code of the file as opposed
to the output that it sends out.

Re: newbie: setting the page title

Steve wrote:
Quoted text here. Click to load it


Actually, I am much more familiar with Windows machines.  I've been
programming them since Dos 1.0 on the original IBM PC (did mainframes
for 15 years before that). I used the Unix example because that's what
the original question was about.

My example include works on ALL Apache servers.  I use it regularly on
both my home test system (Windows) and my hosted accounts (on Linux).  I
upload the files unchanged and they work fine.

Additionally - I generally have test machines as well as production
machines. Each machine, obviously, has its own root directory. I test
the files on a test machine.  When I get them working, I just copy them
unchanged over to the production machine.  On either Windows or Linux.

Also - yes, I know what happens when you include something via http.
But the original questioner had already figured that out, so I didn't
pursue the matter so as not to confuse him more.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: newbie: setting the page title

Right, I was replying to the original poster, I didn't mean to imply
anything about your experience.

Re: newbie: setting the page title

Thanks, Jerry,

  that's great stuff.  Yes, very useful if I ever change host, but also
very useful at the moment, so that I can test the site off line.


Site Timeline