PHP include dilemma

Discussion in 'PHP' started by Jdog, May 31, 2006.

  1. #1
    I have a site that uses php includes for the <head> information for all of the pages. The problem with this is that every page is going to get the same title. Is there a way around this? I'm thinking of something like having the title pull the information out of the content somehow. Is this possible? I found a way to do it in blogger, but I know that is a lot different.
     
    Jdog, May 31, 2006 IP
  2. woodside

    woodside Peon

    Messages:
    182
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you pulling information out of a database or is your content just static in php pages? If you are pulling it out of a database, you can do the same for the title, meta, tags etc.

    If the pages are static then you can use the php variable _SERVER["SCRIPT_NAME"] and do a switch on that to create different title tags.

    So if you script name was "welcome.php" you could set the title to be a welcome message, and if you script name was sitemap.php you could create the title to be "site map" or whatever.
     
    woodside, May 31, 2006 IP
  3. mikelbeck

    mikelbeck Well-Known Member

    Messages:
    790
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    108
    #3
    You can buffer the output and change the page title in your script:

    http://us3.php.net/manual/en/function.ob-start.php#53173

    This function dynamically changes title of HTML page:

    function change_title($new_title) {
    $output = ob_get_contents();
    ob_end_clean();

    $output = preg_replace("/<title>(.*?)<\/title>/", "<title>$new_title</title>", $output);
    echo $output;
    }

    Example:
    ob_start();
    // ... some output
    change_title('NEW TITLE!');
     
    mikelbeck, May 31, 2006 IP
  4. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #4
    I tend to just have variables and common code before the include statement. Then just echo the variable out in the included file e.g.
    
    <head>
        <title><?php echo $PageTitle; ?></head>
    
    PHP:
     
    dct, May 31, 2006 IP
  5. Jdog

    Jdog Peon

    Messages:
    267
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5

    So, if I put that in my included file and put something like

    $PageTitle = 'This page is the sitemap';
    PHP:
    before the include, it should work right?

    I'm looking in to the other ways too, but they are over my head right now. I'm very new to php. Thank you for the help.
     
    Jdog, May 31, 2006 IP
  6. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thats exactly how I do it for titles and meta tags as well.
     
    mad4, May 31, 2006 IP
  7. Jdog

    Jdog Peon

    Messages:
    267
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Perfect. I think I can handle that.

    Thanks all.
     
    Jdog, May 31, 2006 IP
  8. Jdog

    Jdog Peon

    Messages:
    267
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It worked perfectly. This is what I did on my title.php file that is incuded on all my pages:
    <?php
    if ( $pagetitle  ) {
        echo $pagetitle;
    } else {
        echo "Generic default title here";
    } 
    ?>
    
    PHP:
    This way I will still have the generic title on all my pages until I can get the
    $pagetitle="new title here";
    PHP:
    on all my pages.

    :) Thank you. This was my first php other than include.
     
    Jdog, May 31, 2006 IP