Forum Moderators: coopster

Message Too Old, No Replies

Dynamic titles for dynamic pages (PHP)

dynamic title titles meta keywords php html

         

Nealreal

12:15 am on Mar 2, 2009 (gmt 0)

10+ Year Member



So I just got into using PHP includes to set up sites. The problem is: the TITLE tag stays the same within the template I'm using. How can I get it to change (the TITLE KEYWORDS DESCRIPTIONS) with each page that is loaded within the template. I've been all over net and the solutions don't work. MY FILES ARE NOT SERVED FROM A DATABASE.
Here is my code set-up:

------------------- code ------------------------

<html>
<head>
<title>None</title>
</head>

<?php @ require_once ("structure/bodynav.php"); ?>

<? $path = $_SERVER['PHP_SELF'];
$page = basename($path);
$page = basename($path, '.php');

$query = $_SERVER['QUERY_STRING'];
$query = explode('=', $query);
$query = $query; ?>

<ul id="menu">
<li><a href="about.php?page=home" <? if($query == home) print ' class="current"'; ?>>Welcome</a></li>
</ul>

<div id="content">
<?php if (isset($_GET['page'])) {include 'content/'.$_GET['page'].'.htm';} else {include 'content/services.htm';} ?>
</div>

<?php @ require_once ("structure/footer.htm"); ?>

------------------- end of code ------------------------

Any help is much appreciated :)

[edited by: tedster at 12:21 am (utc) on Mar. 2, 2009]

[edited by: Nealreal at 12:27 am (utc) on Mar. 2, 2009]

g1smd

12:20 am on Mar 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Get into the habit of examining POST variables, getting stuff from databases (where applicable), and making decisions, BEFORE you send the DOCTYPE out to the browser. This allows you to do a lot more useful things. Once the DOCTYPE is sent, you can't send additional HTTP Headers out to the browser for example - and that can cause a lot of issues.

In your case you would need to do something like this:

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

You would need to store the titles somewhere and have some sort of logic that selected the correct one. This would happen above/before the DOCTYPE is sent. You then simply echo it into the title tag later.

Nealreal

3:03 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



I don't understand POST variables at all - I'm new to PHP. I however do have a DOC Type and I didn't include it because I was trying to minimize the code for the sake of this post. I understand this:

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

But what do I put on the actual pages that will be included into the template?

Other post have given 2 junks of code:
1) for the template
2) for the actual pages that's being included

Your post doesn't help me.

[edited by: Nealreal at 3:14 pm (utc) on Mar. 2, 2009]

londrum

3:19 pm on Mar 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



your 'bodynav' template page could look something like this...

<html>

<head>

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

</head>

<body>

and your document page could look something like this...

<?php $document_title_tag = 'Blah Blah';

require_once ("structure/bodynav.php"); ?>

<h1>Page title</h1>

<p>Blah blah blah</p>

<?php require_once ("structure/footer.php"); ?>

Nealreal

3:42 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



Londrum, this is shooting back this error in the TITLE bar:

-----------------------------------------------------------
Notice:Undefined variable: document_title_tag in C:\wamp\www\about.php on line 6
-----------------------------------------------------------

This is a same error I got before from another post solution online.

londrum

3:53 pm on Mar 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



which page are you loading first. are you loading the bodynav template page first, or the page with the actual text on.

you need to load the page which sets the variable first, before you call the page that uses it.

why don't you paste the code of both pages down here, so we can have a look

Nealreal

4:07 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



The template page is "about.php" - the code you see in my initial post is from this page. It loads: "structure/bodynav.php" and "structure/footer.htm" - these two files are just junks of code that make up the template.

<div id="content">
<?php if (isset($_GET['page'])) {include 'content/'.$_GET['page'].'.htm';} else {include 'content/services.htm';} ?>
</div>

<div id="content"></div> is where the pages are loaded into.

"content/services.htm" is the content file that I need to have a TITLE KEYWORDS and DESCRIPTION for and it should replace the TITLE KEYWORDS and DESCRIPTION in the 'about.php" template's page.

londrum

4:27 pm on Mar 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



not sure i've understood, but if you are loading the code in your original post first, then you have things in the wrong order.

the first thing that your code does is load the "structure/bodynav.php" page -- which asks for the value of $document_title_tag.

But you do not set the value of $document_title_tag until further down your original code (when you load the "content/services.htm" page).
that is why you are getting your error -- you are trying to write it out before you even know what it is.

you need to set the value of $document_title_tag before you load "structure/bodynav.php".

the easiest way to do it is to set all the values at the top of the "about" page, and then load the templates further down. not sure why you really need that other page called "content/services.htm", because you can set the values on the "about" page.
so your "about" page would look something like this...

