PHP Include- Page Titles

marvso

Member
Messages
61
Reaction score
0
Points
6
On my site, I have a header.php,a footer.php, and a content.php which grabs the header/footer and selects page content from a directory file (?page=pagename). The problem is that when content.php includes the directory content files, it doesn't include all of the <header> information.

To fix this, I added this to one of the directory content files (placed above all other content):
Code:
<?php
$title="PAGE TITLE";
?>

And this to my header.php page:
Code:
<header>
<title><?php echo $title;?></title>  

</header>

I was hoping this would print the designated title of the page, but instead I just still get no page title. Does anyone know what I did wrong/if this is possible at all? :confused:

I'm pretty new to php, so any help is appreciated! :biggrin:
 

techairlines

x10 Flyer
Community Support
Messages
2,867
Reaction score
165
Points
63
Try this:

Code:
<header>
<title><?php echo "$title"; ?></title>  

</header>

Single/double quotes are important in PHP. See: http://php.net/manual/en/function.echo.php

I can't check if this is possible without knowing the content script. The code you posted would cause the titles of all pages to display as PAGE TITLE as it is basically echoing that.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
HTML:
<header>
The tag for the header element is <head>, not <header>.

You neglected to include a link to a live sample page so we can see the current output, in case there is another issue causing problems with the page title.

PHP:
<title><?php echo "$title"; ?></title>
Quotes are unnecessary around variables; all "$title" will do is interpolate the value of $title into a string with no other content. It's basically equivalent to $title by itself (except that it converts the content of $title to a string, which echo will do anyway.)
 
Last edited:

Hello71

New Member
Messages
14
Reaction score
0
Points
0
Try this:

Code:
<header>
<title><?php echo "$title"; ?></title>  

</header>

Single/double quotes are important in PHP. See: http://php.net/manual/en/function.echo.php

I can't check if this is possible without knowing the content script. The code you posted would cause the titles of all pages to display as PAGE TITLE as it is basically echoing that.

PHP:
<?php echo "$title"; ?>
is much slower than
PHP:
<?php echo $title; ?>
 

marvso

Member
Messages
61
Reaction score
0
Points
6
The tag for the header element is <head>, not <header>.
Opps, sorry. Late night typing does not work for me. :redface:

I'm attempting a different approach, this time with switch statements:
<?php
$title="$title";

switch( $title)
{

case($page = 'home.htm')
$title = 'Title1';
break;

case($page = 'page1.htm')
$title = 'Title2';
break;

}

?>
However, I receive an error (expected T_VARIABLE, expecting T_CASE or T_DEFAULT or '}' ) on line 2, does anyone know the problem?

(I'm so sorry if I'm explaining things... oddly. I will put together a live example to show soon.)
 

brutetal

New Member
Messages
23
Reaction score
1
Points
0
PHP:
<?php
$title="$title";

switch( $title)
{

case($page = 'home.htm')
$title = 'Title1';
break;

case($page = 'page1.htm')
$title = 'Title2';
break;

}

?>

You can't do that, its invalid syntax.

This is correct usage:
PHP:
switch($page){
case "home.html":
$title = "Home";
break;
}

If you want to do your suggestion, then use an if structure.
PHP:
<?php
 
 if($page == 'home.htm'){
 $title = 'Title1';
 }else if($page == 'page1.htm'){
 $title = 'Title2';
 }
 
 ?>
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
PHP:
<?php echo "$title"; ?>
is much slower than
PHP:
<?php echo $title; ?>
Not really. Certainly not noticeably slower. Try looping over "$title" and $title millions of times. The difference will be a few milliseconds at most.

The double quotes are, however, unnecessary and (slightly) less readable.

PHP:
case "home.html":
$title = "Home";
...
if($page == 'home.htm'){
 $title = 'Title1';
Make sure you indent to make code readable.
PHP:
switch($page) {
case 'home.html':
    $title = 'Home';
    break;
case 'page1.html':
....
}
...
if ($page == 'home.html') {
    $title = 'Title 1';
} elseif ($page == 'page1.html') {
    $title = 'Title 2';
} else {
    ...
}

If you want to do your suggestion, then use an if structure.
A switch or an associative array is preferable to a sequence of ifs. It's more readable and potentially more performant. Numerous ifs is a minor smell.

PHP:
$pages = array(
    'home' => array('title' => 'Home', 'file' => 'index.html'),
    'about' => array('title' => 'About Us'),
    'contact' => array('title' => 'Contact Us'),
   ...
    'invalid' => array('file' => '404.html')
);
function getFile($pages, $page) {
    if (isset($pages[$page])) {
        return $pages[$page];
    } else {
        return $page . '.html';
    }
}

// prevent injection attacks
if (isset($pages[ $_REQUEST['page'] ])) {
    $page = getFile($pages, $_REQUEST['page']);
    $title = $pages[$page]['title'];
     ...
} else {
    // invalid page requested
    header('HTTP/1.0 404 Not Found');
    $page = $pages['invalid']['file'];
}
include('header.php');
include($page);
include('footer.php');
 
Last edited:

marvso

Member
Messages
61
Reaction score
0
Points
6
Thanks everyone for the help. I corrected the switch statement, but it still doesn't seem to work. You can see a live example here.

This is the source code for content.php:
Code:
<?php
//Snag the header:
  include('head.php');

// Get the name of the page from the string query 
// Set $page to "home" if a parameter is not passed
  if ($_GET['page']) {
    $page = $_GET['page'] . '.htm';
  } else {
    $page = 'index.htm';
  }

// If file specified exists, include it in.
  if (file_exists("pages/$page")) {
    include("pages/$page");


//If page 1 has page 2, next/previous:
  if (preg_match("/^(.*_page)(\d+)/", $page, $matches)) {
    $prev_page = $matches[2] - 1;
    $next_page = $matches[2] + 1;
    echo("<p style=\"text-align: center\">");

    if (file_exists("pages/" . $matches[1] . $prev_page . ".htm")) {
      echo("<a href=\"{$_SERVER['PHP_SELF']}" .
           "?page={$matches[1]}$prev_page\">Previous Page</a>");
      $prev = 1;
    }
    if (file_exists("pages/" . $matches[1] . $next_page . ".htm")) {
      if ($prev) {
        echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
      }
      echo("<a href=\"{$_SERVER['PHP_SELF']}" .
           "?page={$matches[1]}$next_page\">Next Page</a>");
    }
    echo("</p>");
  } 


// If page doesn't exist, display this error message.
  } else {
    echo("<h1 align=\"center\">Page cannot be found</h1>\n");
  }

//Snag the footer
  include('foot.php');
?>

And here is head.php:
Code:
<?php

//Declare what the title is based on the page
$title="$title";

switch($page){
case "index.htm":
$title = "Home";
break;

case "page1.htm":
$title = "Page 1";
break;

}

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html>
 <head>
        <title><?php echo $title; ?></title>
    </head>
    <body>
<a href="?page=index">Click here to go home</a> <a href="?page=page1">Click here to go to page 1</a> </br>

Foot.php simply closes off the </body> and </html>.

What seems to happen (at least from the browser source point of view) is that <title> won't "echo" the title variable. If I try to view the source code from my internet browser, <title></title> is blank:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html>
 <head>
        <title></title>
    </head>
    <body>
<a href="[URL="http://x10hosting.com/forums/view-source:http://mariovsonic.pcriot.com/content.php?page=index"]?page=index[/URL]">Click here to go home</a> <a href="[URL="http://x10hosting.com/forums/view-source:http://mariovsonic.pcriot.com/content.php?page=page1"]?page=page1[/URL]">Click here to go to page 1</a> </br><p>This is the index.</p></body>
</html>

Thanks once again for any more assistance! :D
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You're including head.php before you define $page, so the latter doesn't exist when the former references it.

head.php should only output the page header; it shouldn't set any page configuration variables. The two are different functions, and, due to separation of concerns, should be handled by separate modules.

Try the
PHP:
 tag when posting PHP code. It colorizes the code, making it more readable.

There are a few other problems with the latest code (such as an injection attack, though not a terribly harmful one), but I don't have time to go into it right now.
 
Last edited:

marvso

Member
Messages
61
Reaction score
0
Points
6
Thank you so much, misson. I moved the include() head.php statement on content.php and it works like a PHP-induced dream!
PHP:
<?php

//DECLARE WHAT PAGE IS
...

//Snag the header:
  include('head.php');

// If file specified exists, include it in.
  if (file_exists("pages/$page")) {
    include("pages/$page");
(I'll try to fix the injection attack problem... I think I see what you mean.)

Thanks once more to everyone who helped. I've learned a lot from your assistance. :D
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
(I'll try to fix the injection attack problem... I think I see what you mean.)
An earlier post shows one way of doing it, using an array to store a whitelist. Another is to use basename() to remove all path components excepting the page.
 
Top