...

View Full Version : Changing Title Of HTML Page Through PHP



As5a5sIn5
09-25-2004, 10:01 PM
Okay I'm making my homepage http://jacostuff.com/ and I'm using a system that always shows main.php and in the middle includes a page ...My system mainly uses $_GET to decide what page I'm on...I've put in all my secruity measures etc. but now my problem is, I can't change the HTML title with each included page. Is there anyway to do this ? I have already tryed echo $variable;
and changing the variable thorugh the included page, but the variable can't be echoed unless defined before the <head> :confused:

Thanks A Lot In Advance!

dumpfi
09-25-2004, 10:30 PM
<html>
<head>
<title>
<?php
switch($_GET['p']) {
case 'login': echo 'Login'; break;
case 'register': echo 'Register'; break;
... etc.
default: echo 'Home';
}
?>
</title>
...remaining part of the page

dumpfi

AaronW
09-25-2004, 10:36 PM
Here's the method I use (for similar things, not just title):


<?
ob_start (); // Buffer output
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title><!--TITLE--></title>
</head>

<body>
<?
$pageTitle = 'Title of Page'; // Call this in your pages' files to define the page title
?>
</body>
</html>
<?
$pageContents = ob_get_contents (); // Get all the page's HTML into a string
ob_end_clean (); // Wipe the buffer

// Replace <!--TITLE--> with $pageTitle variable contents, and print the HTML
echo str_replace ('<!--TITLE-->', $pageTitle, $pageContents);
?>

Hopefully my comments are clear enough. Look up ob_start () in the php manual ( http://php.net/ob_start ) if you want to know exactly how that works (and you should) :)

As5a5sIn5
09-26-2004, 07:35 AM
<html>
<head>
<title>
<?php
switch($_GET['p']) {
case 'login': echo 'Login'; break;
case 'register': echo 'Register'; break;
... etc.
default: echo 'Home';
}
?>
</title>
...remaining part of the page

dumpfi

heh...y didn't i think of that....sorry aaron, I'm sure your way is great but I don't get ob_stuff() lol...Ill remember to try and use that in the future... but for now..Im clueless... :o
Thanks All

As5a5sIn5
09-26-2004, 07:41 AM
hmmm I mite actually try both and see witch one is less time consuming....
of course I like to understand how things works before I just use them...I looked up that ob_stuff() and I dont understand...(twice I looked it up) so....thanks all again!

As5a5sIn5
09-26-2004, 07:53 AM
Hey Aaron sorry to be a pain but could maybe PM or post here a breif explaination of oupt buffer? I used it, it works like a charm, but dont quite understand it...I read ur script about 5 times, trying to comprehend, read the majority of the output buffer php.net page, still no getting... :confused:

WOuld Really Appreciate It! :D

AaronW
09-26-2004, 12:40 PM
PHP usually works be executing any bits of code and printing all output directly to the browser. If you say "echo 'Some text here.';", that string will get sent the browser and is emptied from memory.

What output buffering does is say "Print all output to a buffer. Hold onto it. Don't send ANYTHING to the browser until I tell you to."

So what this does is it buffers all your pages' HTML into the buffer, then at the very end, after the </html> tag, it uses ob_get_contents () to get the contents of the buffer (which is usually all your page's HTML source code which would have been sent the browser already) and puts that into a string.

ob_end_clean () empties the buffer and frees some memory. We don't need the source code anymore because we just stored it in $pageContents.

Then, lastly, I do a simple find & replace on your page's source code ($pageContents) for any instances of '<!--TITLE-->' and replace them to whatever the $pageTitle variable was set to. Of course, it will then replace <title><!--TITLE--></title> with <title>Your Page's Title</title>. After that, I echo the $pageContents, just like the browser would have.

It effectively holds onto output so you can manipulate it before sending it to the browser.

As5a5sIn5
09-26-2004, 05:55 PM
Hmm u should be a teacher thanks so very much!
But one last thing...if you don't echo the pagecontents will nuthin come out? :p

AaronW
09-26-2004, 06:14 PM
Right. Not echoing the contents will mean the browser sees nothing.

However, using ob_end_flush () in place of ob_end_clean () will just flush the buffer to the browser, though I don't know why that'd be useful. If you don't call ob_end_clean () AND you don't call ob_end_flush(), ob_end_flush () will be called by PHP automatically.

Another thing you may find useful is that you can nest the buffers. That is:


<?
ob_start ();
// Anything here

// Save a big block of HTML as a variable:
ob_start ();
?>
<table>
<tr>
<td>This is a table which will be stored in a variable.</td>
</tr>
</table>
<?
$table = ob_get_contents ();
ob_end_clean ();
// The HTML is now stored in $table

// More stuff here

$contents = ob_get_contents(); // Save the contents of the first ob_start() call to $contents
ob_end_clean ();
?>

Think of it as:


<?
buffer
{
// Anything here

buffer
{
// More stuff here
}

// Anything else here
}
?>

As5a5sIn5
09-26-2004, 09:14 PM
Hmm thats cool...but okay then how do u stop the page contents, or basically if u think of it like this

<?
buffer
{
// Anything here

buffer
{
// More stuff here
}

// Anything else here
}
?>
Then what defines the } part? cleaning the buffer? :D

**EDIT
Err I c, then ob_end_flush completely cleans its, like no nesting, and ob_end_clean allows nesting I guess? :confused:

**EDIT EDIT
Nvm Thanks! I get it !



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum