0

I would like to change the title of the HTML page based on the content, but im including only the content below the header part, so i have to change the title from this included php. To explain:

<html>

<header><title>I would like to change</title></header>
<!--CONTENT-->
        <?
        include "pages/some_page.php";
        ?>
</html>

How could i do that? Anyone can help in this?

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
dodo
  • 459
  • 2
  • 8
  • 21

4 Answers4

5

You cant do that without a nasty hack.

What you should do is perform all your logic BEFORE you output html. A simple example follows:

<?php
//index.php
//perform logic and set variables before any html

$page = isset($_GET['menu'])?$_GET['menu']:'home';

switch($page){
    case 'home':
        $title = ' welcome to myco.ltd';
        $content = 'pages/home.php';
        break;
    case 'about':
        $title = 'about us';
        $content = 'pages/about.php';
        break;
    case 'contact':
        $title = 'get in touch';
        $content = 'pages/contact.php';
        break;
}
//the following html could be in a separate file and included, eg layout.php
?>
<html>
<head>
    <title><?php echo $title;?></title>
</head>
<body>
<!--menu and other shared html here-->
<?php include $content;?>
<!-- shared footer stuff here-->
</body>
</html>

This is essentially a VERY barebones router script, an essential component of any framework. I would highly recommend you consider a lightweight framework rather than write everything from scratch. http://fatfreeframework.com/home would be a great start

Steve
  • 20,703
  • 5
  • 41
  • 67
  • It becames nasty if i have thousands of pages, but it's a small website so it's perfectly fits my expectations! Otherwise, i still can add a 'default:' case to make sure a default title is always shown. Thank you! – dodo Jul 16 '14 at 23:16
  • @JancsikZsolt glad I could help. Yes this would not suit a large site, it's just an illustration of the technique. Real routers are more complex. Please check out the framework I linked, I think it will really help you, even if you just learn from it rather than use it. – Steve Jul 16 '14 at 23:21
2

The function below will let you change document title, meta keywords and meta description. You may use it anywhere in your application.

Just be sure to turn on output buffering using ob_start() before the function is called. I prefer including it at the top of application, just after all global settings are loaded.

function change_meta_tags($title, $keywords, $description){

    $output = ob_get_contents();
    if (ob_get_length() > 0) { ob_end_clean(); }

    $patterns = array("/<title>(.*?)<\/title>/", "/<meta name=\"keywords\" content=\"(.*?)\" \/>/", "/<meta name=\"description\" content=\"(.*?)\" \/>/");
    $replacements = array("<title>$title</title>", "<meta name=\"keywords\" content=\"$keywords\" />", "<meta name=\"description\" content=\"$description\" />");

    $output = preg_replace($patterns, $replacements, $output);  
    echo $output;
}
hex494D49
  • 9,109
  • 3
  • 38
  • 47
  • 1
    Very interesting idea. Seems little bit "dirty" to me but that may relates to the problem itself. +1 – Daniel K. Jul 16 '14 at 22:05
  • "You may use it anywhere in your application" provided you have started output buffering, before outputting data, and called this function after outputting data. Not exactly anywhere – Steve Jul 16 '14 at 22:05
  • @Daniel It's just a legitimate way, there's nothing dirty in there :) – hex494D49 Jul 16 '14 at 22:07
  • @user574632 Yes, I mentioned it as well. I usually put `ob_start` at the beginning of application, before any output – hex494D49 Jul 16 '14 at 22:09
1

Use javascript in some_page.php .

<?php echo "<script>document.title = '".$dynamicTitleVariable."';</script>"; ?>
XaxD
  • 1,429
  • 14
  • 27
1

Pending what you are trying to base the content off of, this could easily be done via an MVC-style setup. In your controller, you would generate the title based off of content that could be grabbed and pass this through to the view as a variable. Then, in your view have the title be dynamically set:

<html>
    <head>
        <title>
            <?php echo $title; ?>
        </title>
    </head>
</html>

This should also work fine with SEO capability, as crawlers will be able to interpret this far better than they would JavaScript.

ihgann
  • 505
  • 3
  • 11