<?php

$document_title_tag = 'Blah Blah';

$document_meta_description_tag = 'Blah Blah'

$document_meta_keywords_tag = 'Blah Blah'

[/code]
[code]include ("structure/bodynav.php");

?>

[/code]
[code]<h1>Blah Blah</h1>

[/code]
[code]<?php

include ("structure/footer.php");

?>

all that basename stuff you've got on your original post is a bit more complicated than it needs to be. if you are trying to set up some kind of navigation that changes depending on which page it's on, then all you've got to do is include some kind of "template/navigation.php" page with this...

if(basename($_SERVER['SCRIPT_NAME']) == 'blah.php') {

Do one thing

} elseif(basename($_SERVER['SCRIPT_NAME']) == 'blahblah.php') {

Do another thing

} else {

Do default thing

}

g1smd

4:36 pm on Mar 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** you have things in the wrong order ***

That's what I was saying, above.

You'll need to re-organise the way you do things. The best way is to have all your PHP code that fetches and assembles the content placed before the point that you send the DOCTYPE out to the browser.

After the DOCTYPE you then simply ECHO out the pre-assembled headers, navbar, content, to the browser. That is, there is no fetching or logic decisions after the DOCTYPE, just ECHOing of the content that you have already assembled before you sent the DOCTYPE out.

Nealreal

4:47 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



Guys, I don't really understand PHP, I'm still learning. I'll make try to apply what you've posted and see what happens...

g1smd

4:56 pm on Mar 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just to explain one reason why it is a good idea to do it the way I showed, above...

Say someone asks for a page using a URL that doesn't exist for any valid content on your site. Assume that the way your script is programmed, that you have sent the DOCTYPE out before you have even checked that there is actually real content to be served for this URL.

It is now too late to send a HEADER 404 NOT FOUND response back for that request. That is, every URL requested from your your site, even random gibberish URLs, will serve the basic template layout, with a 200 OK response, and no content on the page. That's a major problem and a Duplicate Content issue.

How can this happen? For example, should you ever 'delete' a page, search engines would continue to index a blank template at that URL instead of receiving the proper 404 response that tells them the page has gone. The 200 OK response tells them that this *is* a real page, even when it isn't.

So, later on in your PHP experiments, you'll one day see the need to sometimes use the HEADER command to send a 301 or 404 response out instead of sending readable content - and the way that HTTP works, you MUST send that HEADER out before you send any DOCTYPE or on-page content. That can only be done if all the logic that assembles the page happens before the point you start sending anything out to the browser.

So, the order of stuff in your script should be:
- initialise variables
- fetch data and content that will later populate the page
- decide whether to send an error message (like 404), or a redirect to another URL, or real content.
- Send HTTP headers if anything other than "200 OK" is required.
- Send DOCTYPE for start of document.
- Send HTML header (title, meta description, etc).
- Send page content (header, navbar, content, etc).

Nealreal

12:00 am on Mar 3, 2009 (gmt 0)

10+ Year Member



My template document "about.php" loads different stuff into the page. My links are on the about.php already. Currently by default the page "services.htm" displays. But if I want to load "company.htm" the current code loads that chunk of content into the "about.php" page resulting in a URL that looks like this:

 website.com/about.php?page=company 

and if I want to load another page into the about.php the URL would change and so would the content - example:

website.com/about.php?page=services 

The problem is the TITLE KEYWORDS and DESCRIPTION stay the same because its coded on the about.php page. How can I get the TITLE etc in the about.php template to change when a new link is loaded. Is there a way to add some title codes to the htm file itself and it would replace the TITLE in the about.php page? I have it set this way because ABOUT is a section with sub pages like "company" or "services".

g1smd

2:07 am on Mar 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The simple answer is this. The way you have things set up at the moment, you cannot do what you want to do.

You will have to reorganise the whole thing to incorporate this.

What does your site show if I ask for

website.com/about.php?page=123abc567qwerrty
?
Use Live HTTP Headers to find out what HTTP Response Code is returned by your site.

Nealreal

4:33 pm on Mar 3, 2009 (gmt 0)

10+ Year Member



Ok. I guess you guys are the experts. I believe I have an understanding of the other order you mentioned and that my current way is the wrong setup.

I will probably have to post another question in the forum for the other features that i will loose by changing my organization.

Thanks for your help. I'll be back.

Nealreal

6:41 pm on Mar 8, 2009 (gmt 0)

10+ Year Member



Thanks Guys, your solutions worked.

g1smd

2:17 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That would have been many days work to fix, but it puts your code in a much better position to be modified in the future.