View Full Version : Page Titles
fastsol1
07-28-2010, 02:32 PM
So when using a dynamic page link like below
<a href='index.php?page=blah'>
How would you get a SEO friendly page title dynamically for the incoming page without setting a specific array on the index.php page checking the incoming page name from the GET variable.
The problem I find is that almost always the included page is outputted well after the <head> of the page so if you had set the page title on the incoming page it still won't work cause the title has already been rendered in the browser before the included page is rendered.
fastsol1
07-28-2010, 02:33 PM
Oh also without doing this with a database.
Beverleyh
07-28-2010, 02:53 PM
I dont understand why you cant use GET.
See:
http://www.jemcon.org/process_scripts/test/title.php?page=Kit Kat
or
http://www.jemcon.org/process_scripts/test/title.php?page=Milky Bar
or
http://www.jemcon.org/process_scripts/test/title.php?page=Snickers is my fave chocolate bar of all time
Put anything for the $page variable in the URL and it can print to the title tag.
Here's the code for my test page:
<?php
$title = $_GET['page'];
?>
<!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>
<title><?php echo $title ;?></title>
</head>
<body>
<h1><?php echo $title ;?></h1>
</body>
</html>
Is that not something you can expand on?
fastsol1
07-28-2010, 02:59 PM
Ok, where are you defining the text that is being displayed in the page title. I assume it's defined on the incoming page, so are you making a variable on that page or are you saving the file name as the page title you want displayed? Show me more please :)
Beverleyh
07-28-2010, 04:00 PM
Its just the text in the URL after the '=' sign. Same principle as your example where you put '=blah' - it will be passed to the URL then GET takes it, assigns it to the variable $page, and its printed wherever you want in your web page, be it in the body or title tag.
Beverleyh
07-28-2010, 04:03 PM
Sorry, I keep saying the $page variable - I mean the $title variable
fastsol1
07-28-2010, 06:40 PM
What you could do is just create two variables on those included pages, one for the title and one for the content. Then include them on the top of your index page and use the variables where you like in the output.
This was an answer on another forum I posted on and it's pretty much the kind of answer I think I was looking for. I hadn't thought about making the entire content on the included page a variable and then calling it when and where I needed on the main page. The only thing I can think that might be an issue is if the page is really full of content, the variable would be pretty large too on the main page until it was used, so would that slow down page load or anything else bad?
Beverleyh
07-28-2010, 07:09 PM
I hadn't thought about making the entire content on the included page a variable and then calling it when and where I needed on the main page. The only thing I can think that might be an issue is if the page is really full of content, the variable would be pretty large
Thats not what the example I gave does - you dont appear to be understanding what I'm saying.
Just look at the URL (the address bar) of the page once its called. You can pass anything after the = sign in the URL only - nothing to do with content on the page.
Try it by putting your cursor at the end of any of the example URL's I gave then delete everything up to the = sign and type anything else >> refresh the page and that's what GET uses for the title.
Here - to make it easier, I've added a load of Lorem Ipsum to show that content has no bearing on the passed variable. I've also created some links to illustrate what you wanted in your original post. Notice how its always the same title.php page that's being used - the fact that its called title.php is irrelevant - and see that its your index.php?page=blah example. Revised page: http://www.jemcon.org/process_scripts/test/title.php
There's no title initially because nothing is passed to GET from the URL. As soon as you click on the links, text after the "=" is passed to "$title" and it can be echo'd on the page.
The code for the links is just:
<ul>
<li><a href="title.php?page=About Us">About Us</a></li>
<li><a href="title.php?page=Contact Us">Contact Us</a></li>
<li><a href="title.php?page=Our Services">Our Services</a></li>
<li><a href="title.php?page=More Information">More Information</a></li>
<li><a href="title.php?page=Terms and Conditions">Terms and Conditions</a></li>
</ul>
fastsol1
07-28-2010, 07:34 PM
Ah ok, now I understand your concept. But I typically use this code to verify the page that is being called
if ($page){
if (!strpos($page,".")&&!strpos($page,"/")){
$path = "$page.php";
if (file_exists($path)){
include ("$page.php");
}
else{
echo "That page does not exist<br/><br/><a href='#' onclick='history.go(-1)'>Go Back To Previous Page</a>";
}
}
}
But I suppose I could (in you type of example) just add a &title=blahblah and GET that for the title, which I suppose works good to since the link has to be defined somehow so adding that would be easy enough. Cool
Beverleyh
07-28-2010, 09:15 PM
Your welcome.
djr33
07-29-2010, 03:09 AM
The way that your system is setup (which is a common method), you are outputting your included page after the beginning of the <body> section, so you can no longer output a page title. This is the same problem that happens with Javascript, CSS and other items that should go in the head.
There are two ways to solve this:
1. Approach this as a two-step process: first set the page title using the information from get, within your index page, then include the content (body) of the page later.
2. The only way to "really" do this, by using a single page, is to create a basic templating system where you output different parts of the page at one time.
Here's a very basic idea for version (2):
In your included pages, set the variables $title and $body to be your page's title and body text, respectively.
Then echo those variables within your template on the index page.
There are of course much more common methods for all of this and those might be more helpful here (but also harder to create).
If you can avoid dealing with templates and use a method like explained by Beverley, that's probably the best way to do this.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.