Determine section of the website for navigation

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Determine section of the website for navigation

Post by matthijs »

It's often useful to show a user in which section of the website he is, by highlighting the current section in the navigation bar. For a website I build I wrote the snippet below. I use it to determine the (main) section we are in.

This website uses clean URLs, so they always look like /services/, /services/webdesign/, /contact/, etc. Each main section can have more subpages. Therefore I couldn't use the page_ID.

Code: Select all

<?php
	// only allow specific set of sections
	$allowed_sections = array('home','services','portfolio', 'weblog', 'about', 'contact');
	// the URLs look like /portfolio/ , /contact/, etc
	$request_uri = $_SERVER["REQUEST_URI"];
	
	$parts = explode(DIRECTORY_SEPARATOR, $request_uri);
	
	if(in_array($parts[1], $allowed_sections)) {
		$section = $parts[1]; 
	} else {
		$section = 'home';
	}

// Then, in the template for the header section of all pages:
echo '<body id="' . $page_ID . '" class="' . $section . '" >';
?>
Then in the CSS I do something like:

Code: Select all

#navbar {background:transparent url(images/navbar.gif) 0 0 no-repeat;}
.services #navbar {background-position: 0px -28px;}
//etc
But my main concern is the PHP. So any tips, critique, security issues, code beautifers?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

DIRECTORY_SEPARATOR is not a good idea for exploding URLs because on a windows system it is '\'. Everything else was restructuring:

Code: Select all

<?php
/**
 * Gets the active sub section in the REQUEST_URI, if the sub section is
 * unknown the first legal one is returned.
 *
 * @return string
 * @uses getLegalSubSection()
 */
function getActiveSubSection()
{
    list(,$subSection) = explode('/', $_SERVER["REQUEST_URI"]);

    if(in_array(strtolower($subSection), $legalSubSections = getLegalSubSections())) {
        return $subSection;
    }
    // unknown subsection; lets prevent XSS
    return $legalSubSections[0];
}
/**
 * The subsections that are allowed
 *
 * @return array of string
 */
function getLegalSubSections()
{
    return array('home', 'services', 'portfolio', 'weblog', 'about', 'contact');
}

echo '<body id="' . $pageId . '" class="' . getActiveSubSection() . '" >';
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

That looks much better. Very cool, thanks Ole.

I'm pretty sure it will only be used on Linux servers, as I'm already using htaccess for the rewrite stuff. But still, using forward slash seems better (and shorter).
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'm pretty sure it will only be used on Linux servers, as I'm already using htaccess for the rewrite stuff
Portability is not something I go heavily out of my way to achieve but if its easy there's really no reason why not.
Post Reply