View Full Version : Nav bar - disabling current page link

07-16-2012, 01:17 AM
A lot of samples on this topic build a navigation bar element by element, checking each time if the link points to the current page. I think my approach is much simpler.

I create the navbar once as an HEREDOC string, and then do a simple string replace (str_replace) to inject an id (or class) for the current page. I can do the same for the main navigation-menu (for the current page). That is, the main category can appear differently formatted, to indicate it includes the current page.

I'm also reading session data to add a link for either 'Log In' or 'Log Out: user name'. This requires session_start() to have occurred earlier in the code.

// The current page's link will have the id 'current' added. This is
// made inactive with CSS. Similarly, the main navigation-group will
// be given id 'currentgroup' - if there is a group.
$navbar = <<< EOT
<ul id="nav">
<li class="level1"><a href="index.php">Home</a>
<div id="navslider"></div></li>
<li class="level1"><a href="html.php">HTML</a></li>
<li class="level1"><a href="css.php">CSS</a>
<li><a href="css10rules.php">10 Rules</a></li>
<li><a href="cssbox.php">Box Model</a></li>
<li><a href="cssposition.php">Positioning</a></li>
<li><a href="css_coding.php">Code Paper</a></li>
<li class="level1"><a href="javascript.php">JavaScript</a>
<li><a href="richtext.php">Rich Text</a></li>
<li><a href="map.php">Map</a></li>
<li><a href="another3.html">Another 3</a></li>
<li class="level1"><a href="login.php">Log In</a></li>
<li class="level1"><a href="testarea/test.php">Test Area</a></li>

if (isset($_SESSION['agent']) && $_SESSION['agent'] ==
$navbar = str_replace("login.php", "logout.php", $navbar);
if (isset($_SESSION['username'])) {
$navbar = str_replace("Log In", "Log Out: " . $_SESSION['username'], $navbar);
} else {
$navbar = str_replace("Log In", "Log Out", $navbar);

if (!isset($thispage) || empty($thispage)) {
// get the sub-domain(s) and page for the current page
$thispage = curSubsAndPage();
$navbar = str_replace("\"$thispage\"", "\"#\" id=\"current\"", $navbar);

if (isset($thisgroup) && !empty($thisgroup)) {
$navbar = str_replace("\"$thisgroup\"", "\"$thisgroup\" id=\"currentgroup\"",

echo "\n$navbar\n";

I can set the variable $thispage before including the navbar - for testing, or if there is some issue preventing the page-name being found. The variable $thisgroup needs to be set manually.

It also uses the following function to read the current sub-domains/page.

function curSubsAndPage() {
// Returns the /subdomains/page.extn, with the leading '/' removed.
$locally = strpos($_SERVER['PHP_SELF'], '/AndysProject2/');
if ($locally !== false) {
return substr($_SERVER['PHP_SELF'], 15);
// .. return everything after '/AndysProject2/';
if (substr($_SERVER['PHP_SELF'], 0, 1) == '/') {
return substr($_SERVER['PHP_SELF'], 1); // remove leading '/'
} else {
return $_SERVER['PHP_SELF'];

I've included my local project-folder name within this function; this could be set as a variable to make it more flexible.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum