Adding an active link color to nav include file

Hi all,

Iv created a header include file storing main nav etc, I want to add css to the active page of the site but not sure what php I have to add to the page to achieve this?

Nav html:

<ul id="main_nav">
	<li><a href="index.php">Home</a></li>
	<li><a href="gallery.php">Gallery</a></li>
	<li><a href="reviews.php">Testimonials</a></li>
	<li><a href="about.php">About</a></li>
	<li><a href="prices.php">Prices</a></li>
	<li><a href="contact.php">Contact</a></li>

The current text color is #fff, when the user is on index.php i want that active link to be color #f00, how can I achieve this?

Thanks in advance


How about this?

<ul id=“main_nav”>
if(isset($_SERVER[“PHP_SELF”])) {
if ($_SERVER[“PHP_SELF”]==“index.php”) $status=“class=‘active’”; else $status=“”;
echo ‘<li><a “.$status.” href=“index.php”>Home</a></li>’;
if ($_SERVER[“PHP_SELF”]==“gallery.php”) $status=“class=‘active’”; else $status=“”;
echo ‘<li><a “.$status.” href=“gallery.php”>Gallery</a></li>’;
if ($_SERVER[“PHP_SELF”]==“reviews.php”) $status=“class=‘active’”; else $status=“”;
echo ‘<li><a “.$status.” href=“reviews.php”>Testimonials</a></li>’;
if ($_SERVER[“PHP_SELF”]==“about.php”) $status=“class=‘active’”; else $status=“”;
echo ‘<li><a “.$status.” href=“about.php”>About</a></li>’;
if ($_SERVER[“PHP_SELF”]==“prices.php”) $status=“class=‘active’”; else $status=“”;
echo ‘<li><a “.$status.” href=“prices.php”>Prices</a></li>’;
if ($_SERVER[“PHP_SELF”]==“contact.php”) $status=“class=‘active’”; else $status=“”;
echo ‘<li><a “.$status.” href=“contact.php”>Contact</a></li>’;

thanks smftre, got it sorted!

You’d do better to refer to the current page as current rather than active since an active link is one that is currently being run, not aone that is pointing at the current page.

Calling it active means that there is a potential for confusion in the future when someone is unsure whether the reference is to the current page or an active link.

In the code you could refer to it as sitepointforums if you really wanted to.

felgall, you make a valid point in terms of “anchor states”, an active state can be applied to an anchor and should not be mixed up with current page values etc.