0

I'm using php to highlight my current page.

Everything works correctly however when I first go to the page there is nothing after "/" in the domain name.

Ex: mydomain.com/

How can I highlight that page. It only gets highlighted when I have /index.php

Ex: mydomain.com/index.php

Here's my html:

<nav>
    <ul class="cd-primary-nav">
      <li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
      <li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a></li>
      <li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a></li>
      <li><a href="brands.php" <?=echoSelectedClassIfRequestMatches("brands")?>>Brands</a></li>
      <li><a href="testimonials.php" <?=echoSelectedClassIfRequestMatches("testimonials")?>>Reviews</a></li>
      <li><a href="contact.php" <?=echoSelectedClassIfRequestMatches("contact" , "thankyou")?>>Contact</a></li>
    </ul>
  </nav>

Here's the PHP:

<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
    $current_file_name = basename($_SERVER['REQUEST_URI'], ".php");

    if ($current_file_name == $requestUri)
        echo 'class="selected"';
}
?>

Thanks in advance.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Daniel
  • 55
  • 2
  • 10

1 Answers1

2

You can actually use $_SERVER['SCRIPT_NAME'] to get the executed filename. Even if you browse to example.com, it will still contain index.php as the value.

Anonymous
  • 11,740
  • 3
  • 40
  • 50