Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts

    Changing Title Of HTML Page Through PHP

    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>

    Thanks A Lot In Advance!

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    PHP Code:
    <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

  • #3
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the method I use (for similar things, not just title):

    PHP Code:
    <?
      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)

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by dumpfi
    PHP Code:
    <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...
    Thanks All

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts
    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!

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts
    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...

    WOuld Really Appreciate It!

  • #7
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts
    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?

  • #9
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    <?
      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:

    PHP Code:
    <?
      buffer
      
    {
        
    // Anything here
        
        
    buffer
        
    {
          
    // More stuff here
        
    }
        
        
    // Anything else here
      
    }
    ?>

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Hmm thats cool...but okay then how do u stop the page contents, or basically if u think of it like this
    PHP Code:
    <?
      buffer
      
    {
        
    // Anything here
        
        
    buffer
        
    {
          
    // More stuff here
        
    }
        
        
    // Anything else here
      
    }
    ?>
    Then what defines the } part? cleaning the buffer?

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

    **EDIT EDIT
    Nvm Thanks! I get it !
    Last edited by As5a5sIn5; 09-26-2004 at 09:16 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •