Highlight Current Page Using PHP Includes

If you’re already using css to highlight the ‘current page’ in your navigation but then decide that you want to include your navigation using a PHP Include, a problem becomes apparent:

How do I apply a page specific class to an individual navigation link?

Fortunately, PHP has a useful predefined variable called $_SERVER['PHP_SELF'] which returns the path of our current page. Using this in conjunction with the strpos function, we can apply a dynamic class to the current link.

PHP

1
2
3
<?php 
if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';
?>

The function strpos() searches the path of our current page for the specified string, in our example, ‘about.php’. If it finds ‘about.php’, it writes ‘class=”current”‘ to the page. If it doesn’t find ‘about.php’, it returns FALSE and nothing gets written to the page.

Now, all we need to do is insert that code in to each navigation link inside our included file.

Included File

1
2
3
4
5
6
<ul id="navigation">
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="index.php">Home Page</a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?> href="about.php">About Me</a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?> href="contact.php">Contact Me</a></li>
    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'portfolio.php')) echo 'class="current"';?> href="portfolio.php">My Work</a></li>
</ul>

The result is the current page link being assigned a class of current which can then be styled with your css:

1
.current {}

Feed IconFollow me on Twitter





15 Responses to “Highlight Current Page Using PHP Includes”

  1. Thanks for the great explanation!

  2. Thanks a lot! It did exactly what I wanted. :)

  3. Thanks so much for the tutorial! :D

  4. Thanks a million for the tutorial, adding this to my site redesign. :)

  5. You’re very welcome Nathan,
    Glad it’s of some use :)

  6. thanks for the wonderful tutorial.

  7. how would i do this if each page resides

    /home/index.php
    /about/index.php
    /contact/index.php

  8. Hi dreadycarpenter,
    In that case you could use the __FILE__ constant or $_SERVER['SCRIPT_FILENAME']

    <?php
    if (strpos(__FILE__, '/home/index.php')) echo 'class="current"'
    ?>
  9. I am using switch to fetch html code for each specific page as follows:

    <?php
    switch($ptr){
    case 'home':include "public/home.html"; break;
    case 'about':include "public/about.html";break;

    The first part of code fetches the html and the second part of code dumps it between the div maincontent.
    Is there a way to get the highlighting code to work on this layout.

  10. Never mind you can use $_SERVER['REQUEST_URI'] to get the full path of the current page and use strpos to search for page specific string. In my case I just compared it to my case strings i.e. home, about et cetera et cetera.

    Thanks for the great tutorial.

  11. Glad to see you got it sorted snetts :)

  12. Great post, thanks! One problem I’m having is that ‘class=”current”‘ is displaying at the top of my pages.

  13. thanks again great site..

  14. Guys, if I need to highlight two or more links, like: about.php, about_1.php, about_2.php, like sublink using the same page, what can I do?

  15. Hi Daniel,
    As this method is just using string comparisons, you would have to check for a common string segment in all links.
    In the example you gave, this would mean checking for ‘about’, not an ideal solution. You might be better looking for a breadcrumb class that does this sort of thing automatically.

Leave a Comment







XHTML: You can use the following tags in your comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">