Hello guys
I want to create something like SEO link i have website with three pages, but the header is the same
header.php

<!DOCTYPE html>
<html>
<head>
    <style>
        .active{
            background-color: #365214;
        }
        ul{
            display: inline;
        }
        li{
            display: inline;
        }
    </style>
    <title></title>

</head>
<body>
<ul>
    <li><a class="active" href="index.php">HOME</a></li>
    <li><a href="a.php">ABOUT</a></li>
    <li><a href="s.php">SERVICES</a></li>
    <li><a href="c.php">CONTACT</a></li>
    </ul>

footer.php

</body>
</html>

index.php

<?php
include("header.php");
include("home.php");
include("footer.php");
?>

what I need is when user click on any link on header it changed the title of page dynamically

<title></title>

and applying .active class to current page link.

Dani AI

Generated

A simple, robust pattern is to set the per-page metadata and state in each page before including the shared header. That keeps header.php generic, avoids duplicated logic, and makes both the <title> and the nav "active" class explicit and easy to test. 's single-entry idea (with rewritten URLs) is a good next step for larger sites; already solved the routing with .htaccess, which is exactly the kind of thing that pairs well with the pattern below.

Example (put this at the very top of each page file, before any output):

<?php
$pageTitle = 'About - MySite';
$activePage = 'about';
include 'header.php';
?>

In header.php output the sanitized title and use the $activePage value to mark the current link:

<title><?php echo htmlspecialchars(isset($pageTitle) ? $pageTitle : 'MySite', ENT_QUOTES, 'UTF-8'); ?></title>

<ul>
  <li><a href="index.php" <?php echo ($activePage==='home') ? 'class="active"' : ''; ?>>HOME</a></li>
  <li><a href="a.php" <?php echo ($activePage==='about') ? 'class="active"' : ''; ?>>ABOUT</a></li>
  <!-- add other links similarly -->
</ul>

Practical notes and SEO cautions: keep titles unique and readable (brand at the end), aim for ~50-60 characters for best display in search results, and include a unique H1 and meta description per page. Always escape the title with htmlspecialchars to avoid XSS. Provide a sensible fallback title in header.php in case a page forgets to set $pageTitle.

Troubleshooting: if the active class doesn't match, dump $_SERVER['REQUEST_URI'] to see the actual path your links produce (useful after adding mod_rewrite). For sites that grow beyond a few pages, switch to a small router or a single index.php with a dispatch map (as suggested) to centralize title/nav configuration.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

for simple one...

<?php
$pageArray = array(
        "home" => array("label"=>"home","url"=>"pages/index.php","title"=>"...","nav"=>'/home/'),
        "about" => array("label"=>"about","url"=>"pages/a.php","title"=>"...","nav"=>'/about/'),
        "services" => array("label"=>"services","url"=>"pages/s.php","title"=>"...","nav"=>'/services/'),
        "contact" => array("label"=>"contact","url"=>"pages/c.php","title"=>"...","nav"=>'/contact/'),
    );

$chosen= (isset($_GET['page']) && in_array(strtolower($_GET['page']), array_keys($pageArray))) ? $_GET['page'] : 'home';
$page = $pageArray[$chosen];

include("header.php");
include($page['url'] . ".php");
include("footer.php");
?>

Your header file could contain something like...

<?php
    $nav = '';
    foreach($pageArray as $key=>$item)
    {
        if(isset($item['nav']))
        {
            $cls = ($chosen == $key) ? ' class="active"' : ''; 
            $nav .= "\n\t<li><a$cls href="{$item['nav']}">".strtoupper($item['label'])."</a></li>";
        }
    }
?>

<!DOCTYPE html>
<html>
<head>
    <style>
        .active{
            background-color: #365214;
        }
        ul{
            display: inline;
        }
        li{
            display: inline;
        }
    </style>
    <title><?php echo $page['title'];?></title>
</head>
<body>
<ul>
    <?php echo $nav;?>
</ul>

You can now use a mod_rewrite having just a single index page - calling different include files depending on the rewritten url, e.g.

www.example.com/home/ which is: www.example.com/index.php?page=home
www.example.com/about/ which is: www.example.com/index.php?page=about
www.example.com/contact/ which is: www.example.com/index.php?page=contact
www.example.com/services/ which is: www.example.com/index.php?page=services

Just a matter of a simple .htaccess file.

I've Created .htaccess file for "title" I thought there is another solution :P
but this solve my .active class issue.
Thanks for you Sir, you are amazing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.