Jump to content

How do I change the page <title> dynamically with PHP


SauloA

Recommended Posts

I have a header, like so:

 

header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Website Page Title</title>
<link href="master.css" rel="stylesheet" type="text/css" />
</head>

<body>

 

a footer like so:

 

footer.php

</body>
</html>

 

and several pages use the header and footer like so:

 

index.php, page1.php, and page2.php

<?php include("header.php"); ?>
<p>Page Content</p>
<?php include("footer.php"); ?>

 

The only problem is that I can't change the page <title> for each page.

 

I tried using this code to change the page <title>:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php 
$title;  
switch($_SERVER['PHP_SELF']) 
{ 
case 'index.php': 
     $title = 'Welcome to the Home Page'; 
     break; 
case 'page1.php': 
     $title = 'This is the Page One'; 
     break;
case 'page2.php': 
     $title = 'This is the Second Page'; 
     break; 
} 
echo '<title>'.$title.'</title>'; 
?>
<link href="master.css" rel="stylesheet" type="text/css" />
</head>

<body>

 

I get no errors but the title always comes out blank.  Is there a better solution? or is this the code I should be using?

Link to comment
Share on other sites

You are returning more than the filename, to get just the filename you must breal it apart. The best way when the return may be ant number of folders deep is to use php's basename() function.

 

$path=$_SERVER['PHP_SELF'];
$page=basename($path);
switch("$page")

 

 

HTH

Teamatomic

Link to comment
Share on other sites

one way is to include it into a function like this:

function header($title)
{
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>.'$title'.</title>
<link href="master.css" rel="stylesheet" type="text/css" />
</head>

<body>';
}

 

then you can use the function anywhere you like:

header('This is page One');

 

header('This is page Two');

 

header('This is page Three');

 

and so on

Link to comment
Share on other sites

I ended up using the code from teamatomic.  I received no errors and the code works fine:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php 
$title;
$path=$_SERVER['PHP_SELF'];
$page=basename($path);
switch("$page") 
{ 
case 'index.php': 
     $title = 'Welcome to the Home Page'; 
     break; 
case 'page1.php': 
     $title = 'This is the Page One'; 
     break;
case 'page2.php': 
     $title = 'This is the Second Page'; 
     break; 
} 
echo '<title>'.$title.'</title>'; 
?>
<link href="master.css" rel="stylesheet" type="text/css" />
</head>

<body>
<link href="master.css" rel="stylesheet" type="text/css" />
</head>

<body>

 

Thanks everybody.  This is officially solved.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.